This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes System.in.read giving a number when no entry has been made Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "System.in.read giving a number when no entry has been made" Watch "System.in.read giving a number when no entry has been made" New topic
Author

System.in.read giving a number when no entry has been made

Tyler Knecht
Greenhorn

Joined: Jul 21, 2008
Posts: 4
This is just the start of my program, I am curios as to why when I run the program and just press enter, the result I get is "You typed: 13" when the entry is blank. It's baffling me.

import java.io.*;
public class birthYear
{
public static void main(String[] args) throws java.io.IOException
{
System.out.println("Please type your birth year.");
int x = (int)System.in.read();

System.out.print("You typed: " + x);

}
}
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3168
    
  10
When you hit return either a CR or CR/LF is generated (depending on your OS). System.in.read() reads the next character from the input and you then cast it to an int which will give you the ASCII value of the character. The ASCII value of a CR is 13.


Joanne
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: System.in.read giving a number when no entry has been made
 
Similar Threads
Input Output If
reading from keyBoard
Break back to Topic Menu
Exeption Handling
Accepting Numeric User Input