I have been able to open the command prompt, but how would I get what the user types from it? And how would I "print" to the command prompt?
-
This is basic subject of any Java tutorial (or even site with examples like mkyong.com/java/how-to-read-input-from-console-java). Please go through one, try described there solution and come back if you will face any problem.Pshemo– Pshemo2016-10-30 17:11:28 +00:00Commented Oct 30, 2016 at 17:11
-
too broad and very basic java questionNadim Baraky– Nadim Baraky2016-10-30 17:14:10 +00:00Commented Oct 30, 2016 at 17:14
-
I'm talking about getting input from command prompt, not command line or console.Luis– Luis2016-10-30 17:20:23 +00:00Commented Oct 30, 2016 at 17:20
-
What do you mean, you want just to do some basic input/output operation or to communicate with a windows command line process?aleb2000– aleb20002016-10-30 17:20:44 +00:00Commented Oct 30, 2016 at 17:20
-
From windows cmd. Like if I run a .jar that opens the cmd prompt I want to get the input that user types from it. Same way you would do something like "ipconfig" on command prompt and it would show a bunch of infoLuis– Luis2016-10-30 17:23:26 +00:00Commented Oct 30, 2016 at 17:23
2 Answers
The command prompt is simply another view of a java log console that view messages you tell it to print using the System.out.println command or that it will print errors, get input etc...
To get input simply declare and initialize a Scanner:
Scanner s = new Scanner(System.in);
For getting input with the scanner, that will be stored in a String or int etc... use:
String dataReaden = s.nextLine();
So this will read the line that the user inputed when the method is called.
there are many more methods such as this which you can discover.
If you are talking about getting input when running the jar file through the command prompt, like:
java -jar myfile.jar hello world
The text you input after the "java -jar" command is stored in the String[] args in the main method of the jar. So this means that args[0] = hello and args[1] = world .
Hope this helps.