Java - System.in.read() not giving correct value.
Well.. I've started to learn Java. I want to create a simple program that will calculate how much someone earns based on how much he earns per hour and how many hours he worked. If the guy worked more than 24 hours, then those hours will be worth 1.5 his hourly salary.
However, for some strange reason, the variable "hours" gets other values than what I want!! Here is the source code: Code:
import java.io.*; Quote:
|
Code:
import java.io.*; I have used a different method to take input from the user , which I think is a better way to do it. |
However, I would appreciate if I could actually know what's wrong with my code.
|
Re: Java - System.in.read() not giving correct value.
Quote:
What happens with system.in.read is Say you take input as "a" then,it takes input as 97 which is the int value of a Similarly when you give input as "12" . It s taking only 1 as input and outputting 49 which happens to be the int value of char 1 If you know C, I can show you a similar thing... Suppose int ch; When you try to get it using getchar() , you don't get the entire number say "123" only one character will be accepted . Here it s 1 and so, 49 will be outputted. Suppose "23456" then the value of 2 i.e,50 will be outputted Summary :- What I would like to say is that same as getchar() cannot be used to accept integers in C , In Java system.in.read should not be used to accept integers. It can be used to get characters one - by - one USE READLINE INSTEAD.... |
Oh! I get it. Thank you very much for your time. I understood now. Thanks a lot for your attention :D
|
All times are GMT -5. The time now is 06:47 AM. |