| | |
User input sample code
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Oct 2006
Posts: 179
Reputation:
Solved Threads: 0
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...
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...
Java Syntax (Toggle Plain Text)
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.
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
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
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
•
•
Join Date: Oct 2006
Posts: 179
Reputation:
Solved Threads: 0
Hi,
Thanks guys, but what I am looking for is something like on this page:
url:
http://www.codeproject.com/useritems/javabasicsio.asp
Thanks guys, but what I am looking for is something like on this page:
Java Syntax (Toggle Plain Text)
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
Last edited by tech291083; Aug 5th, 2007 at 11:22 am.
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
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
The only thing I use for input on the CLI is
java.util.Scanner. Java Syntax (Toggle Plain Text)
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(); } }
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.
-Ginsburg
Don't tell me to "google it" - I already have.
![]() |
Similar Threads
- Tutorial: User Input: Strings and Numbers [C++] (C++)
- Tutorial: User Input: Strings and Numbers [C] (C)
- how to input joystick in c++ to this code? (Game Development)
- Handling Invalid User Input? (C)
- inline assembly getting user input.(djgpp) (Assembly)
- Wrong user input (C++)
- error checking of user input (C++)
- Creating a GUI that accepts user input help (Java)
- Need Help With Error Checking User Input (C)
- filtering bad user input (Java)
Other Threads in the Java Forum
- Previous Thread: Query : Object creation error
- Next Thread: engg. java project
Views: 10045 | Replies: 9
| Thread Tools | Search this Thread |
Tag cloud for Java
3d @param affinetransform android api apple applet application arc arguments array arrays automation binary bluetooth byte c# chat class classes click client code color compare component corrupted database detection draw eclipse error event exception file fractal game givemetehcodez graphics gui guitesting helpwithhomework html ide image input integer j2me java java.xls javaprojects jmf jni jpanel julia keytool linux list loop map method methods mobile netbeans newbie number object oracle pong print problem producer program programming project projectideas read recursion reflection replaysolutions rim scanner screen server set size sms socket sort sql string swing terminal test threads time transfer tree web windows






