This question already has answers here:
integer value read from System.in is not the value typed
(5 answers)
Closed 4 years ago.
I'm trying to enter integer value through System.in.read().
But when I'm reading the value it is giving different output 49 for 1, 50 for 2 & so on int e=(int)System.in.read();
System.out.print("\n"+e);
Because character 1 (i.e. char ch = '1') has ASCII code 49 (i.e. int code = '1' is 49).
System.out.println((int)'1'); // 49
To fix your examle, just substract code for 0:
int e = System.in.read() - '0';
System.out.println(e); // 1
You're reading char with function
System.in.read()
You can see the use of System.in.read() here. Also please check here how to read a value from user.
As other answers have mentioned, System.in.read() reads in a single character in the form of an int, or -1 if there is no input to read in. This means that characters read in with System.in.read() will be ints representing ASCII values of the read characters.
To read in an integer from System.in it might be easier to use a Scanner:
Scanner s = new Scanner(System.in);
int e = s.nextInt();
System.out.print("\n"+e);
s.close();
or, if you wish to stick to using System.in.read(), you can use Integer.parseInt(String) to obtain an integer off of the character input from System.in.read():
int e = Integer.parseInt("" + (char) System.in.read());
System.out.print("\n"+e);
Integer.parseInt(String) will throw a NumberFormatException you can catch if the input is not a number.
Related
This question already has answers here:
Convert int to char in java
(18 answers)
Closed 1 year ago.
So what i understand is that if for example i have an int a = 49 and want to find out the ASCII equivalent to it, all i need to do is:
int a = 49;
System.out.println((char)a);
which has the Output: 1.
But how can i do this reversed? So lets say i have int a = 1 and i want the output to be 49?
I have already tried stuff like:
int a = 1;
System.out.println ((char)a);
But the output here is "." instead of 49.
This:
int a = 49;
System.out.println((char)a);
gives you the char whose internal value is 49, which is to say the unicode character '1'. There is no numerical conversion going on, it's just two ways to look at the same number.
In this:
int a = 1;
System.out.println((char)a);
you get the char whose internal value is 1, which is a unicode control character (ctrl/A, which probably won't be printed in any useful way). There is nothing there that can magically come up with the value 49.
If you had the character '1' then it's easy:
int a = '1';
System.out.println(a);
but this is practically the same as your first case; '1' and 49 are the same value.
I have this code:
public class test {
public static void main(String args[]) throws IOException {
BufferedReader in = new BufferedReader(new
InputStreamReader(System.in));
char x =(char)in.read();
char y =(char)in.read();
char z =(char)in.read();
System.out.print(x+y+z);
}
}
and this input:
1
2
and the output is:
109
Why do I get this output?
I can't understand how read function works.
I tried using the skip function and didn't get the right answer either.
You are reading your input as characters. Your input is three characters (1, 2, and line feed):
1 with an ASCII value of 49.
2 with an ASCII value of 50.
line feed with an ASCII value of 10.
Then you add those three chars by their ASCII value, giving a total of 109.
The problem is that you've misunderstood how the character is being returned when read() is called.
The character read, as an integer in the range 0 to 65535 (0x00-0xffff), or -1 if the end of the stream has been reached
The read method returns an int so it can return the Unicode code for the character. For simple letters and numbers, Unicode overlaps ASCII, where 1 is 49, 2 is 50, and a newline character is 10. The sum of those codes is 109.
Options:
Use a Scanner and its nextInt method.
Use BufferedReader's readLine method and parse the strings to integers with Integer.parseInt.
What does :
System.in.read()
return ? The documentation says :
Returns:
the next byte of data, or -1 if the end of the stream is reached.
But for example if I enter : 10 I get back 49 . Why is that ?
49 is the ASCII value of the char 1. It is the value of the first byte.
The stream of bytes that is produced when you enter 10Enter on your console or terminal contains the three bytes {49,48,10} (on my Mac, may end with 10,12 or 12 instead of 10, depending on your System).
So the output of the simple snippet
int b = System.in.read();
while (b != -1) {
System.out.println(b);
b = System.in.read();
}
after entering a 10 and hitting enter, is (on my machine)
49
48
10
System.in.read() reads just one byte.
49 is the Unicode point value for 1.
Try to print:
System.out.println((char)49);
This will help you to understand it more.
When you enter 10, it is not read as an integer but as a String or, more precisely here, an array of bytes.
49 is the ASCII code for the character 1.
This question already has answers here:
Get ASCII value at input word
(6 answers)
Closed 8 years ago.
I have a loop with i incrementing through a string, and I want to print that character, but its ASCII code.
myString = "90210";
for (int i = 0; i < myString.length(); i++) {
System.out.println(myString.charAt(i));
}
To output:
57
48
50
49
48
Obviously charAt() doesn't do this, but I need another method to append to it to get my desired result.
Just cast to an int so that you get the numerical value of the char, which consequently uses the overloaded println(int) method
System.out.println((int)myString.charAt(i));
System.out.println(myString.charAt(i) - '\0');
System.out.println(myString.charAt(i) - 0);
I have a piece of code, that receives values from a sensor (Via serialport using rxtx) and displays it. Strangely, the following code
int value = in.read();
System.out.print((char) value);
Outputs the desired value as:
RXTX Warning: Removing stale lock file. /var/lock/LK.005.018.009
20
27
29
26
21
But when I change the above code as following:
int value = in.read();
System.out.print("The value is"+(char) value);
The output becomes:
RXTX Warning: Removing stale lock file. /var/lock/LK.005.018.009
The value is2The value is6The value is
The value is2The value is2The value is
As it can be seen, the integer splits. For quite a while, I am unable to figure it out?
Is there a way where I can save the console value into an integer, as I would be using this value in the future.
As it can be seen, the integer splits. For quite a while, I am unable to figure it out?
You are not reading integers, you are reading bytes which have characters encoded as ?ASCII?
Is there a way where I can save the console value into an integer, as I would be using this value in the future?
The simplest way is to use a Scanner
Scanner scanner = new Scanner(in);
while (scanner.hasNextInt()) {
// read bytes up the next whitespace, parse as a int
int n = scanner.nextInt();
Don't cast it to char. Just print
System.out.print("The value is " + value);
In second example you are converting int to char, but when you concatenate it to string you don't see anything because these are non printable characters.
Your code converted the value number into a character, using Java's UCS2 representation. You may need it only if you want to treat the value as a character data (e.g. String).
If you need it only as an integer value (e.g. for printing), you don't need to convert.
please check this snippet by adopting to your code
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = br.readLine();
String[] words = line.split("[ ]",0); //white space delimiter
for(int i = 0; i < words.length; i++) {
System.out.print("The value is " +words[i]);
System.out.print("\n");