7 Replies - 12512 Views - Last Post: 29 August 2009 - 09:17 AM Rate Topic: -----

#1 trillian  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 25-August 09

how do I initialize this string variable?

Post icon  Posted 28 August 2009 - 09:11 PM

The message that I am getting is Payroll.java:30: variable employeeName might not have been initialized
While ( employeeName != "stop" )
/*
   Author: Jamie Simonds 
   Date Modified: August 28, 2009
   Purpose: To calculate weekly salary, then display's the result. 
*/

import java.util.Scanner;

public class Payroll
{
   // main method begins execution of Java application
   public static void main( String args [] )
   {
	  String employeeName; // employee name
	  int employeeCounter = 0; // number of employee's entered
	  double hourlyRate = 0; // salary earned per hour
	  double hoursWorked = 0; // hours worked for the week
	  double salary = 0; // total salary  
	  double weeklyPay = 0; // pay including overTime hours
	  double overtimeSalary = 0; // overtime salary only
	  
	  
	  // create payroll object to obtain input from command window
	  Scanner input = new Scanner( System.in );

	  Payroll myPayroll = new Payroll();

	  //processing phase
	  // loop until sentinel value read from user
	  while ( employeeName != "stop" )
	  {

	  System.out.println();  // prints blank line
	  // prompt for input and read employee name from user
	  System.out.println( "Enter the name of the employee or type 'stop' to quit: " ); 
	  employeeName = input.nextLine(); // read name entered from user
	  System.out.println();  // prints blank line 

	  System.out.println( "Enter the hourly pay rate: " ); // prompt
	  hourlyRate = input.nextDouble(); // read first number from user
	  System.out.println();   // prints blank line

	  System.out.println( "Enter the total number of hours worked for this week: " ); // prompt
	  hoursWorked = input.nextDouble(); // read second number
	  System.out.println();   // prints blank line 

	  } // end while

	  // displays error message if hourly rate is a negative number
	  if( hourlyRate <= -1 ) 
	  {
		 System.out.print( "Error1: The hourly rate must be a positive number! " );
	  }

	  // displays error message if hours worked are less than 1
	  if( hoursWorked <= 0 )
	  {
		  System.out.print( "Error2: The hours worked must be greater than 0! " );
	  }

	  // calculating overtime
	  if ( hoursWorked > 40 )
	  {
		 overtimeSalary = ( hoursWorked - 40 ) * ( salary * 1.5);
		 hoursWorked = 40;
	  }
	  else
		 overtimeSalary = 0;

	  // processing

	  salary = hourlyRate * hoursWorked; // mulitply's pay rate by hours worked
	  weeklyPay = salary + overtimeSalary;  // adds regular salary with overtime salary

	  
	  // display employee and salary
	  System.out.printf( "The total salary %s earned this week is $%.2d\n", employeeName, weeklyPay); // display employee's name and salary

   } // end method main
} // end class Addition


Is This A Good Question/Topic? 0
  • +

Replies To: how do I initialize this string variable?

#2 prankster  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 67
  • Joined: 31-July 09

Re: how do I initialize this string variable?

Posted 28 August 2009 - 09:24 PM

Java requires that you initialize your variables, there cant be any chance that one isn't initialized or it will through an error. To fix this:

String employeeName = "";


Was This Post Helpful? 0
  • +
  • -

#3 trillian  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 25-August 09

Re: how do I initialize this string variable?

Posted 28 August 2009 - 09:30 PM

Thanks, that fixed my compile error. However, for some reason I cannot enter a different employee named when I run the program. Any ideas/tips?


Much thanks,
Jamie
Was This Post Helpful? 0
  • +
  • -

#4 trillian  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 25-August 09

Re: how do I initialize this string variable?

Posted 28 August 2009 - 09:44 PM

