954,228 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

User input sample code

Hi,
I have been trying to learn java on my own on Linux from command line. Can any one please give me a few very basic samples of code that ask the user for an input (interger/double/float/string all possible data types)? I have downloaded the official Sun Java tutorials but it does not contain many examples of user input from command line. Thanks...

tech291083
Junior Poster
181 posts since Oct 2006
Reputation Points: 15
Solved Threads: 0
 

google + java util scanner? (Maybe)

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 
System.out.println("Please provide some input");


That's all you need to do to ask for input.
How you capture that input and what you do with it i'll leave as an exercise to the reader.

jwenting
duckman
Team Colleague
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
 

Sun Java Tutorial website always good place to start
Basic I/O . As iamthwee said scanner is what you looking for start so Scanning is your tutorial, reading rest of it will not harm you either

peter_budo
Code tags enforcer
Moderator
15,436 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 901
 

Hi,

Thanks guys, but what I am looking for is something like on this page:

import java.io.* ;
 
class Tut1 {
     public static void main(String args[])
     {
          InputStreamReader istream = new InputStreamReader(System.in) ;
          BufferedReader bufRead = new BufferedReader(istream) ;
 
          System.out.println("Welcome To My First Java Program");
 
          try {
               System.out.println("Please Enter In Your First Name: ");
               String firstName = bufRead.readLine();
 
               System.out.println("Please Enter In The Year You Were Born: ");
               String bornYear = bufRead.readLine();
 
               System.out.println("Please Enter In The Current Year: ");
               String thisYear = bufRead.readLine();
 
               int bYear = Integer.parseInt(bornYear);
               int tYear = Integer.parseInt(thisYear);
 
               int age = tYear – bYear ;
               System.out.println("Hello " + firstName + ". You are " + age + " years old");
 
          }
          catch (IOException err) {
               System.out.println("Error reading line");
          }
          catch(NumberFormatException err) {
               System.out.println("Error Converting Number");
          }  
 
     } 
}



url: http://www.codeproject.com/useritems/javabasicsio.asp

tech291083
Junior Poster
181 posts since Oct 2006
Reputation Points: 15
Solved Threads: 0
 

That is what you can find in Java tutorials which I gave it to you...

peter_budo
Code tags enforcer
Moderator
15,436 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 901
 

and what more would you need than have everything predigested for you like that sample does?
Want someone to put in the exact text you want to show your users for you?

jwenting
duckman
Team Colleague
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
 

The only thing I use for input on the CLI is java.util.Scanner .

import java.util.Scanner;

public class Input {

  private Scanner scanner;
  private String ps = "> ";

  public Input {
  
    scanner = new Scanner(System.in);

  }

  public String input(String prompt) {
 
    System.out.print(prompt + ps);
    return scanner.nextLine();

  }

}
indienick
Junior Poster in Training
71 posts since Aug 2005
Reputation Points: 23
Solved Threads: 2
 

never used that, grew up with the original APIs and now use commandline arguments and configuration files exclusively as user interfaces :)

jwenting
duckman
Team Colleague
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
 
indienick
Junior Poster in Training
71 posts since Aug 2005
Reputation Points: 23
Solved Threads: 2
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You