• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Help with Java programming!

jamesbond007

Diamond Member
Hey guys! I need some help with my code. I want to make this program fool-proof by providing if statements to help with program faults, user input errors, etc.

Here's what I have so far:
import java.util.Scanner;

public class ITSwages
{
public static void main(String[]args)
{
Scanner inputRate = new Scanner(System.in);
System.out.print("Please enter your hourly wage, excluding a dollar sign($): ");
double rate = inputRate.nextDouble();

Scanner inputHours = new Scanner(System.in);
System.out.print("Please enter the number of hours worked: ");
double hours = inputHours.nextDouble();
if (hours > 40.000) {
hours = (1.5*rate)*(hours - 40.000)+(40 * rate);
} else if (hours <= 40.000) {
hours = rate*hours;
} else if (hours < 0) {
// --> hours =
System.out.printf("Total Salary : $%5.2f" , hours);
System.out.println("");
}
}

The line with the comment on there next to hours is where I need the help...I need to assign something or create a variable/object telling the program that if the hours are less than 0 (such as a negative integer), the program will detect the input error and cause it to say that there was an invalid input dected.

Please help or give any links or any thoughts of possible fixes...I'm stuck right now as this thing works fine and compiles, but I need help with that input. I'll be re-creating the if statements for the wage input as well, in case the user inputs a negative integer in there as well.

Thanks in advance!
~Travis W.
 
double hours;
hours = inputHours.nextDouble();
while (hours < 0) {
System.out.println("You must input a POSITIVE amount of hours unless you want to OWE the company money you moron!");
hours = inputHours.nextDouble();
}
 
Crusty, thanks! We haven't learned the 'while' expression yet...must be why this assignment is extra credit! 😛 (yes, total newb java class)

Do you see anything wrong with my statement that is calculating the 1.5x payrate when they work above 40 hours? I am testing my program now and after inputting the hours worked, it just exits the program (press any key to continue)

EDIT: Nevermind, seems to be working fine so far...I'm having troubles entering the WHILE section you wrote above into my program, though.
 
Yes. It caculates your number just fine, but then you're not printing it out 😛

Another thing to consider: what happens when the user doesn't enter a valid double? (Hint: your program will 'blow up' 😉)

One other thing. I don't see why you need to create a second scanner. Just give the first one a more generic name and use it for all input.
 
Originally posted by: kamper
Another thing to consider: what happens when the user doesn't enter a valid double? (Hint: your program will 'blow up' 😉)

One other thing. I don't see why you need to create a second scanner. Just give the first one a more generic name and use it for all input.

I thought I had to create a new scanner each time I needed to query user input...I'm guessing I'm wrong? 😛

I have no clue how to prevent against an invalid double value. (ie: text/String input) Got any hints or statements I can research? This is a little more advanced than what we've covered so far. We haven't done if/else/while statements in class yet and we've only begun to learn the Scanner utility.

That's why this is extra credit, but for the love of God, I have no idea what methods to really look up! 🙂

EDIT: After some playing around, I figured out how to use the WHILE statements Crusty wrote above! Thanks guys for the input thus far. I just need to figure out how to prevent my program from crashing when String values or null values are entered into the program.
 
First of all, take a look at the javadoc page for Scanner: http://java.sun.com/j2se/1.5.0/docs/api/index.html?java/util/Scanner.html

Check out the method hasNextDouble(). This should prompt the user for input and then tell you if what they entered is a double or not. Then you can call nextDouble() as you were if it's fine or print an error message if it's not. See if you can work in your newly learned while loop to keep prompting them until they enter something valid. But there's a trick there: if the input was invalid and you don't call nextDouble() the input is still on the "input stream" so you have to figure out how to move it off before asking them to try again.

The other method of doing this means not checking hasNextDouble() but going ahead and calling nextDouble(). Then, if bad input is entered, you get that 'blow up', but there's a way to 'catch' the error before it screws you up. It's called catching exceptions, but I'd suggest not tackling that quite yet. Your teacher will probably (hopefully) run you through the wonderful world of exceptions in a few weeks/months.
 
Back
Top