This week's book giveaway is in the Agile forum.
We're giving away four copies of Head First Agile and have Andrew Stellman & Jennifer Greene on-line!
See this thread for details.
Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

multiple inputs on one line using the scanner class  RSS feed

 
rick pine
Ranch Hand
Posts: 90
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a way to get multiple inputs on one line?
example
system.print("  enter two numbers: ");
int num1 = input.nextInt();
int num2 = input.nextInt();

this would create an output like this
enter two numbers: 5
4

Any suggestions, besides an array?
oh i tried placing the two input.nextInt(); on the same line, no luck either.
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can take an input as string separated by delimiter may be comma or space or anything.. then you can split the string as per delimiter
 
Norm Radder
Ranch Foreman
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a way to get multiple inputs on one line? 

Yes the user should be able to enter multiple input values on one line.  Have you tried it?

What did you try to enter?  Remember not  to press Enter until all the values have been entered for the line.

Your example shows the input on 2 lines: 5 on the first and 4 on the second.
 
rick pine
Ranch Hand
Posts: 90
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perhaps my question is not well structured. While it is obviously possible to enter multiple values on the dame line, that doesnt mean that i can do much with them.
Let me try again, i would like to enter multiple values on the same line and be able to read in those values. I could be wrong but, in order to read in , i have to hit entrer. Hitting entrer takes the cursor on a new line, thats where my problem is. I can display a promt using print instead of println. But after i read in, cursor moves to the nextline.
 
Stephan van Hulst
Saloon Keeper
Posts: 7821
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So when do you expect the program to process the first value? If you want it to process when you hit enter, the command line is going to move to the next line. That has nothing to do with Java, but with the command line prompt.
 
Norm Radder
Ranch Foreman
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure what you are trying to do.
i would like to enter multiple values on the same line and be able to read in those values.

Yes that can be done .
If on the console window you type several values separated by spaces and hit Enter,
those values are then passed to the Scanner object and can be read by its methods like next(), nextInt(), etc
or the whole line by nextLine()
On the console, the cursor will have been moved to a new line when Enter was pressed.
 
rick pine
Ranch Hand
Posts: 90
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Allright, i feel like an idiot.
So yeah, i entered two numbers on the same line then preseed enter and the expected output, an addition was computed and displayed on the console.
Sorry, i was under the impression that for every .nextInt i had to press enter to actually read in the value. Now i now its not necessary. 🙈
Thanks for helping out.
 
Stephan van Hulst
Saloon Keeper
Posts: 7821
142
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's how we learn.

I feel like an idiot on a daily basis, and I think I'm better for it :P
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!