How does the input function in java work?
Input
Collapse
X
-
Tags: None
-
Input for strings and numbers. I don't know how to answer your questions. I'm new to java. Just do any of them.Originally posted by pronerdWhat input method, in what object, in what library?Comment
-
-
Prompting (aka output) can be done through System.out.prin tln(), or System.out.prin t() if you don't want the newline. As to input, Scanner is what you want. You also might want to look at Sun's Java tutorials for all sorts of useful information.Comment
-
If you're using 1.6, I like java.io.Console in simple situations: http://java.sun.com/javase/6/docs/ap...o/Console.htmlComment
-
System.out.prin tln and System.out.prin t simply make a message appear. I want the user to actually be able to enter something. In the language python for example the command would be:
blah = input("please enter a number or word: ")Comment
-
Hence the link to the Scanner class I posted. The example code there shows how to use it as console-based input. Just remember to put 'import java.util.Scann er;' at the top of your file to use it. You have to use two commands to do the same thing as input("blah") in Python. One to output the string, one to read the input from the user.Comment
-
Okay I got it to work. Thanks. One more question. Is it possible to have the user only input a number.Comment
-
How can you prompt the user if it is a wrong input?Originally posted by sukatoaIn console? That is impossible... but you can prompt the user if it is a wrong input.....
regards,
sukatoaComment
-
Capture the user's input... ( variable should handle this )...Originally posted by Kid ProgrammerHow can you prompt the user if it is a wrong input?
Compare it with what you want.... since it should be a number....
Convert it first into arrays of characters.
Loop in every element and compare it with digits... ( depends on how you will implement them )....
or simply put a try/catch block inside the conversion code...
regards,Code:try{ int digit = new java.util.Scanner(System.in).nextInt(); }catch(InputMismatchException em){ new javax.swing.JOptionPane().showMessageDialog(null, "BAD INPUT!!"); }
sukatoaComment
Comment