LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Java - System.in.read() not giving correct value. (https://www.linuxquestions.org/questions/programming-9/java-system-in-read-not-giving-correct-value-92851/)

Jose Muņiz 09-14-2003 09:40 PM

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.*;


public class ExtraTime
{
        public static void main(String args[]) throws IOException
        {
                double salary = 0,
                      total = 0; 
                int hours = 0;
 
                while (hours != -1)
                {       
                        salary = 0;

                        System.out.print("Insert hours you worked <-1 to finish>: ");
                        System.out.flush();       

       
                        hours = System.in.read();
                        System.in.skip(1);

                        System.out.println("Hours = " + hours);
       
                        if (hours == -1)
                                break;

                        System.out.print("Insert your salary ($00.00): ");
                        System.out.flush();
       
                        salary = System.in.read();
                        System.in.skip(1);

                        System.out.println("Salary = " + salary);
       

                        if (hours > 0 && hours <= 24)
                                total = salary * hours;
                        else if (hours > 24)       
                                total = ( (hours - 24) * (salary * 1.5)  ) + salary * 24;
 
                        System.out.println("Your total earnings for this month are: " + total);
                       
                }

        }
}

And here is what I get as a result:
Quote:

[joseamuniz@localhost Java]$ javac ExtraTime.java
[joseamuniz@localhost Java]$ java ExtraTime
Insert hours you worked <-1 to finish>: -1
Hours = 45
Insert your salary ($00.00): 1
Salary = 10.0
Your total earnings for this month are: 555.0
Insert hours you worked <-1 to finish>: 1
Hours = 10
Insert your salary ($00.00): 1
Salary = 10.0
Your total earnings for this month are: 100.0
I've tried to search for what I?m doing wrong in my book and on the Internet but I have no idea. Could you please tell me at least where the problem comes from? Thank you very much for reading this.

SaTaN 09-14-2003 10:40 PM

Code:

import java.io.*;
public class ExtraTime
{
        public static void main(String args[]) throws IOException
        {
                double salary = 0,
                      total = 0;
                      int hours = 0;

                      String s;
                        BufferedReader stdin =new BufferedReader(new InputStreamReader(System.in));

                      while (hours != -1)
                      {
                              salary = 0;
                              System.out.print("Insert hours you worked <-1 to finish>: ");
                              System.out.flush();



                                s=stdin.readLine();
                                hours=Integer.parseInt(s);

                              //System.in.skip(1);


                              System.out.println("Hours = " + hours);

                              if (hours == -1)
                                      break;
                              System.out.print("Insert your salary ($00.00): ");
                              System.out.flush();

                              s=stdin.readLine();
                              salary=Integer.parseInt(s);

                              //System.in.skip(1);
                            //Don't know what skip does but didn't need it
                              System.out.println("Salary = " + salary);

                              if (hours > 0 && hours <= 24)
                                      total = salary * hours;
                              else if (hours > 24)
                                      total = ( (hours - 24) * (salary * 1.5)  ) + salary * 24;

                              System.out.println("Your total earnings for this month are: " + total);

                      }
        }
}



I have used a different method to take input from the user , which I think is a better way to do it.

Jose Muņiz 09-14-2003 10:41 PM

However, I would appreciate if I could actually know what's wrong with my code.

SaTaN 09-15-2003 01:49 AM

Re: Java - System.in.read() not giving correct value.
 
Quote:

Originally posted by Jose Muņiz


Code:

import java.io.*;
public class ExtraTime
{
        public static void main(String args[]) throws IOException
        { 
                int hours = 0;
                System.out.print("Insert hours you worked <-1 to finish>: ");
                System.out.flush();       
                hours = System.in.read();
                System.in.skip(1);
                System.out.println("Hours = " + hours);
        }
}


Let us take this part of this code....

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....

Jose Muņiz 09-15-2003 09:27 AM

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.