User input sample code

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Oct 2006
Posts: 179
Reputation: tech291083 is an unknown quantity at this point 
Solved Threads: 0
tech291083 tech291083 is offline Offline
Junior Poster

User input sample code

 
0
  #1
Aug 5th, 2007
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...
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,273
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 378
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: User input sample code

 
0
  #2
Aug 5th, 2007
google + java util scanner? (Maybe)
Last edited by iamthwee; Aug 5th, 2007 at 10:02 am.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 212
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: User input sample code

 
0
  #3
Aug 5th, 2007
  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.
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 4,272
Reputation: peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of 
Solved Threads: 494
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Code tags enforcer

Re: User input sample code

 
0
  #4
Aug 5th, 2007
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.
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)

LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 179
Reputation: tech291083 is an unknown quantity at this point 
Solved Threads: 0
tech291083 tech291083 is offline Offline
Junior Poster

Re: User input sample code

 
0
  #5
Aug 5th, 2007
Hi,

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


  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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 4,272
Reputation: peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of 
Solved Threads: 494
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Code tags enforcer

Re: User input sample code

 
0
  #6
Aug 5th, 2007
That is what you can find in Java tutorials which I gave it to you...
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)

LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 212
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: User input sample code

 
0
  #7
Aug 5th, 2007
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?
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 71
Reputation: indienick is an unknown quantity at this point 
Solved Threads: 2
indienick's Avatar
indienick indienick is offline Offline
Junior Poster in Training

Re: User input sample code

 
0
  #8
Aug 5th, 2007
The only thing I use for input on the CLI is java.util.Scanner.

  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.
Angel-headed hipsters burning for the ancient heavenly connection, to the starry dynamo in the machinery of the night.
-Ginsburg

Don't tell me to "google it" - I already have.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 212
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: User input sample code

 
0
  #9
Aug 5th, 2007
never used that, grew up with the original APIs and now use commandline arguments and configuration files exclusively as user interfaces
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 71
Reputation: indienick is an unknown quantity at this point 
Solved Threads: 2
indienick's Avatar
indienick indienick is offline Offline
Junior Poster in Training

Re: User input sample code

 
0
  #10
Aug 5th, 2007
Angel-headed hipsters burning for the ancient heavenly connection, to the starry dynamo in the machinery of the night.
-Ginsburg

Don't tell me to "google it" - I already have.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 10045 | Replies: 9
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC