LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-14-2003, 09:40 PM   #1
Jose Muņiz
Member
 
Registered: Jul 2003
Location: Mexico City
Distribution: Slackware 9.1, SuSE 9.1
Posts: 248

Rep: Reputation: 32
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.
 
Old 09-14-2003, 10:40 PM   #2
SaTaN
Member
 
Registered: Aug 2003
Location: Suprisingly in Heaven
Posts: 223

Rep: Reputation: 33
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.
 
Old 09-14-2003, 10:41 PM   #3
Jose Muņiz
Member
 
Registered: Jul 2003
Location: Mexico City
Distribution: Slackware 9.1, SuSE 9.1
Posts: 248

Original Poster
Rep: Reputation: 32
However, I would appreciate if I could actually know what's wrong with my code.
 
Old 09-15-2003, 01:49 AM   #4
SaTaN
Member
 
Registered: Aug 2003
Location: Suprisingly in Heaven
Posts: 223

Rep: Reputation: 33
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....

Last edited by SaTaN; 09-15-2003 at 02:00 AM.
 
Old 09-15-2003, 09:27 AM   #5
Jose Muņiz
Member
 
Registered: Jul 2003
Location: Mexico City
Distribution: Slackware 9.1, SuSE 9.1
Posts: 248

Original Poster
Rep: Reputation: 32
Oh! I get it. Thank you very much for your time. I understood now. Thanks a lot for your attention
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
your-freedom.net software giving errors with java learner@new Linux - Software 2 01-10-2006 01:26 AM
giving permission to read windows partitions jaakkop Slackware 2 01-13-2005 08:51 AM
fstab: giving a user acct read-only access to specific partition doorbits Linux - General 4 05-18-2004 02:11 PM
giving apache the correct permissions dflorence Linux - Newbie 3 11-06-2003 08:27 AM
Is giving away a distro illegal ?? Read this. trickykid Linux - General 5 04-07-2002 08:29 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:02 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration