13 Replies - 530 Views - Last Post: 15 August 2011 - 11:45 AM Rate Topic: -----

#1 Irish_hammer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 07-August 11

Program will not end when stop is inputed

Posted 14 August 2011 - 10:15 PM

This is my code so far and it compiles, runs, and shows no errors. However, it will not end when stop is put in as the employee name. I do not understand what went wrong.


package payroll.program;
// This program calculates weekly payroll
import java.util.Scanner;
public class PayrollProgram 
{    
public static void main(String args []) 
    {     
    Scanner input = new Scanner(System.in);
	 
        String cleanInputBuffer;
	String employeeName;
	double number1;
	double number2;
	double product;
	boolean stop = false;
        while (stop == false)
	        {
            number1 = -1;
	    number2 = -1;
	    System.out.print("Enter Name of Employee:");
	    employeeName = input.nextLine();
	if("stop".equals(employeeName.toLowerCase()))
	    stop = true; 
	    System.out.print("Enter a positive hourly rate:");
	    number1 = input.nextDouble(); 
	            
	while (number1 < 0)
{
	    System.out.print("Enter a positive number of hours worked:");
	    number2 = input.nextDouble();
    }
	    product = number1 * number2;
	    System.out.println("The employee %s was paid $ %.2f this week", employeeName, product);
            cleanInputBuffer = input.nextLine();
	 
    }
	 
    }
	 
	}


Is This A Good Question/Topic? 0
  • +

Replies To: Program will not end when stop is inputed

#2 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10803
  • View blog
  • Posts: 40,257
  • Joined: 27-December 08

Re: Program will not end when stop is inputed

Posted 14 August 2011 - 10:19 PM

First, please properly indent your code. It makes it much easier for us to read.

For your issue, I would add a break; statement in your if statement comparing "stop" to employeeName. A break statement will jump out of the loop.
Was This Post Helpful? 1
  • +
  • -

#3 Irish_hammer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 07-August 11

Re: Program will not end when stop is inputed

Posted 14 August 2011 - 10:40 PM

Sorry about the wrong indentation. I added the break and it did not help.
Was This Post Helpful? 0
  • +
  • -

#4 supercorey  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 119
  • View blog
  • Posts: 207
  • Joined: 15-February 09

Re: Program will not end when stop is inputed

Posted 14 August 2011 - 10:54 PM

Irish_hammer, realistically, your code shouldn't even compile, nevertheless run. I just tried it myself. The issue here being your System.out.println("The employee %s was paid $ %.2f this week", employeeName, product); call. System.out.println() does not work like sprintf or the like. You need to change this to something like System.out.println("The employee "+employeeName+" was paid $"+product+" this week"); Also, please excuse my concatenation, but this should work. As for the break not working, I think this one faulty line should affect your whole program. Fix that print line and if something still isn't working, then we'll worry about the breaking.
Was This Post Helpful? 2
  • +
  • -

#5 Irish_hammer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 07-August 11

Re: Program will not end when stop is inputed

Posted 14 August 2011 - 11:05 PM

No need to be excused you are just trying to help. I replaced that line with one similar to yours. However, the program will not end when the employee name is replaced with stop. When I enter stop it recognizes it as a name.
Was This Post Helpful? 0
  • +
  • -

#6 supercorey  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 119
  • View blog
  • Posts: 207
  • Joined: 15-February 09

Re: Program will not end when stop is inputed

Posted 14 August 2011 - 11:12 PM

Odd. With my changes for the println() and macosxnerd101's addition of a break; statement, it worked for me:
import java.util.Scanner;

public class PayrollTester{
	public PayrollTester(){
		Scanner input = new Scanner(System.in);

		String cleanInputBuffer;
		String employeeName;
		double number1;
		double number2;
		double product;
		boolean stop = false;
		while (stop == false){
			number1 = -1;
			number2 = -1;
			System.out.print("Enter Name of Employee:");
			employeeName = input.nextLine();
			if("stop".equals(employeeName.toLowerCase())){
				stop = true;
                                break;
			}
			System.out.print("Enter a positive hourly rate:");
			number1 = input.nextDouble();

			while (number1 < 0){
			    System.out.print("Enter a positive number of hours worked:");
			    number2 = input.nextDouble();
			}
			 product = number1 * number2;
			 System.out.println("The employee "+employeeName+" was paid $"+product+" this week");
			 cleanInputBuffer = input.nextLine();
	   	 }

	}

}

