The Artima Developer Community
Sponsored Link

Java Answers Forum
Java Scanning

9 replies on 1 page. Most recent reply: Mar 13, 2009 6:42 PM by Manny Grewal

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 9 replies on 1 page
Manny Grewal

Posts: 5
Nickname: mannyg
Registered: Mar, 2009

Java Scanning Posted: Mar 1, 2009 8:06 AM
Reply to this message Reply
Advertisement
Hey I am trying to create a program for class that asks the user to input a hourly wage, and too keep it reasonable if it exceeds a certain number to ask for them to input another valid hourly wage. So my problem lies within the scanning for another value, here is my code.

import java.util.Scanner;
class PayTime
{
public static void main(String[] args)
{
Scanner pay = new Scanner (System.in);

double wage=0;

System.out.print("Enter Hourly Wage: ");
wage=pay.nextDouble();
if (wage>100){
for (wage=0;wage>=100 ;wage++ ){
System.out.print("Enter a valid wage: ");
wage=pay.nextDouble();
}
}


}
}


The reason I used a if statement was first to determine if the wage is greater then 100 then enter the loop where it can scan for a valid wage over and over again. However the way I have it set up, it doesn't work. I want it to be that if the wage is greater then 100 it will continue to scan for another number. However when I change it too wage<100 it scans for the wage over and over until the wage is over 100 which I would like the exact opposite off. Any help would be appreciated!

-Manny


Vincent O'Sullivan

Posts: 724
Nickname: vincent
Registered: Nov, 2002

Re: Java Scanning Posted: Mar 2, 2009 3:43 AM
Reply to this message Reply
> Hey I am trying to create a program for class that asks
> the user to input a hourly wage, and too keep it
> reasonable if it exceeds a certain number to ask for them
> to input another valid hourly wage. So my problem lies
> within the scanning for another value, here is my code.
>
> import java.util.Scanner;
> class PayTime
> {
> public static void main(String[] args)
> {
> Scanner pay = new Scanner (System.in);
>
> double wage=0;
>
> System.out.print("Enter Hourly Wage: ");
> wage=pay.nextDouble();
> if (wage>100){
> for (wage=0;wage>=100 ;wage++ ){
> System.out.print("Enter a valid wage: ");
> wage=pay.nextDouble();
> }
> }
>
>
> }
> }
>
>
> The reason I used a if statement was first to determine if
> the wage is greater then 100 then enter the loop where it
> can scan for a valid wage over and over again. However the
> way I have it set up, it doesn't work. I want it to be
> that if the wage is greater then 100 it will continue to
> scan for another number. However when I change it too
> wage<100 it scans for the wage over and over until the
> wage is over 100 which I would like the exact opposite
> off. Any help would be appreciated!
>
> -Manny

You are using the variable wage to store the user entered value and also using it as a loop counter in the for loop. When you enter to loop, wage is set to zero regardless of what the user entered. The loop counter needs to be a separate variable. But even then the code is too complicated. The following (untested) code might do what you want:
while( wage > 100)
{
    System.out.print("Enter a valid wage: ");
    wage=pay.nextDouble();
}

Manny Grewal

Posts: 5
Nickname: mannyg
Registered: Mar, 2009

Re: Java Scanning Posted: Mar 2, 2009 7:12 PM
Reply to this message Reply
Okay, yeah I did the while loop before I checked this thread, actually about 15 minutes before this thread, but I ran into another problem. I am going to paste just the variables I have declared and show (in comments) where the error occurs.

double wage=0;
double hours=0;
double overTime=hours-40;
double grossPay=hours*wage;
double netPay=(grossPay-incomeTaxOverall);
/*I get an error here saying
symbol: variable incomeTaxOverall
location: class PayTime
double netPay=(grossPay-incomeTaxOverall);
^
*/
double overHours=hours+overTime;
double incomeTax=0;
double incomeTaxOverTime=overTime*.25;
double incomeTaxOverall=incomeTax+incomeTaxOverTime;
double overTimePay=overTime*1.5;
int employeeNumber =0;



I also get an error using a switch statement I have in place, here it is:

