Jump to content

Infinite loop.... Why?!

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1
SolidState

SolidState

    Learning Programmer

  • Members
  • PipPipPip
  • 38 posts
So I am working on this program for my Java Course, and am very perplexed as to why I have an infinite loop... Can someone please help me figure this out? Code is posted below:
/* Check Point: Payroll Program Part 2

Coded by Curtis Allyn Green

Friday, May 28th 2010 */


//import Scanner object from java.util package

import java.util.Scanner;


//declare the class

public class Payroll2

{

   //declare the main method

   public static void main(String args[])

   {

      //declare the method's variables

      String empName;

      double rate;

      double hours;

      int more = 1;





      //Create an instance of a Scanner object to gather input from the user

      Scanner input = new Scanner(System.in);


      //Create execution loop that terminates with an empName == "stop"

      while(more != 0)

      {

         //Prompt user to input values for each variable and retrieve them from input

         System.out.print("Enter Employee's Name or Enter stop to exit: ");

         empName = input.next();

         if(empName == "stop")

            more = 0;


         //Check for continue

         if(more == 1)

         {

            System.out.print("Enter Employee's Hourly Wage: ");

            rate = input.nextDouble();

            //Ensure that rate is a positive value

            if(rate <= 0) 

            {

               while(rate <= 0)

               {

                  System.out.print("Error non-positive input. Please enter a value greater than 0 for Employee's Wage: ");

                  rate = input.nextDouble();

               }

            }



            System.out.print("Enter The Hours Worked By Employee: ");

            hours = input.nextDouble();

            //Ensure that hours is a positive value

            if(hours <= 0) 

            {

               while(hours <= 0)

               {

                  System.out.print("Error non-positive input. Please enter a value greater than 0 for Employee's Hours: ");

                  hours = input.nextDouble();

               }

            }



            //Display the results

            System.out.printf("Employee: %s  Net Pay:  $%.2f\n", empName, (rate*hours));


         }//End continue check


      }//End execution while loop


   }//End main method


}//End class


#2
xWalnuT

xWalnuT

    Newbie

  • Members
  • Pip
  • 1 posts
Hey, SolidState.

I think that the problem is with line 32:
if(empName == "stop")
, but I haven't tested to see if it would fix the issue. I'm guessing that the infinite loop is coming from an incorrect condition, which is why I recommend using booleans instead of 0 and 1, but I guess it's the same exact idea and a matter of taste.

As for fixing line 32, you should use the method equals or equalsIgnoreCase to check for the same String. Using == is best for char's, ints, etc.

Here's the corrected line:
if(empName.equalsIgnoreCase("stop"));

equalsIgnoreCase is a better method too, because it can check for STOP, SToP, etc.

#3
SolidState

SolidState

    Learning Programmer

  • Members
  • PipPipPip
  • 38 posts
Thank you OHHHHH SOOO MUCH, xWalnut! This was killing my brain since I was at a total loss for why this was happening. Now I can submit my program without using the instructor's example, which makes me so happy, and I also learned about a series of interesting methods for manipulating string within the java.lang package. Thanks for the help!

#4
ahmed

ahmed

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 304 posts
== compares references i think correct me if i am wrong , you can use the equals function :) , if ( name.equals("stop") ) //do something