File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Taking input from keyboard: takes ints, but not string

 
Rahul Sudip Bose
Ranch Hand
Posts: 637
Eclipse IDE Firefox Browser Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The program below compiles. But there is a problem during running. It does not accept a String, it accepts only ints. Where have i made a mistake ?



Here is the incorrect output :
 
Rob Spoor
Sheriff
Pie
Posts: 20368
43
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The nextLine() reads the enter that you entered for entering the first number. nextInt() only takes the 1111, not the enter after that.
 
Rahul Sudip Bose
Ranch Hand
Posts: 637
Eclipse IDE Firefox Browser Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:The nextLine() reads the enter that you entered for entering the first number. nextInt() only takes the 1111, not the enter after that.


Can you suggest a way to take the data correctly ? Should i take all data as strings using nextLine() and convert such a string into an int when required ?
 
Roger Fed
Ranch Hand
Posts: 82
Java Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should use "next()" method instead of "nextLine()" as following

this method will take the data correctly.
 
Matthew Brown
Bartender
Posts: 4549
8
Java Netbeans IDE Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that by default Scanner splits on whitespace, so just using next() as it is won't work if you want to be able to enter a name with spaces in it. You can set the delimiter that the Scanner is using to split on an end-of-line to avoid that.
 
Rahul Sudip Bose
Ranch Hand
Posts: 637
Eclipse IDE Firefox Browser Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:Note that by default Scanner splits on whitespace, so just using next() as it is won't work if you want to be able to enter a name with spaces in it. You can set the delimiter that the Scanner is using to split on an end-of-line to avoid that.


Can you illustrate that with code ?
 
prem pillai
Ranch Hand
Posts: 87
Java Opera
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


 
Rahul Sudip Bose
Ranch Hand
Posts: 637
Eclipse IDE Firefox Browser Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
prem pillai wrote:



Can you please tell me what the above code is doing ? I have never used delimiter(), useDelimiter() etc ?
 
Campbell Ritchie
Sheriff
Pie
Posts: 47216
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That delimiter is hazardous; it would restrict your application to Windows platforms. I don't think it would be helpful, since the default delimiter is whitespace, which includes CR and LF.
Your printing delimiter method won't help; since it is whitespace you won't see anything on screen!
There is something about delimiters in the API documentation for Scanner.
There is another explanation about line ends and Scanner here, but I would never believe anything that person writes
 
prem pillai
Ranch Hand
Posts: 87
Java Opera
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:That delimiter is hazardous; it would restrict your application to Windows platforms. I don't think it would be helpful, since the default delimiter is whitespace, which includes CR and LF.
Your printing delimiter method won't help; since it is whitespace you won't see anything on screen!
There is something about delimiters in the API documentation for Scanner.
There is another explanation about line ends and Scanner here, but I would never believe anything that person writes


Output of the above code is here; My reply is there in the output.. Green colored text is console input , back text with -> prefix is output .

\p{javaWhitespace}+
Hello Campbell Ritchie
->Hello Campbell Ritchie
this code works ... windows only thingy you may be correct
->this code works ... windows only thingy you may be correct
I am giving him just a sample of using delimiters
->I am giving him just a sample of using delimiters
printing default delimiter also works ...see the first line in the output :-)
->printing default delimiter also works ...see the first line in the output :-)

Rahul Sudip Bose wrote:Can you please tell me what the above code is doing ? I have never used delimiter(), useDelimiter() etc ?

Why don't you try running it .... sample output is given above...

 
prem pillai
Ranch Hand
Posts: 87
Java Opera
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just by replacing the statment in line number 2 which is using would make it platform independent ..

Sorry for posting platform specific code earlier... I was just trying to give a sample program to show delimiter usage in Scanner.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47216
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It didn't work quite as well for me on a Linux box
\p{javaWhitespace}+
Hello Campbell Ritchie
###


->Hello Campbell Ritchie
###



Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:838)
at java.util.Scanner.next(Scanner.java:1347)
at ScannerDemo.readWithDelimiter(ScannerDemo.java:68)
at ScannerDemo.main(ScannerDemo.java:13)
[2]+ Done
I had to use ctrl-D to terminate the input. You are right about using the system property.
 
Rahul Sudip Bose
Ranch Hand
Posts: 637
Eclipse IDE Firefox Browser Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
prem pillai wrote:Just by replacing the statment in line number 2 which is using would make it platform independent ..

Sorry for posting platform specific code earlier... I was just trying to give a sample program to show delimiter usage in Scanner.


I used a separate scanner object for just taking the "string two". The rest of the code is the same as original program. Before i can figure out all those new methods and how they affect one another, i will use this way. Is there anything bad with this way ?




 
Rahul Sudip Bose
Ranch Hand
Posts: 637
Eclipse IDE Firefox Browser Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
prem pillai wrote:

...Output of the above code is here; My reply is there in the output.. Green colored text is console input , back text with -> prefix is output ....



Can you modify my original source code so that it works ?
 
prem pillai
Ranch Hand
Posts: 87
Java Opera
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here you go;

Please note that this code has bugs in it. For example , if you enter a string with a space for "Your name" prompt , it will fail.. Better you go through the java doc for Scanner and understand it so that you can use it effectively.. Also try to find out the difference between next() and nextLine() method ..!!
 
Campbell Ritchie
Sheriff
Pie
Posts: 47216
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Demonstration of a poorly-documented pitfall with Scanner here.
 
prem pillai
Ranch Hand
Posts: 87
Java Opera
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:I had to use ctrl-D to terminate the input. You are right about using the system property.




Campbell , take a look at the exit condition on line 7 ... you already have an option to exit gracefully from the loop ... Don't know why are you saying it didn't work for you!!
 
Campbell Ritchie
Sheriff
Pie
Posts: 47216
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
prem pillai wrote: . . . Campbell , take a look at the exit condition on line 7 ... you already have an option to exit gracefully from the loop ... . . .
There is nothing graceful about break;
I have already told you; it takes no input because it won't match the delimiter. If you want a graceful way to exit the loop try thisThe reason I didn't get the output you expected was because I was using your original code with \r\n as a delimiter.

Please don't use tab characters for indenting, but only spaces.
 
prem pillai
Ranch Hand
Posts: 87
Java Opera
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
There is nothing graceful about break;
I have already told you; it takes no input because it won't match the delimiter. If you want a graceful way to exit the loop try this

Sir graceful is too relative ... so what is graceful for you may not be for me & vice versa ...
Campbell Ritchie wrote:The reason I didn't get the output you expected was because I was using your original code with \r\n as a delimiter.

You can't blame me for that ...I had corrected the code when you have mentioned its not working in linux for the first time. But then again you said its not working for you ...
Campbell Ritchie wrote:Please don't use tab characters for indenting, but only spaces.

Sure , I will keep that in mind ...

Peace.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic