aspose file tools*
The moose likes Beginning Java and the fly likes Getting an input from the user using command-line arguments? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Getting an input from the user using command-line arguments?" Watch "Getting an input from the user using command-line arguments?" New topic
Author

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

beu curt
Greenhorn

Joined: Jan 16, 2010
Posts: 18
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

Joined: Oct 27, 2005
Posts: 19543
    
  16

This is not user input, these are command line arguments. They are available as the only argument to the main method.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
beu curt
Greenhorn

Joined: Jan 16, 2010
Posts: 18


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

Joined: Oct 27, 2005
Posts: 19543
    
  16

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

Joined: Sep 29, 2008
Posts: 12617

How are you running the program?
beu curt
Greenhorn

Joined: Jan 16, 2010
Posts: 18
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

Joined: Sep 29, 2008
Posts: 12617

Then you need to set up NetBeans to supply some command line parameters to the program.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19543
    
  16

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

Joined: Oct 13, 2005
Posts: 36508
    
  16
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

Joined: Jan 14, 2010
Posts: 16
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

Joined: Oct 02, 2003
Posts: 10916
    
  12

nope.

java SkipLine Hello World

that's it.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Getting an input from the user using command-line arguments?
 
Similar Threads
About GUI start
strings
Guidance on Chat Server structures
HTML templates
Hello World with EJB