2

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?

6
  • Where does your readline method come from? Do you define it? Commented Apr 22, 2009 at 15:51
  • Does this code actually match what generates your error. The above error suggests that you're not finding your main method, and so is that correctly defined (it does appear so above) Commented Apr 22, 2009 at 15:53
  • @phill: Have you seen my second edit? Commented Apr 22, 2009 at 19:13
  • @mmyers yes i did.. thanks .. that got it to start prompting for input Commented Apr 22, 2009 at 19:22
  • You still need to change the condition in the last while loop to i <= 2. As it is now, it will get three strings from the user but will only print the first two. Commented Apr 22, 2009 at 19:23

4 Answers 4

4

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".

Sign up to request clarification or add additional context in comments.

3 Comments

What is this javac of which you speak? :P
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..
Oh, oh... From that last comment, I'm thinking there are much deeper problems here.
1

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!

2 Comments

could you please detail how you found this in the api? Is there an easier search function on java.sun.com/javase/6/docs/api? is there an easier way than poking around the api until you find the function you need?
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'
1
//print out array
    while ( i < 2 ) { 
        System.out.println(s[i]);
        i++; //increase counter!
    } //end while 

1 Comment

Yes, logic errors are nastier than syntax errors. (A for-loop would be even better.)
1

This might not be what you are looking for, but it takes three command line arguments, stores them into an array, then prints out the arguments from the new array:

public class CommandArray {

    public static void main (String[] args){

        //Set up array to hold command line values
        String[] arr = new String[3];

        //Copy command line values into new array
        for(int i = 0;i < 3;i++)
            arr[i] = args[i];

        //Print command line values from new array
        for(int j = 0; j < 3; j++)
            System.out.print(arr[j] + " ");

            //Extra line for terminal
            System.out.println();
    }
}

Then, after you compile your code with javac CommandArray.java, you can execute it with java CommandArray Arg1 Arg2 Arg3.

Also, I noticed that, in your final while loop, you had:

while(i < 2) {

If the command line accepts 3 arguments, you would only print 2. The array indexes printed would be 0 and 1 since 1 < 2. You can change it to say:

while(i <= 2) {

And don't forget to increment i.

1 Comment

good catch.. thanks for pointing out the conditional statement

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.