aspose file tools*
The moose likes Beginning Java and the fly likes read numbers from the standard input into an array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "read numbers from the standard input into an array" Watch "read numbers from the standard input into an array" New topic
Author

read numbers from the standard input into an array

mehmet emin
Greenhorn

Joined: Dec 12, 2008
Posts: 8
Hi again,
I have an assignment which requires to read numbers from the standard input into an array until something which is not a number appears (empty line). My instructor added this last bracket particularly and I have no idea how to implement that part but I can think of a way to construct the array similar to this:


But anotherArray=(Integer.parseInt(args[ i ])) didn't put the numbers into the array. Then I thought about replacing it with:



but I determine the args.length by adding numbers (actually strings) to the command line. So the loop will be meaningless. Any clue?
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by mehmet emin:
... So the loop will be meaningless...

Why would this loop be meaningless? You are starting with an array of Strings (args), and you are looping through the elements to parse them as ints and add them to another array. That seems reasonable.

But...

Is the intent really to input all the numbers as arguments to main? Or is the program supposed to be more interactive? For example, ask the user for a number, then process it, then ask for another number, then process it... Until the user enters something other than a number?


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36579
    
  16
Originally posted by mehmet emin:


I agree with Marc. As far as I can see, if you write
java MyClass 1 2 3 4
that statement will successfully create an int[] with the contents 1, 2, 3, 4. That might be what you want.
But if you do that all in the main method, you have that array as a local variable in the main method, and you will have to pass it elsewhere to be of use elsewhere.
mehmet emin
Greenhorn

Joined: Dec 12, 2008
Posts: 8
Actually, my assignment has three parts.
1-Read numbers from standard input into an array until something which is not a number appears (empty line).
2-Calculate something about the array by using a function A.
3-Calculate some other thing about the array by using a function B.

Since mainly it is the first time I'm dealing with java, my intend is not accomplish everything I am asked. I'm just trying to get whatever I can.

Originally posted by Campbell Ritchie:

...But if you do that all in the main method, you have that array as a local variable in the main method, and you will have to pass it elsewhere to be of use elsewhere.


Yes, I will try to pass it to my A and B methods but I'm going step by step to understand everything I'm doing. Firstly, I tried to figure out the command line business and pass the array into a very basic method. I tried to print out the elements of the array like this:



But it prints only the first element of the array. Is my error in myMethod or main?

Originally posted by marc weber:

...Is the intent really to input all the numbers as arguments to main? Or is the program supposed to be more interactive? For example, ask the user for a number, then process it, then ask for another number, then process it... Until the user enters something other than a number?


Does it mean that I have to add some conditions to main or I am doing a more complicated mistake?

edit:--Sorry for this stupid mistake. Above code is working just fine. Ignore that question.
[ December 14, 2008: Message edited by: mehmet emin ]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36579
    
  16
The only error I can see is inconsistent indentation, which makes your cide hard to read and will lose marks if you hand it in! Apart from that, I can't see why you aren't getting all your numbers printed.

What are you passing? I copied and pasted your sample:
[Campbell@queeg java]$ gedit SampleClass3.java
[Campbell@queeg java]$ javac SampleClass3.java
[Campbell@queeg java]$ java SampleClass3 1 2 3 4
1
2
3
4
[Campbell@queeg java]$
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36579
    
  16
Is using the command line arguments quite the same as "reading from standard input?" Please ask. You will lose marks if you do the wrong thing.
mehmet emin
Greenhorn

Joined: Dec 12, 2008
Posts: 8
Originally posted by Campbell Ritchie:
Is using the command line arguments quite the same as "reading from standard input?" Please ask. You will lose marks if you do the wrong thing.


I don't know if it's true in programming sense but we are asked to do it by using command line arguments.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36579
    
  16
If they said "command line" then you are all right
mehmet emin
Greenhorn

Joined: Dec 12, 2008
Posts: 8
What I don't understand is this, the assignment says "read until something which is not a number appears" but the arguments we send from command line are not numbers already. Does it mean that stop when you encounter an error while parsing the arguments into integers?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36579
    
  16
Write in the assignment that you interpret "until something is not a number" as meaning when you push "enter" after "1 2 3 4"
mehmet emin
Greenhorn

Joined: Dec 12, 2008
Posts: 8
Originally posted by Campbell Ritchie:
Write in the assignment that you interpret "until something is not a number" as meaning when you push "enter" after "1 2 3 4"


Could the meaning of the "new line" in the assignment be that? Then I'll be very happy But I want to figure out how to add conditions on the arguments.

Is it possible to test if Integer.parseInt(args[i]) returns an integer or not?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36579
    
  16
The easiest way to see whether a command-line argument is an integer is to use a Regular Expression. As an alternative you could look for the NumberFormatException if you pass the wrong sort of argument. There is a third way: write this:
I asked on JavaRanch and Campbell Ritchie told me that using Exceptions or regular expressions to check whether the argument is actually an integer would be beyond the scope of the present assignment.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: read numbers from the standard input into an array
 
Similar Threads
how to create reverse numbers by comand line
array index out of bound problem
functions in java?
Finding maximum value from command line arguments.
populating an array via the user