Was This Post Helpful? 0
  • +
  • -

#7 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1820
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Program will not end when stop is inputed

Posted 14 August 2011 - 11:13 PM

Wheredid you put that break told before? Replace stop = true; by break;
Was This Post Helpful? 1
  • +
  • -

#8 Irish_hammer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 07-August 11

Re: Program will not end when stop is inputed

Posted 14 August 2011 - 11:30 PM

I tried it supercoreys way and yours smohd. It ran but still will not end when stop is used as a name. However, I am now getting a error.
"java.lang.ExceptionInInitializerError
Caused by: java.lang.RuntimeException: Uncompilable source code - payroll.program.PayrollProgram.payrollprogram is already defined in payroll.program.PayrollProgram
at payroll.program.PayrollProgram.<clinit>(PayrollProgram.java:44)
Could not find the main class: payroll.program.PayrollProgram. Program will exit.
Exception in thread "main" Java Result: 1"

This post has been edited by Irish_hammer: 14 August 2011 - 11:31 PM

Was This Post Helpful? 0
  • +
  • -

#9 supercorey  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 119
  • View blog
  • Posts: 207
  • Joined: 15-February 09

Re: Program will not end when stop is inputed

Posted 14 August 2011 - 11:35 PM

That's because the name I used for my class isn't the same as what you used for yours. Different package, too. I'm not even sure if I copied my package statement. Are you using an IDE of some sort? This would help point out the errors for you.
Was This Post Helpful? 1
  • +
  • -

#10 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1820
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Program will not end when stop is inputed

Posted 14 August 2011 - 11:35 PM

No need to change the whole program, just from your previous code make the changes which I told you. Just here:
 if("stop".equals(employeeName.toLowerCase()))
break; 

Was This Post Helpful? 1
  • +
  • -

#11 Irish_hammer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 07-August 11

Re: Program will not end when stop is inputed

Posted 14 August 2011 - 11:58 PM

supercorey, I just used your as a reference and smohd I did what you said and replaced
 stop = true;
with
 break;
and it did not work and i am still getting the same error. I remade the entire program and now it works??? maybe just a glitch. Thanks for the help.
Was This Post Helpful? 0
  • +
  • -

#12 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1820
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Program will not end when stop is inputed

Posted 15 August 2011 - 12:29 AM

Glad you get it working :)
Also you dont need variable String cleanInputBuffer, if you need to clean the line in scanner, just replace:
cleanInputBuffer = input.nextLine();

with
input.nextLine();

Was This Post Helpful? 0
  • +
  • -

#13 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,904
  • Joined: 06-March 08

Re: Program will not end when stop is inputed

Posted 15 August 2011 - 09:18 AM

No bneed to set to true the stop variable anymore if you use the break statement

if("stop".equals(employeeName.toLowerCase())) break;

will do the job and
while(stop == false)
can be replace by
while(true)
Was This Post Helpful? 0
  • +
  • -

#14 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2876
  • View blog
  • Posts: 11,051
  • Joined: 15-July 08

Re: Program will not end when stop is inputed

Posted 15 August 2011 - 11:45 AM

View PostIrish_hammer, on 15 August 2011 - 02:30 AM, said:

I tried it supercoreys way and yours smohd. It ran but still will not end when stop is used as a name. However, I am now getting a error.
"java.lang.ExceptionInInitializerError
Caused by: java.lang.RuntimeException: Uncompilable source code - payroll.program.PayrollProgram.payrollprogram is already defined in payroll.program.PayrollProgram
at payroll.program.PayrollProgram.<clinit>(PayrollProgram.java:44)
Could not find the main class: payroll.program.PayrollProgram. Program will exit.
Exception in thread "main" Java Result: 1"


This error has little to do with the error that you said you already fixed. This is do to a bad package set up and badly compiled source code. Try cleaning the project, and recompiling as well, after you have made the changes that the others talked about above.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1