Results 1 to 17 of 17
Thread: Help with my code
- 04-09-2011, 09:25 AM #1
Member
- Join Date
- Apr 2011
- Posts
- 27
- Rep Power
- 0
Help with my code
I am a complete new student to java programming, taking the courses in school (online, only option for me :( )
For some reason this assignment is kicking my butt, and I cannot figure out what I am doing wrong. I have 3 books, video tutorials, and googled it every way I know how to ask hehe.
Here is the assignment:
The program should prompt user for a name. (works)
Ask for hours worked. (works)
Ask for hourly wage. (works)
Display employee name and weekly wage. (works)
Should also
Continue to prompt user for a name after one has been figured (doesnt work)
Prompt user to input positive numbers if a negative is entered (doesnt work)
End the program if "stop" is entered (works)
The problem I am running into is that once the user goes through the program it loops back and skips asking for a name again (eliminating a possible stop command in the process), and goes straight to asking for hours. This is what is returned:
Welcome to Payroll manager!
Please enter stop when you are finished
Please enter the Employee's Name:Erik
Now enter the number of hours Erik has worked:10
Now enter Erik's hourly wage as a positive number:$10
Erik will get paid 100.0 this week.
Welcome to Payroll manager!
Please enter stop when you are finished
Please enter the Employee's Name:Now enter the number of hours has worked:
Also if at any time a negative number is entered into a wage or hours field, it just stops, this is returned:
Welcome to Payroll manager!
Please enter stop when you are finished
Please enter the Employee's Name:Erik
Now enter the number of hours Erik has worked:-1
No errors, it just stops.
Netbeans shows no errors, and it compiles with no errors. So I dont know what I am doing wrong.
Here is my code:
Thank you for any help at all!Java Code:package payrollcp;//This is the project for the payroll checkpoint. import java.util.Scanner;//This imports the utility "Scanner" for use by the program. public class Payrollcp { //This is the main class of the program public static void main(String[] args) { //This is the main method Scanner input = new Scanner(System.in); // This tells the program to accept input from the user. String name; // The variable for the Employee's Name double hours; // The variable for amount of hours that the employee works. double wages; // The variable for the amount the employee makes per hour. double weeklypay; // The variable for the weekly pay for the employee. boolean stop = false; while (!stop){ System.out.println("Welcome to Payroll manager!");//A welcome message for the user. System.out.println("Please enter stop when you are finished"); System.out.println(""); //A blank line to seperate from the work area for the user, for aesthetics. System.out.print("Please enter the Employee's Name:");//Prompts user for the employee's name. name = input.nextLine();//Accepts the input of the employee's name. if(name.equals("stop")){ //If statement to end the program System.out.println("Thank you for using payroll manager!");// Ending program line. stop = true;}// stops the program else//Continues on if "stop" is not entered. System.out.print( "Now enter the number of hours " + name + " has worked:");//Prompts user for the number of hours worked. hours = input.nextDouble();//Accepts the input from the user, hours can be in decimal format. while(hours <=0.0)// If hours are negative prompts user to input a positive number. { System.out.print ( "Please enter a positive number" + "Please enter the number of hours " + name + " has worked:");// prompt for positive number. } System.out.print("Now enter " + name + "'s hourly wage as a positive number:$");//Prompts user for the hourly pay of the employee, inserts employees name into the "name" field.} wages = input.nextDouble();//Accepts input from the user, can be in decimal format. while(wages <=0.0)// if wages are negative, prompts user to input a positive number. { System.out.print("Wages must be a positive number, please enter a positive number:$");// prompt for positive number. } weeklypay = hours * wages;//Calculates the weekly pay for the employee, multiplying hours by hourly wage. System.out.println(name + " will get paid " + weeklypay + " this week."); //Displays the employee's name and the final amount they will get paid for the week. System.out.println("");// blank line to break up the while loop }// end while }// end class }// end programLast edited by erikjd21; 04-09-2011 at 09:30 AM. Reason: Was rude, and forgot to thank anyone willing to help :)
-
The most common problem is when you input a number value (Scanner.nextInt(), Scanner.nextDouble() etc) you can't get the next String immediately because there is an extra line break. Take the following input for example:
John 40 1000.00[LINE BREAK]
Doe 30 1500.00[LINE BREAK]
now, calling Scanner.nextLine() would firstly take the whole first line including [LINE BREAK]
calling Scanner.nextLine() again would take the whole second line.
The problem arises when you call Scanner.nextDouble(), take this for example:
40 1000.00[LINE BREAK]
John[LINE BREAK]
Scanner.nextDouble() would take 40
Scanner.nextDouble() again would take 1000.00
then you need to call Scanner.nextLine() to eat the [LINE BREAK]
then again, you can call Scanner.nextLine() to eat the next line
So thats probably why it doesn't work the second time around, the name input part just takes the previous line break, and the double tries to take the String name
if you want to make a negative number always positive, use Math.abs(number).
Math.abs(-10) = 10
Math.abs(10) = 10
Math.abs(-n) = n
Math.abs(n) = n
- 04-09-2011, 09:57 AM #3
Member
- Join Date
- Jan 2011
- Location
- Beirut, Lebanon
- Posts
- 90
- Rep Power
- 0
using while before the condition waged <= 0.0 and hours <= 0.0
will get you into an infinite loop, since it will always type you println and then find again that it is still negative, this is the first thing, another is that the else
you have to rap its code with a { to perform them betterif(name.equals("stop")){ //If statement to end the program
System.out.println("Thank you for using payroll manager!");// Ending program line.
stop = true;}// stops the program
else//Continues on if "stop" is not entered.
- 04-09-2011, 09:59 AM #4
Member
- Join Date
- Apr 2011
- Posts
- 27
- Rep Power
- 0
Thank you for the reply, I will try to figure something out with the scanner input.
As far as the numbers, I am attempting to make the program loop that segment until the user inputs a positive number, not change the number to a positive one.
Does that make sense, or am I wording it wrong?
-
baf, i think the idea IS to get an infinite loop, and then only to stop it if "stop" has been input, so i don't think thats a problem in this case.
although it would've been more ideal to write something like this:
while (true) {
}
to show that you intended an infinite loop.
-
erik, then you need a second while loop like this:
//2nd while loop
int positiveNumber = Scanner.nextInt();
boolean numIsNegative = positiveNumber < 0;
while (numIsNegative) {
//ask again
}
edit:
a better method though, would be to use a DO-WHILE loop instead considering the data must be input once first, so you could use this instead:
Java Code:int positiveNumber; boolean numIsNegative; System.out.println("Enter next integer:"); do { positiveNumber = Scanner.nextInt(); numIsNegative = positiveNumber < 0; if (numIsNegative) { System.out.println("Sorry, positive numbers only."); System.out.println("Try again:"); } } while (numIsNegative);Last edited by ozzyman; 04-09-2011 at 10:09 AM.
- 04-09-2011, 10:04 AM #7
Member
- Join Date
- Jan 2011
- Location
- Beirut, Lebanon
- Posts
- 90
- Rep Power
- 0
yeah i know about the infinite loop for the whole thing, but i meant over here :
if you run the program it will just keep on printing ("Please enter a positive number" + "Please enter the number of hours " + name + " has worked:") and you can't be given a chance to write a positive number, or am i wrong about that ?hours = input.nextDouble();//Accepts the input from the user, hours can be in decimal format.
while(hours <=0.0)// If hours are negative prompts user to input a positive number.
{
System.out.print ( "Please enter a positive number" + "Please enter the number of hours " + name + " has worked:");// prompt for positive number.
}
- 04-09-2011, 10:06 AM #8
Member
- Join Date
- Jan 2011
- Location
- Beirut, Lebanon
- Posts
- 90
- Rep Power
- 0
I run it on my BlueJ and that was what happened, if NetBeans works differently then this is another case, and hope to be acknowledged about that.
-
no you are right, that is an infinite loop, but it should be, but there also should be some clause to get out of it, like the solution i posted for erik before that post.
- 04-09-2011, 10:14 AM #10
Member
- Join Date
- Jan 2011
- Location
- Beirut, Lebanon
- Posts
- 90
- Rep Power
- 0
yeah I just saw it, and understood what you mean by it being an infinite loop, YOu need to be infinite when entering negative numbers but not when printing the command
System.out.println("Sorry, positive numbers only.");
System.out.println("Try again:");
-
those commands are only printed if the number is negative, and its necessary because System.nextInt() will prompt the user for another input, so they need to know that they are expected to input a number for the same item again
- 04-09-2011, 10:23 AM #12
Member
- Join Date
- Apr 2011
- Posts
- 27
- Rep Power
- 0
I changed the while statements to if statements, and although it returns a value now, its not looping correctly, I am missing something.
Also, I have the { braces in the code you quoted, are they misplaced?
Next, looking through all of my books, I only see the nextLine methods for using the Scanner import. What should I use to fix that area of the program.
- 04-09-2011, 10:28 AM #13
Member
- Join Date
- Jan 2011
- Location
- Beirut, Lebanon
- Posts
- 90
- Rep Power
- 0
I don't know exactly about the Netbean but it usually reads one line after the else if you didn't embrace the code after it, you should put braces after the else and till the last thing you want to perform if the name is not stop
-
- 04-09-2011, 10:58 AM #15
Member
- Join Date
- Apr 2011
- Posts
- 27
- Rep Power
- 0
HOLY HELL YOU GUYS FIXED IT
Thank you sooo much!!
If you guys lived anywhere near me, Id buy you all a beer!
Thank you again!
@Ozz
It took some figuring on what I was doing, but I got your solution to work, you really helped me. All of you did. You guys have no idea how greatful I am!
- 04-09-2011, 11:00 AM #16
Member
- Join Date
- Jan 2011
- Location
- Beirut, Lebanon
- Posts
- 90
- Rep Power
- 0
it is our pleasure :) this is what this website is about, help whenever you can :P
- 04-09-2011, 11:11 AM #17
Member
- Join Date
- Apr 2011
- Posts
- 27
- Rep Power
- 0
For anyone who wants to see the end result (I havent commented everything yet, but here is the code of the working product:
Java Code:package payrollcp;//This is the project for the payroll checkpoint. import java.util.Scanner;//This imports the utility "Scanner" for use by the program. public class Payrollcp { //This is the main class of the program public static void main(String[] args) { //This is the main method Scanner input = new Scanner(System.in); // This tells the program to accept input from the user. String name; // The variable for the Employee's Name double hours; // The variable for amount of hours that the employee works. double wages; // The variable for the amount the employee makes per hour. double weeklypay; // The variable for the weekly pay for the employee. boolean stop = false; while (!stop){ System.out.println("Welcome to Payroll manager!");//A welcome message for the user. System.out.println("Please enter stop when you are finished"); System.out.println(""); //A blank line to seperate from the work area for the user, for aesthetics. System.out.print("Please enter the Employee's Name:");//Prompts user for the employee's name. name = input.next();//Accepts the input of the employee's name. System.out.print(""); if (name.equals("stop")) { //If statement to end the program System.out.println("Thank you for using payroll manager!");// Ending program line. stop = true;// stops the program } else//Continues on if "stop" is not entered. System.out.print( "Now enter the number of hours " + name + " has worked:");//Prompts user for the number of hours worked. boolean negHours; do { hours = input.nextDouble(); negHours = hours < 0; if (negHours) { System.out.print("Please enter a positive number:");} } while (negHours); System.out.print("Now enter " + name + "'s hourly wage as a positive number:$");//Prompts user for the hourly pay of the employee, inserts employees name into the "name" field.} boolean negWages; do { wages = input.nextDouble(); negWages = wages < 0; if (negWages) { System.out.print("Please enter a positive number:");} } while (negWages); weeklypay = hours * wages;//Calculates the weekly pay for the employee, multiplying hours by hourly wage. System.out.println(name + " will get paid " + weeklypay + " this week."); //Displays the employee's name and the final amount they will get paid for the week. System.out.println("");// blank line to break up the while loop }// end while }// end class // end program }
Similar Threads
-
C server code - Java CLient Code _ TCP Connection Problem
By rmd22 in forum NetworkingReplies: 0Last Post: 02-21-2011, 11:50 AM -
Code to check if a piece of code is legal.
By vahshir in forum New To JavaReplies: 3Last Post: 08-30-2010, 04:21 AM -
can any one pls send me a sample code for calling a jsp code in swings
By sniffer139 in forum AWT / SwingReplies: 1Last Post: 03-04-2010, 11:19 AM -
Convert java code to midlet code
By coldvoice05 in forum Advanced JavaReplies: 1Last Post: 08-09-2009, 01:21 PM -
Generating Code Automatically Using Custom code Template In Eclipse
By JavaForums in forum EclipseReplies: 1Last Post: 04-26-2007, 03:52 PM


LinkBack URL
About LinkBacks


Bookmarks