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

Member Avatar for iamthwee

google + java util scanner? (Maybe)

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.

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

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

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

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?

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();

  }

}

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

:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.