1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Development Java command line input

Discussion in 'Software' started by 731|\|37, 10 Nov 2004.

  1. 731|\|37

    731|\|37 ESD Engineer in Training

    Joined:
    5 Sep 2004
    Posts:
    1,047
    Likes Received:
    0
    Is there an easy way for a java n00b to take input from a command line?

    thanks
     
  2. the harlequin

    the harlequin What's a Dremel?

    Joined:
    31 Oct 2004
    Posts:
    41
    Likes Received:
    0
    This is how we got taught in first year comp sci:

    Code:
    static String readln(String message) {
    			// this is the method that reads a String from the terminal / console
    		
    			BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    			String line = "";
    			System.out.println(message);
    			try {
    			line = reader.readLine();
    			} catch (IOException e){
    				System.out.println("Failed to read a String, error: " + e);
    			} // try catch
    			return line;
    		} // readln 
    
    This'll read the input line by line. Just call the function when ever you want a line read, parse it a string, and it'll prompt the user for it.

    e.g.

    Code:
    //in a method
    String name = readln("Input your first name.");
    
    Outputs:
    Input your first name.
    [waits for input, and for the user to hit the enter key]
     
  3. Jamie

    Jamie ex-Bit-Tech code junkie

    Joined:
    12 Mar 2001
    Posts:
    8,180
    Likes Received:
    54
    Any introductory Java book will tell you how to do this.
     
  4. 731|\|37

    731|\|37 ESD Engineer in Training

    Joined:
    5 Sep 2004
    Posts:
    1,047
    Likes Received:
    0
    thanks that last line is what i needed (not into error handeling yet)

    ya its in my book somewhere but for the life of me i cant find it.

    thanks



    EDIT: im using readln() but im gettting an error uppon compile.

    Code:
    --------------------Configuration: numberConverter - j2sdk1.4.1 <Default>--------------------
    C:\Program Files\Xinox Software\JCreator LE\MyProjects\Current project\numberConverter\numberConverter.java:64: cannot resolve symbol
    symbol  : method readln (java.lang.String)
    location: class numberConverter
    		input1 = readln("Enter first number");
                             ^
    1 error
    
    Process completed.
     
    Last edited: 11 Nov 2004
  5. the harlequin

    the harlequin What's a Dremel?

    Joined:
    31 Oct 2004
    Posts:
    41
    Likes Received:
    0
    Can you post the whole code?
     
  6. Deviate

    Deviate What's a Dremel?

    Joined:
    3 Jun 2002
    Posts:
    1,515
    Likes Received:
    7
    Did you define the method readln() somewhere in your class? The most important part about what the harlequin gave in code is not the line that says:

    String name = readln("Input your first name.");

    It's the part where he coded the readln() method. All that last line is, is in another method somewhere you are asking for input from the user.

    Here is a good article telling you what the harlequin told you, but with a little explanation of each step. BTW, that was just from a Google for "Java take input from command line". :D
     
  7. 731|\|37

    731|\|37 ESD Engineer in Training

    Joined:
    5 Sep 2004
    Posts:
    1,047
    Likes Received:
    0
    so thers nothing like <STDIN> (perl) i can use? im just looking for a way to test my logic without getting the GUI in place and dealing with an event handler (its being a PITA). :sigh: Its not exactly appealing to me to write annother method just to take input. (im actually writing this in applet form so ide have to fool arround with init and such) but if this is the ony way.?.?.?
     
  8. the harlequin

    the harlequin What's a Dremel?

    Joined:
    31 Oct 2004
    Posts:
    41
    Likes Received:
    0
    In it's most basic form, all you need to write is:
    Code:
    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    String var_name = reader.readLine();
    
    I think that'll work
     
  9. 731|\|37

    731|\|37 ESD Engineer in Training

    Joined:
    5 Sep 2004
    Posts:
    1,047
    Likes Received:
    0
    Compiler wants me to catch or throw the exception (neither of which i know how to do) :wallbash:

    i liker perls way muvh better
     
  10. the harlequin

    the harlequin What's a Dremel?

    Joined:
    31 Oct 2004
    Posts:
    41
    Likes Received:
    0
    Put this function in your code somewhere, just copy and paste. Then call the function.
     

Share This Page