File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Taking input from keyboard: takes ints, but not string Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Taking input from keyboard: takes ints, but not string" Watch "Taking input from keyboard: takes ints, but not string" New topic
Author

Taking input from keyboard: takes ints, but not string

Rahul Sudip Bose
Ranch Hand

Joined: Jan 21, 2011
Posts: 637

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 :


SCJP 6. Learning more now.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19790
    
  20

The nextLine() reads the enter that you entered for entering the first number. nextInt() only takes the 1111, not the enter after that.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Rahul Sudip Bose
Ranch Hand

Joined: Jan 21, 2011
Posts: 637

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

Joined: Oct 17, 2010
Posts: 82

You should use "next()" method instead of "nextLine()" as following

this method will take the data correctly.


I Hear I Forget, I See I Remember, I Do I Understand.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4490
    
    8

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

Joined: Jan 21, 2011
Posts: 637

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

Joined: Nov 02, 2007
Posts: 87



Rahul Sudip Bose
Ranch Hand

Joined: Jan 21, 2011
Posts: 637

prem pillai wrote:



Can you please tell me what the above code is doing ? I have never used delimiter(), useDelimiter() etc ?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
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

Joined: Nov 02, 2007
Posts: 87

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

Joined: Nov 02, 2007
Posts: 87

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

Joined: Oct 13, 2005
Posts: 40052
    
  28
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

Joined: Jan 21, 2011
Posts: 637

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

Joined: Jan 21, 2011
Posts: 637

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

Joined: Nov 02, 2007
Posts: 87

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

Joined: Oct 13, 2005
Posts: 40052
    
  28
Demonstration of a poorly-documented pitfall with Scanner here.
prem pillai
Ranch Hand

Joined: Nov 02, 2007
Posts: 87

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

Joined: Oct 13, 2005
Posts: 40052
    
  28
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

Joined: Nov 02, 2007
Posts: 87

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Taking input from keyboard: takes ints, but not string