For Programmers: Free Programming Magazines  


Home > Archive > Java Help > April 2004 > Help with user input and math functions.









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Help with user input and math functions.
Bezul

2004-04-29, 3:33 am

I am having problems with a couple novice programs I have written. They
will accept user input and assign the value to a variable, but something
crazy happens once I try to perform math functions between the variables. I
end up with a very large, inflated number... and am doing it how my school
book tells it to.

I am unsure if its somethin in the book that isnt explained thoroughly or
some sort of bug in the j2se's from 1.4x to the 1.5 beta.

Here is the code.. (somewhat hacked up from several hours of trying to get
it to work.).

public class Payroll2
{
public static void main(String[] args) throws Exception
{
//Prompt for hourly pay rate.
System.out.println("What is the employee's hourly pay rate?");
double hPay = (double)System.in.read();System.in.read();
//Prompt for hours worked.
System.out.println("How many hours has this employee worked?");
double hours = (double)System.in.read();System.in.read();System.in.read();
//Calculate & Display Gross Pay.
double gPay = (hPay * hours);
System.out.println("The employee's Gross Pay is $" +gPay+ ".");
//Calculate & Display Withholding Tax.
double wTot = 0.0;
if ((gPay >= 0.00) && (gPay <= 300.00))
{
wTot = (gPay * .10);
System.out.println("The employee's Withholding is $" +wTot+ ".");
}
else if ((gPay >= 300.01) && (gPay <= 400.00))
{
wTot = (gPay * .12);
System.out.println("The employee's Withholding is $" +wTot+ ".");
}
else if ((gPay >= 400.01) && (gPay <= 500.00))
{
wTot = (gPay * .15);
System.out.println("The employee's Withholding is $" +wTot+ ".");
}
else if (gPay >= 500.01)
{
wTot = (gPay * .20);
System.out.println("The employee's Withholding is $" +wTot+ ".");
}
else
System.out.println("An error has occured, program terminating.");
System.exit(0);
//Calculate & Display Net Pay.
double nPay = (gPay - wTot);
System.out.println("The employee's Net Pay is $" +nPay+ ".");
}
}


Thanx for the help.. in advance.



Roedy Green

2004-04-29, 3:33 am

On Thu, 29 Apr 2004 00:03:55 -0500, "Bezul" <bezul@highstream.net>
wrote or quoted :

> double hPay = (double)System.in.read();System.in.read();


Java is not that smart. This isn't some advanced language like
FORTRAN or PL/I.

All you can read in Java are strings. If you want to read anything
else, they have to be in internal binary format -- something you can't
create with a text editor.

You have to read strings and then convert them into doubles etc.

See http://mindprod.com/fileio.html

see http://mindprod.com/converter.html
--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
Bezul

2004-04-29, 3:33 am


"Roedy Green" <see@mindprod.com.invalid> wrote in message
news:jr3190dth0n2eefvdvrikaah9o46d9jbk6@
4ax.com...

> All you can read in Java are strings. If you want to read anything
> else, they have to be in internal binary format -- something you can't
> create with a text editor.
>
> You have to read strings and then convert them into doubles etc.


Ahhh, well thats nice. The book specifically says System.in.read() accepts
a byte and return an integer.. then all that is needed is a cast to convert
it. The strings conversions are not even covered until a few more chapters.
I think its time for the college to use decent books.


Roedy Green

2004-04-29, 3:33 am

On Thu, 29 Apr 2004 00:44:42 -0500, "Bezul" <bezul@highstream.net>
wrote or quoted :

>Ahhh, well thats nice. The book specifically says System.in.read() accepts
>a byte and return an integer.. then all that is needed is a cast to convert
>it. The strings conversions are not even covered until a few more chapters.
>I think its time for the college to use decent books.



System.int.read() reads one BYTE e.g. the character '0' which is hex
30 as an integer. Not very useful for newbie data entry.

System.int.read() does not take human readable strings like "123" and
convert them to a binary ints. Java is extremely primitive in that
department. It has no input or output formatting. It is the most
primitive language I have ever used in that regard.

However, you can roll your own formatting methods any way you want.

What you want for newbie work in System.in.readln()
which reads a whole line as a string. If you put one field per line,
you can use the conversion functions without further parsing to break
it up into fields. I gave you the urls last post.

Java seems to want to scare newbies off by not having a simple CSV
input scheme, something like GET LIST in PL/1.


--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com