• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Getting an input from the user using command-line arguments?

 
beu curt
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I'm new to this topic and we have this activity that we will input words then the output should break lines each word.
For example, if the user entered,

Java Hello world that is all

your program should print

Hello
world
that
is
all

My question is do i have to use joption or buffered reader to get an input? Because I'm new to this command line topic and I am really lost which part will the user input the words.

thanks.
 
Rob Spoor
Sheriff
Pie
Posts: 20744
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is not user input, these are command line arguments. They are available as the only argument to the main method.
 
beu curt
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Like this program it doesn't show the expected output. Where should I input the words?
The build is successful but it doesn't do anything.
 
Rob Spoor
Sheriff
Pie
Posts: 20744
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the signature of the main method? What are its parameters? What happens if you print the values of those parameters?

Wait a second. I think I misunderstood. I thought that the "Java Hello world that is all" was how you started the application. It is what the user enters after the application is started.

The source of your data is System.in. Now you can go two ways:
1) use a BufferedReader around an InputStreamReader around System.in; read per line, then split (hint: that's a method) the line on spaces
2) use a Scanner around System.in. Scanner has lots of methods to read data; the easiest are hasNext() and next() that read one word at a time. Note that input is only sent to Java when the user pressed enter.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How are you running the program?
 
beu curt
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK here is the problem my friend gave:
Get input from the user using command-line arguments and print all the arguments to the screen. For example, if the user entered,

Java Hello world that is all

your program should print

Hello
world
that
is
all


I press shift + f6 to make the program run, I'm using netbeans.O.O
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then you need to set up NetBeans to supply some command line parameters to the program.
 
Rob Spoor
Sheriff
Pie
Posts: 20744
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So my first guess was right after all. Your code does just that, you just need to provide some command line arguments.
 
Campbell Ritchie
Sheriff
Posts: 51332
86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But you can't get it to print "Hello" with those command-line arguments unless you have a line saying

System.out.println("Hello");

The first token after "java" is the name of the class, the remaining tokens form the String[] array for the command-line arguments which we usually call "args". For a simple application like that, don't use NetBeans, and use the enhanced for (or for-each) loop.
 
Rob Sweeny
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On this topic:

What is the correct anotation to send in multiple command line args?

Would it be like this: java SkipLine -Hello -World
 
fred rosenberger
lowercase baba
Bartender
Posts: 12263
36
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nope.

java SkipLine Hello World

that's it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic