phill
phill

Reputation: 13844

Turn commandline input into an array

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?

Upvotes: 2

Views: 12230

Answers (4)

glue
glue

Reputation:

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.

Upvotes: 1

Michael Myers
Michael Myers

Reputation: 191875

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

Upvotes: 4

Jani
Jani

Reputation: 111

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

Upvotes: 1

Brian Agnew
Brian Agnew

Reputation: 272247

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!

Upvotes: 1

Related Questions