Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: program loops never ending

  1. #1
    Junior Member
    Join Date
    Nov 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default program loops never ending

    My code compiles correctly. After entering the employee name, it repeats Enter hourly rate without letting me input data. Where have I gone wrong?
       // Payroll Program Part 2
       import java.util.Scanner; //uses class scanner
       import java.io.PrintStream;
     
       public class PayrollProgramPart2
       {
     
       // begin execution
       public static void main( String args[] )
          {
          // create scanner
     
          Scanner input = new Scanner(System.in );
     
          double number1; //input
          double number2; //input
          double sum; //sum of 1 and 2
          boolean end = false; //is input stop
          while (end == false) // end is false proceed
             {
             number1 = -1; both are -1
             number2 = -1; both are -1
             String name;
             System.out.print( "Enter Employee name:" ); //prompt
             name = input.nextLine();
             if(name.toLowerCase() == "stop")
                end = true; // stop is detected
       while loop while(number1<0) // since I set it at -1 ahould loop until a postive number is put in
     
                System.out.print( "Enter hourly rate:" ); //prompt
                number1 = input.nextDouble(); // read first number
     
                while(number2 < 0) // same as number1
                {
                   System.out.print( "Enter number of hours:" ); // prompt
                   number2 = input.nextDouble(); // read second number
                }
             sum = number1 * number2; //multiply numbers
     
             System.out.printf (" Weekly Pay for%S is $ %.2f\n", name, sum); // Wage 
     
       } // end 
          } // end
             } //end
                } // end
    Last edited by copeg; November 27th, 2010 at 05:09 PM. Reason: Code tags


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: program loops never ending

    Hello Ccogh05.

    Welcome to the forums.

    Whats with all the while loops? Take a look at this example:

    import java.util.Scanner;
     
    public class PayrollProgramPart2_jpf {
     
    	/**
    	 * JavaProgrammingForums.com
    	 */
    	public static void main(String[] args) {
     
    		Scanner input = new Scanner(System.in);
    		System.out.print("Enter Employee name:");
     
    		while(input.hasNext()){
    			String strInput = input.next();
    			if(strInput.equals("stop")){
    				System.exit(0);
    			}
     
    			String name = strInput;
     
    			System.out.print("Enter hourly rate:");
    			double number1 = input.nextDouble();
     
    			System.out.print("Enter number of hours:"); 
    			double number2 = input.nextDouble();
     
    			double sum = number1 * number2;
    			System.out.printf("Weekly Pay for%S is $ %.2f\n", name, sum);
     
    		}
     
    	}
     
    }
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  3. #3
    Junior Member
    Join Date
    Jan 2011
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: program loops never ending

    Quote Originally Posted by Ccogh05 View Post
    My code compiles correctly. After entering the employee name, it repeats Enter hourly rate without letting me input data. Where have I gone wrong?
       // Payroll Program Part 2
       import java.util.Scanner; //uses class scanner
       import java.io.PrintStream;
     
       public class PayrollProgramPart2
       {
     
       // begin execution
       public static void main( String args[] )
          {
          // create scanner
     
          Scanner input = new Scanner(System.in );
     
          double number1; //input
          double number2; //input
          double sum; //sum of 1 and 2
          boolean end = false; //is input stop
          while (end == false) // end is false proceed
             {
             number1 = -1; both are -1
             number2 = -1; both are -1
             String name;
             System.out.print( "Enter Employee name:" ); //prompt
             name = input.nextLine();
             if(name.toLowerCase() == "stop")
                end = true; // stop is detected
       while loop while(number1<0) // since I set it at -1 ahould loop until a postive number is put in
     
                System.out.print( "Enter hourly rate:" ); //prompt
                number1 = input.nextDouble(); // read first number
     
                while(number2 < 0) // same as number1
                {
                   System.out.print( "Enter number of hours:" ); // prompt
                   number2 = input.nextDouble(); // read second number
                }
             sum = number1 * number2; //multiply numbers
     
             System.out.printf (" Weekly Pay for%S is $ %.2f\n", name, sum); // Wage 
     
       } // end 
          } // end
             } //end
                } // end

    Just a word of advice, you should probably reduce your WHILEs to a minimum... since the higher the number of loops is the more complex your algorithm is. As for the ending problem, whenever you want to check if two strings you need to use the equals methods, == will only check if the objects' reference is the same. So in your case you'd have name.toLowerCase().equals("stop") or name.equalsIgnoreCase("stop'')

  4. #4
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: program loops never ending

    Another way might be to change the while loop to

    while(!strInput.equals("stop"))

    and get rid of if block.
    Last edited by javapenguin; January 20th, 2011 at 02:51 PM.

  5. #5
    Member
    Join Date
    Jan 2011
    Location
    Phoenix
    Posts
    49
    Thanks
    2
    Thanked 2 Times in 2 Posts

    Default Re: program loops never ending

    ok i agree with the previous posts with getting rid of the while statements.

    but as is, i found your error...

    while loop while(number1<0)
    // there needs to be a { here, otherwise only the next line is associated with the while loop
    System.out.print( "Enter hourly rate:" ); 
    number1 = input.nextDouble(); 
    // and here needs to be the } so the program knows that this is the intended stop point

    so basically when the program reads that number1<0 it then goes on to read the System.out.print line, and since there is no brackets the program assumes that is the only line you want to associate with the while loop therefore the next line is never reached and you have created a forever loop

    hope that helps

  6. #6
    Junior Member
    Join Date
    Feb 2011
    Location
    Kochi,India
    Posts
    18
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: program loops never ending

    I agree with previous posts. Too many while loops complicate your algorithm.

Similar Threads

  1. help with loops... :(
    By Macgrubber in forum Loops & Control Statements
    Replies: 2
    Last Post: November 2nd, 2010, 12:38 PM
  2. Program without loops and recursion
    By jayaram in forum Algorithms & Recursion
    Replies: 7
    Last Post: April 1st, 2010, 09:02 AM
  3. Weird issue with while loop ending/being skipped
    By ang3c0 in forum Loops & Control Statements
    Replies: 4
    Last Post: December 25th, 2009, 12:09 PM
  4. Program with for loops help
    By ixjaybeexi in forum Loops & Control Statements
    Replies: 23
    Last Post: October 8th, 2009, 10:05 AM
  5. [SOLVED] Fixing of bug in number guessing game
    By big_c in forum Loops & Control Statements
    Replies: 6
    Last Post: April 16th, 2009, 02:15 AM