I just had to re-arrange the code. Now it looks like this:
	  //processing phase
	  // loop until sentinel value read from user
	  while ( employeeName != "stop" )
	  {
	  // create payroll object to obtain input from command window
	  Scanner input = new Scanner( System.in );

	  Payroll myPayroll = new Payroll();

	  System.out.println();  // prints blank line
	  // prompt for input and read employee name from user
	  System.out.println( "Enter the name of the employee or type 'stop' to quit: " ); 
	  employeeName = input.nextLine(); // read name entered from user

Was This Post Helpful? 0
  • +
  • -

#5 prankster  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 67
  • Joined: 31-July 09

Re: how do I initialize this string variable?

Posted 28 August 2009 - 10:39 PM

Is everything functioning correctly now?
Was This Post Helpful? 0
  • +
  • -

#6 trillian  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 25-August 09

Re: how do I initialize this string variable?

Posted 29 August 2009 - 12:43 AM

View Postprankster, on 28 Aug, 2009 - 09:39 PM, said:

Is everything functioning correctly now?


No, I am getting logic errors. I am going back over my code and reworking it. :blink:
Was This Post Helpful? 0
  • +
  • -

#7 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5884
  • View blog
  • Posts: 12,769
  • Joined: 16-October 07

Re: how do I initialize this string variable?

Posted 29 August 2009 - 04:44 AM

First, it's a good practice to not declare variables before you need them. When you do declare them, assign the value they should have.

Here's your code with minor changes.
import java.util.Scanner;

public class Payroll {
	public static void main( String args [] ) {
		// this never gets used, wonder why
		int employeeCounter = 0; // number of employee's entered


		Scanner input = new Scanner( System.in );

		// huh?
		//Payroll myPayroll = new Payroll();

		// don't check here, just go forever
		while(true) {
		//while ( employeeName != "stop" ) {
			System.out.println( "Enter the name of the employee or type 'stop' to quit: " ); 
			String employeeName = input.nextLine();
				
			// check here
			if (employeeName.equals("stop")) { break; }

			System.out.println( "Enter the hourly pay rate: " );
			double hourlyRate = input.nextDouble();
			if( hourlyRate <= -1 ) {
				System.out.print( "Error1: The hourly rate must be a positive number! " );
				// now what?  I'm going to leave
				return;
			}


			System.out.println( "Enter the total number of hours worked for this week: " ); // prompt
			double hoursWorked = input.nextDouble(); // read second number
			if( hoursWorked <= 0 ) {
				System.out.print( "Error2: The hours worked must be greater than 0! " );
				return;
			} 
			
			// I don't understand why you're stoping here, let's not
			//} // end while

			double overtimeSalary = 0;
			if ( hoursWorked > 40 ) {
				overtimeSalary = ( hoursWorked - 40 ) * ( salary * 1.5);
				hoursWorked = 40;
			}

			double salary = hourlyRate * hoursWorked;
			double weeklyPay = salary + overtimeSalary;

			System.out.printf( "The total salary %s earned this week is $%.2d\n", employeeName, weeklyPay); // display employee's name and salary
			
			// you made it do the end
			// you have now successfully processed an employee
			// increment your counter
			employeeCounter++;
			
		} // loop end, not you can ask about a different employee
	}
	
}



Note, when I complied this code, it gave me an error that salary was not declared. This revealed that you'd been attempted to use the variable before you'd actually put a real value in it. Kind of nice to have a built in example of why the practice of late variable declaration is good.

Looking at the code, the fix would be:
// here we declare the variables just outside the logic that will fill them
double salary, weeklyPay;

if ( hoursWorked > 40 ) {
	salary = hourlyRate * 40;
	double overtimeSalary = ( hoursWorked - 40 ) * ( salary * 1.5);
	weeklyPay = salary + overtimeSalary;
} else {
	salary = hourlyRate * hoursWorked;
	weeklyPay = salary;
}


Was This Post Helpful? 1
  • +
  • -

#8 Atspulgs  Icon User is offline

  • D.I.C Regular

Reputation: 68
  • View blog
  • Posts: 380
  • Joined: 29-July 09

Re: how do I initialize this string variable?

Posted 29 August 2009 - 09:17 AM

while ( employeeName != "stop" )


You cannot com pare strings like that.
use
while(!employeeName.equals("stop"))

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1