switch (incomeTax)
{
case 1: if (grossPay>=0 || grossPay<=300)
incomeTax=.10;
break;
case 2: if (grossPay>=300.01 || grossPay<=400)
incomeTax=.12;
break;
case 3: if (grossPay>=400.01 || grossPay<=500)
incomeTax=.15;
break;
case4: if (grossPay>=500.01)
incomeTax=.20;
break;

The error for the switch statement is
found: double
required: int
switch (incomeTax)
^

Vincent O'Sullivan

Posts: 724
Nickname: vincent
Registered: Nov, 2002

Re: Java Scanning Posted: Mar 3, 2009 1:22 AM
Reply to this message Reply
These are pretty basic errors:

> /*I get an error here saying
> symbol: variable incomeTaxOverall
> location: class PayTime
> double netPay=(grossPay-incomeTaxOverall);
> ^
> */
> :
> double incomeTaxOverall=incomeTax+incomeTaxOverTime;

You're using the variable incomeTaxOverall before you create it.

> switch (incomeTax)
> {
> case 1: if (grossPay>=0 || grossPay<=300)
> incomeTax=.10;
> break;
> case 2: if (grossPay>=300.01 || grossPay<=400)
> incomeTax=.12;
> break;
> case 3: if (grossPay>=400.01 || grossPay<=500)
> incomeTax=.15;
> break;
> case4: if (grossPay>=500.01)
> incomeTax=.20;
> break;
>
> The error for the switch statement is
> found: double
> required: int
> switch (incomeTax)
> ^

You need to take another look at how the Java switch statement works and not just glance at an example and guess.
1) It only takes integers as the switch parameter (you've supplied a double).
2) case 1, case 2, case 3, etc. are not indexed labels. They are the cases executed if incomeTax = 1, 2, 3, etc.
3) It won't do what you're trying to here do anyway.

You could try replacing the switch statement with a set of if..else if.. statements.

Manny Grewal

Posts: 5
Nickname: mannyg
Registered: Mar, 2009

Re: Java Scanning Posted: Mar 3, 2009 3:40 PM
Reply to this message Reply
I see, well thanks for that, I fixed those errors. Actually I am just learning from slides, so I don't quite know it well enough, thanks for letting me know. So I have completed the assignment but now I recieve a logical error. I believe I know what the problem is, but I do not know how to solve it. I will post my whole program so you can understand it, I also added comments explaining what some things do (as pertained in the assignment).

import java.util.Scanner; // Importing Scanner
class PayTime // Naming the class PayTime
{
public static void main(String[] args)
{
Scanner pay = new Scanner (System.in); //Creating class for Scanner, naming it "pay"
int employeeNumber =0;
double wage=0;
double hours=0;
double overTime=hours-40;
double grossPay=hours*wage;
double incomeTax=0;
double incomeTaxOverTime=overTime*.25;
double incomeTaxOverall=incomeTax+incomeTaxOverTime;
double netPay=(grossPay-incomeTaxOverall);
double overHours=hours+overTime;
double overTimePay=overTime*1.5;


System.out.print("Enter Employee Number: ");//Prompting Employees Number
employeeNumber=pay.nextInt(); // Declared variable for Employees Number, and saving it
System.out.print("Enter Employee Name: ");//Prompting Employees Name
String name=pay.next(); // Declare String "name" storing Employees Name here
System.out.print("Enter Hours Worked: ");//Prompting for Employees hours worked
hours=pay.nextDouble();//Declaring variable for hours to be used, and saving it

System.out.print("Enter Hourly Wage: $");//Prompting Employee for hourly wage
wage=pay.nextDouble();//Declaring variable wage, and saving input given by user

while (wage>100)//If the hourly wage is above 100 dollars, then a invalid message is shown
{
System.out.print("Invalid, Please enter a valid hourly wage: ");//Prompting for a realistic value for the hourly wage
wage=pay.nextDouble();//scanning/saving the new wage entered until it meets condition
}
if (grossPay>=0 || grossPay<=300)
incomeTax=.10;
else if (grossPay>=300.01 || grossPay<=400)
incomeTax=.12;
else if (grossPay>=400.01 || grossPay<=500)
incomeTax=.15;
else incomeTax=.20;


if (hours>40){


System.out.println("Employee Name: "+name);
System.out.println("Regular Pay: "+grossPay);
System.out.println("Overtime Pay: "+overTimePay);
System.out.println("Regular Income Taxes: "+incomeTax);
System.out.println("Overtime Income Taxes: "+incomeTaxOverTime);
System.out.println("Net Pay: "+netPay);

else
System.out.println("Employee Name: "+name);
System.out.println("Gross Pay: "+grossPay);
System.out.println("Income Taxes: "+incomeTax);
System.out.println("Net Pay: "+netPay);
}


}
}

