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 https://www.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 Oct 30 '16 at 17:11
-
too broad and very basic java question – Nadim Baraky Oct 30 '16 at 17:14
-
I'm talking about getting input from command prompt, not command line or console. – Luis Oct 30 '16 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 Oct 30 '16 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 info – Luis Oct 30 '16 at 17:23
2 Answers
In your program use input command given below to input any character :
char ch;
ch = (char)System.in.read();
To print the character the user has entered use the command given below :
System.out.println("entered character is" + ch);
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.

- 99
- 1
- 10