views:

1724

answers:

4

I'm working on a Bruce Eckel exercise on how to take a keyboard command line input and put it into an array. It's supposed to take 3 separate inputs and place them into an array and print them back out.

Here is the code I have so far:

//: object/PushtoArray
import java.io.*;
import java.util.*; 
class PushtoArray { 
    public static void main(String[] args) { 
    String[] s = new String[3]; 
    int i = 0; 

    //initialize console 
    Console c = System.console();   

    while ( i <= 2 ) {
     //prompt entry:
     //System.out.println("Enter entry #" + i + ": ");  
     //readin 
            s[i] = c.readLine("enter entry #" + i + ": "); 

     //increment counter 
     i++;    

    } //end while 

    //reset counter 
    i = 0; 

    //print out array
    while ( i <= 2 ) { 
     System.out.println(s[i]);
            i++;
    } //end while 
    }//end main

} //end class

UPDATE: Now I get a different error:

PushtoArray.java:15: readline(boolean) in java.io.Console cannot be applied to (java.lang.string) 
      s[i]= c.readline("Enter entry #" + i + ": ");

I'm trying to read in from the command prompt but it isn't prompting at all. It compiles correctly when I javac the java file.

Am I using the wrong function? Should I be using a push method instead of an assignment?

+4  A: 

Are you sure you're running javac on the right file? There is no way that file could compile.

  1. You need a semicolon on the import statement:

    import java.io.*;
    
  2. You forgot an end brace:

    } // end main()
    
  3. There is no such method as readline(). You need to read from System.in. The easiest way is to make a Scanner before the loop, then read from it in the loop. See the Javadocs for Scanner for an example.

Edit: See the Java Tutorials for more on reading from the command line. The Console class (introduced in Java 6) looks like it has the readLine() method that you wanted.

Edit 2: You need to capitalize Line. You wrote "readline", but it should be "readLine".

Michael Myers
What is this `javac` of which you speak? :P
erickson
thanks.. i made the corrections on the code. I found the readline() method by going java.io > DataInput > string readline() method.. i guess this is incorrect..
phill
Oh, oh... From that last comment, I'm thinking there are much deeper problems here.
Michael Myers
+1  A: 

Try

BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

and then in your loop:

in.readline();

I would push the received lines into an ArrayList or similar. That way you can accept more/less than 3 lines of data (but that's only necessary if the number of lines you want to accept is variable)

EDIT: I thought originally the NoSuchMethod exception was highlighting a scoping problem. Obviously not, and thx to those who pointed that out!

Brian Agnew
could you please detail how you found this in the api? Is there an easier search function on http://java.sun.com/javase/6/docs/api/? is there an easier way than poking around the api until you find the function you need?
phill
Ah. Experience, I'm afraid. Knowing that System.in is the input stream, and that you have to wrap it in a reader to convert to chars, and then BufferedReader delivers lines. You could start by searching for 'java readline'
Brian Agnew
+1  A: 
//print out array
    while ( i < 2 ) { 
        System.out.println(s[i]);
        i++; //increase counter!
    } //end while
Jani
Yes, logic errors are nastier than syntax errors. (A for-loop would be even better.)
Michael Myers
+1  A: 
good catch.. thanks for pointing out the conditional statement
phill