I believe the problem lies with the if statement at the end, where I do the final output, but the if statement only accepts the first condition after, it, everything else is just written. I dont know how to do it properly, I recieve a logical error. The values i use are this:
Number: 12345
Name: Manny
Hours worked: 50
Hourly wage: $10

and the output is:
Name: Manny
Regular Pay: 0.0
Overtime Pay: -60
Regular Income Taxes: 0.1
Overtime income taxes: -10.0
Net Pay: 10.00
Gross Pay: 0.0
Income taxes: 0.1
Net Pay: 10.0

it's a logical error, and it also displays the else part automatically, I do apologize if for you this is basic, but I am only a month into learning Java, doing it twice a week, I do find some things difficult to fix. Thanks for any help

Vincent O'Sullivan

Posts: 724
Nickname: vincent
Registered: Nov, 2002

Re: Java Scanning Posted: Mar 4, 2009 1:18 AM
Reply to this message Reply
> Actually I am just learning from slides, so I don't quite
> know it well enough, thanks for letting me know.

That's a difficult way to learn. You are doing very well.

You appear to have missed out some brackets. The format of the if statement is:

if (condition)
{
// System.out. etc.
}
else
{
// System.out. etc.
}

Manny Grewal

Posts: 5
Nickname: mannyg
Registered: Mar, 2009

Re: Java Scanning Posted: Mar 4, 2009 7:15 PM
Reply to this message Reply
Awesome thanks! That helped with the output so it stays true too the condition, but I still receive the logical error with the values I gave you all, and the logical error output I receive. Any ideas :S?

Vincent O'Sullivan

Posts: 724
Nickname: vincent
Registered: Nov, 2002

Re: Java Scanning Posted: Mar 6, 2009 4:43 AM
Reply to this message Reply
You're calculating the answers before asking the questions.

Elizabeth Wiethoff

Posts: 89
Nickname: ewiethoff
Registered: Mar, 2005

Re: Java Scanning Posted: Mar 11, 2009 7:58 AM
Reply to this message Reply
>> Actually I am just learning from slides, so I don't quite
>> know it well enough, thanks for letting me know.

> That's a difficult way to learn. You are doing very well.

Yes, it's a very difficult way to learn. If I were you, I'd be tearing my hair out.

I find books and good tutorials to be most helpful when learning a programming language. I suggest borrowing Java in a Nutshell or Learning Java from the library and reading the first few chapters.

The online Java tutorial from Sun is also good: http://java.sun.com/docs/books/tutorial/ These pages teach the control flow statements: http://java.sun.com/docs/books/tutorial/java/nutsandbolts/flow.html

Manny Grewal

Posts: 5
Nickname: mannyg
Registered: Mar, 2009

Re: Java Scanning Posted: Mar 13, 2009 6:42 PM
Reply to this message Reply
Thanks, I will look at those, actually I have a Object Oriented Programming class (Java), but I been sick for the past three weeks, and i got a cast, so im learning from slides at home. I completed the assignment (Reason I started the thread) once I sat down and started thinking logically. I realized my mistake was I looked at the requirements and just went through it one by one, but their was no logical thought process, so I did the program in 2 hours, then I re-did it two weeks later (today) in 12 minutes just know how to do it correctly :)

Flat View: This topic has 9 replies on 1 page
Topic: data structures in java stacks and queue plz help Previous Topic   Next Topic Topic: JAX India 2009: International Conference on Java Technologies April 06-10,

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use