944,126 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 34440
  • Java RSS
Aug 5th, 2007
0

User input sample code

Expand Post »
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...
Similar Threads
Reputation Points: 15
Solved Threads: 0
Junior Poster
tech291083 is offline Offline
181 posts
since Oct 2006
Aug 5th, 2007
0

Re: User input sample code

google + java util scanner? (Maybe)
Last edited by iamthwee; Aug 5th, 2007 at 10:02 am.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Aug 5th, 2007
0

Re: User input sample code

Java Syntax (Toggle Plain Text)
  1. 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.
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Aug 5th, 2007
0

Re: User input sample code

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
Last edited by peter_budo; Aug 5th, 2007 at 11:10 am.
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 874
Code tags enforcer
peter_budo is offline Offline
6,659 posts
since Dec 2004
Aug 5th, 2007
0

Re: User input sample code

Hi,

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


Java Syntax (Toggle Plain Text)
  1.  
  2. import java.io.* ;
  3.  
  4. class Tut1 {
  5. public static void main(String args[])
  6. {
  7. InputStreamReader istream = new InputStreamReader(System.in) ;
  8. BufferedReader bufRead = new BufferedReader(istream) ;
  9.  
  10. System.out.println("Welcome To My First Java Program");
  11.  
  12. try {
  13. System.out.println("Please Enter In Your First Name: ");
  14. String firstName = bufRead.readLine();
  15.  
  16. System.out.println("Please Enter In The Year You Were Born: ");
  17. String bornYear = bufRead.readLine();
  18.  
  19. System.out.println("Please Enter In The Current Year: ");
  20. String thisYear = bufRead.readLine();
  21.  
  22. int bYear = Integer.parseInt(bornYear);
  23. int tYear = Integer.parseInt(thisYear);
  24.  
  25. int age = tYear – bYear ;
  26. System.out.println("Hello " + firstName + ". You are " + age + " years old");
  27.  
  28. }
  29. catch (IOException err) {
  30. System.out.println("Error reading line");
  31. }
  32. catch(NumberFormatException err) {
  33. System.out.println("Error Converting Number");
  34. }
  35.  
  36. }
  37. }


url:

http://www.codeproject.com/useritems/javabasicsio.asp
Last edited by tech291083; Aug 5th, 2007 at 11:22 am.
Reputation Points: 15
Solved Threads: 0
Junior Poster
tech291083 is offline Offline
181 posts
since Oct 2006
Aug 5th, 2007
0

Re: User input sample code

That is what you can find in Java tutorials which I gave it to you...
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 874
Code tags enforcer
peter_budo is offline Offline
6,659 posts
since Dec 2004
Aug 5th, 2007
0

Re: User input sample code

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?
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Aug 5th, 2007
0

Re: User input sample code

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

Java Syntax (Toggle Plain Text)
  1. import java.util.Scanner;
  2.  
  3. public class Input {
  4.  
  5. private Scanner scanner;
  6. private String ps = "> ";
  7.  
  8. public Input {
  9.  
  10. scanner = new Scanner(System.in);
  11.  
  12. }
  13.  
  14. public String input(String prompt) {
  15.  
  16. System.out.print(prompt + ps);
  17. return scanner.nextLine();
  18.  
  19. }
  20.  
  21. }
Last edited by indienick; Aug 5th, 2007 at 3:12 pm.
Reputation Points: 23
Solved Threads: 2
Junior Poster in Training
indienick is offline Offline
71 posts
since Aug 2005
Aug 5th, 2007
0

Re: User input sample code

never used that, grew up with the original APIs and now use commandline arguments and configuration files exclusively as user interfaces
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Aug 5th, 2007
0

Re: User input sample code

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

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Query : Object creation error
Next Thread in Java Forum Timeline: engg. java project





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC