Endless Loop will not stop and does not increment correctly

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

39 Replies - 4178 Views - Last Post: 20 October 2012 - 09:56 AM Rate Topic: -----

#1 ValeriePurvis  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 78
  • Joined: 21-September 12

Endless Loop will not stop and does not increment correctly

Posted 12 October 2012 - 09:30 AM

This is not homework, it's just practice (because obviously I need it) and I'm not sure what I'm doing incorrectly. I followed the instructions that were given. When I execute the program it loops through what looks like twice, after the 2nd loop it finally keeps incrementing the number of patrons but does not add (accumulate) the number of stars. I have tried changing doubles to integers and removing the initialization of the totalStars = 0 I want it to add up the entries of the number of stars and then average them at the end to give an average star rating. Here's what I have so far.
// MovieGuide.java - This program allows each theater patron to enter a value from 0 to 4 
// indicating the number of stars that the patron awards to the Guide's featured movie of the 
// week. The program executes continuously until the theater manager enters a negative number to 
// quit. At the end of the program, the average star rating for the movie is displayed.  

import javax.swing.JOptionPane;

public class MovieGuide2 
{
	public static void main(String args[])
	{
                
		// Declare and initialize variables.
		double numStars;         // star rating.
		String numStarsString; 	 // string version of star rating
		double averageStars;  	 // average star rating.
		double totalStars = 0; 	 // total of star ratings.
		int numPatrons = 0;      // keep track of number of patrons
			
	
		// This is the work done in the housekeeping() method
		
		{
		numStarsString = JOptionPane.showInputDialog ("Enter Number of Stars between 1 and 4: ");// Get input.
			
		// This is the work done in the detailLoop() method
			
		numStars = Double.parseDouble (numStarsString);	// Convert to double.
		while (numStars > 0)			// Write while loop here
		{
			totalStars =+ numStars;
			System.out.println ("Number of Patrons Responding: " + numPatrons);
			numPatrons ++;
			System.out.println ("Number of Stars Given: " + numStars);
	                numStarsString = JOptionPane.showInputDialog ("Enter Number of Stars between 1 and 4: ");// Get input.

			{
			if (totalStars == -1)
			{
			averageStars = (totalStars/numPatrons);			// Calculate average star rating
			System.out.println("Average Star Value: " + averageStars);
			}
					}
				}
		
			// This is the work done in the endOfJob() method
					System.out.println ("End of File");
					
			}		

					
		System.exit(0);
	} // End of main() method.

} // End of MovieGuide class.



It's not missing a curly brace because it submitted when I was making some adjustments. - sorry about that.

Quote

It's not missing a curly brace because it submitted when I was making some adjustments. - sorry about that.


It is NOW missing a curly brace because it submitted when I was making some adjustments. Sorry....

This post has been edited by jon.kiparsky: 12 October 2012 - 09:34 AM
Reason for edit:: fixed code tags - [/code], not [END CODE]


Is This A Good Question/Topic? 0
  • +

Replies To: Endless Loop will not stop and does not increment correctly

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: Endless Loop will not stop and does not increment correctly

Posted 12 October 2012 - 09:33 AM

If the loop control variable, numStars, doesn't change in the loop, then the condition will always be either true or false. At a glance, I don't see where numStars changes in the loop.

I don't get the second part of your question.
Was This Post Helpful? 3
  • +
  • -

#3 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7805
  • View blog
  • Posts: 13,199
  • Joined: 19-March 11

Re: Endless Loop will not stop and does not increment correctly

Posted 12 October 2012 - 09:37 AM

while (numStars > 0)		


Since numStars does not change within the body of the loop, this will always be true. If you want to exit the loop, you'll have to find a way to alter this.
This will probably involve getting a new rating from the user within the loop.
I'm not sure I'd want to use zero stars as the sentinel value. After all, that's the rating I give to most movies these days.
Was This Post Helpful? 1
  • +
  • -

#4 rfs02  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 26
  • View blog
  • Posts: 70
  • Joined: 30-September 12

Re: Endless Loop will not stop and does not increment correctly

Posted 12 October 2012 - 09:47 AM

Also, this line

totalStars =+ numStars;


Should be

totalStars += numStars;

Was This Post Helpful? 3
  • +
  • -

#5 ValeriePurvis  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 78
  • Joined: 21-September 12

Re: Endless Loop will not stop and does not increment correctly

Posted 12 October 2012 - 12:49 PM

In my while I had it read while ( numStars != -1) It didn't stop the loop when -1 was entered.

And I had totalStars += numStars to begin with but it didn't work then either so I changed it! I'll put it back.

I do ask for next input at the end of the loop.

35 numStarsString = JOptionPane.showInputDialog ("Enter Number of Stars between 1 and 4: ");// Get input.

Is this not the proper way to ask for the next Patron to enter their rating?

View PostGregBrannon, on 12 October 2012 - 09:33 AM, said:

If the loop control variable, numStars, doesn't change in the loop, then the condition will always be either true or false. At a glance, I don't see where numStars changes in the loop.

I don't get the second part of your question.


numStars is what is input by the Patron as in Enter a Rating between 1 and 4..... numStars, added to totalStars should increase each Patron that enters a rating correct?

Thank you for taking the time to help me. I do need to comprehend this stuff Going back to school at 46 is very painful sometimes! I do appreciate the help.
Was This Post Helpful? 0
  • +
  • -

#6 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: Endless Loop will not stop and does not increment correctly

Posted 12 October 2012 - 02:11 PM

Focusing on the loop:
while (numStars > 0)			// Write while loop here
{
    totalStars =+ numStars;
    System.out.println ("Number of Patrons Responding: " + numPatrons);
    numPatrons ++;
    System.out.println ("Number of Stars Given: " + numStars);
    numStarsString = JOptionPane.showInputDialog ("Enter Number of Stars between 1 and 4: ");// Get input.

    {
        if (totalStars == -1)
        {
            averageStars = (totalStars/numPatrons);			// Calculate average star rating
            System.out.println("Average Star Value: " + averageStars);
        }
    }
}

In order for numStars to change within the loop, it needs to be on the left side of an assignment statement, as in:

numStars = some expression;

or acted upon by a postfix or unary operator.

Your comments,

Quote

numStarsString = JOptionPane.showInputDialog ("Enter Number of Stars between 1 and 4: ");// Get input.

and

Quote

numStars is what is input by the Patron as in Enter a Rating between 1 and 4..... numStars, added to totalStars should increase each Patron that enters a rating correct?

suggest you're thinking that a new value for numStarsString changes numStars. That's simply wrong thinking. You'll need to update numStars with the new numStarsString value as you'd done previously at line 28:

numStars = Double.parseDouble (numStarsString);

This post has been edited by GregBrannon: 12 October 2012 - 02:12 PM

Was This Post Helpful? 2
  • +
  • -

#7 ValeriePurvis  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 78
  • Joined: 21-September 12

Re: Endless Loop will not stop and does not increment correctly

Posted 12 October 2012 - 09:00 PM

I SEE! the Double.parseDouble has to be inside the loop! it WORKED! Well for the most part. It's still accepting any number. I need to make it only accept 1 through 4. I"ll get there. But I feel a whole lot better learning what I have.

Do I still need the numStars = Double.parse.double at line 28?
Was This Post Helpful? 1
  • +
  • -

#8 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7805
  • View blog
  • Posts: 13,199
  • Joined: 19-March 11

Re: Endless Loop will not stop and does not increment correctly

Posted 12 October 2012 - 09:15 PM

I love that "aha!" moment - thanks for letting me enjoy it vicariously. :)

About the parseDouble on line 28, without it numStars will be uninitialized. This is usually a bad thing. In this case the compiler will complain, because you're reading a value from an uninitialized variable (sometimes the compiler will initialize a value to zero for you, which may or may not be where you want it to be).

You could do a clever trick to avoid needing that initial "pump priming" input. If you make your loop a "do while" loop, it'll always go through the loop at least once. To do this, you start the loop with "do" and put the condition at the end:
do {
  STATEMENTS
} while (CONDITION);

Was This Post Helpful? 1
  • +
  • -

#9 ValeriePurvis  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 78
  • Joined: 21-September 12

Re: Endless Loop will not stop and does not increment correctly

Posted 13 October 2012 - 09:07 AM

I like the way you explain things jon! I like Mr. Brannon's explanations too, but sometimes he makes me feel stupid.... I wondered if I could do that and almost did. I worked a while longer last night, and the problem it is having now is that when the -1 stopper input is entered it messes up the output (it doesn't calculate properly) because it includes the -1 once. I have to input it twice before it will accept it as the "sentinel?" value to continue to the next step.

So in the Do/While loop I would put the double parse and all the other statements, while numStars is >0 || <5 && !=0 ???? or something like that. I was looking at how I might incorporate a for statement to maybe simplify stuff a little, but my brain just wouldn't work after midnight last night.

oh, and you have semicolon after the while statement in your example, is that there because it's the end of that part of the loop like in the statements and only because it's part of the do/while loop? or should there be a semicolon after the statements in the do part?
Was This Post Helpful? 0
  • +
  • -

#10 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: Endless Loop will not stop and does not increment correctly

Posted 13 October 2012 - 09:11 AM

Sorry.
Was This Post Helpful? 3
  • +
  • -

#11 ValeriePurvis  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 78
  • Joined: 21-September 12

Re: Endless Loop will not stop and does not increment correctly

Posted 13 October 2012 - 09:28 AM

@ Greg Brannon - Don't be. If I didn't feel challenged I would be less inclined to try harder! Besides, smart people should never feel sorry for being smart. It took me a long time to realize I had to talk differently to people so I didn't offend them. I'm well read, I am not innocent to the world sometimes come off as a know it all.... and a bit harsh.... I know I don't know everything but I'm darn proud of what I do know and what I've learned! So no offense is ever taken... Thank you for your expertise and willingness to help! Lot's and lots of kudos to you! :)
Was This Post Helpful? 1
  • +
  • -

#12 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7805
  • View blog
  • Posts: 13,199
  • Joined: 19-March 11

Re: Endless Loop will not stop and does not increment correctly

Posted 13 October 2012 - 09:33 AM

View PostValeriePurvis, on 13 October 2012 - 11:07 AM, said:

I like the way you explain things jon! I like Mr. Brannon's explanations too, but sometimes he makes me feel stupid....


Thanks. Greg's grand, I've learned a lot from his explanations. He's certainly one of the most patient explainers I've come across anywhere.

Quote

I wondered if I could do that and almost did. I worked a while longer last night, and the problem it is having now is that when the -1 stopper input is entered it messes up the output (it doesn't calculate properly) because it includes the -1 once. I have to input it twice before it will accept it as the "sentinel?" value to continue to the next step.


This is a logic error that I'm pretty sure you can solve. I use a white board for this sort of thing, it's very useful. The important thing, I think, is to understand the code as the machine sees it - what you really said, not what you meant to say. I call this "running the code in your head" - you step through the thing with some representative data and keep track of how everything changes.

Doing this, I think you'll see how to fix this problem. There are actually two ways, one of which would involve putting the evalutation of the input into a conditional branch, only evaluating the input if a certain condition is met, and the other would involve an early exit from the loop.
The two are formally equivalent, it turns out, and the difference is really one of how you communicate your intent to the reader of the program. The compiler will generate almost exactly the same thing!
So maybe you can take it as a challenge to find both of those approaches and make each of them work. That would probably be useful and might even be interesting.


Quote

So in the Do/While loop I would put the double parse and all the other statements, while numStars is >0 || <5 && !=0 ???? or something like that. I was looking at how I might incorporate a for statement to maybe simplify stuff a little, but my brain just wouldn't work after midnight last night.


[/quote]

Quote

oh, and you have semicolon after the while statement in your example, is that there because it's the end of that part of the loop like in the statements and only because it's part of the do/while loop? or should there be a semicolon after the statements in the do part?



My bad. That's how I typically write abstract syntax, but I see how it could be confusing. Where I have the word "STATEMENTS" I mean a legitimate block of java statements, which can be a single line like
System.out.println("Foo!");


or even :
;  // empty block: nothing happens here


or any collection of such statements within curly braces. This can include, among other things, conditional branches, loops, method calls, and early exit instructions (ie, break or continue - which you might want to look up if you haven't done so)


The empty block is a common error that catches most beginners at least once. It comes about accidentally if you put a semicolon after a loop instruction:

while (foo);  // <- error here
{
  STATEMENTS
}



This is equivalent to

while (foo)
{
  ;  // <- error here

}
{
  STATEMENTS
}



Which is to say, do nothing as long as "foo" is true. Unless some other object has a way to change the value of foo, this program will either skip the empty block or stay in it forever.
Was This Post Helpful? 1
  • +
  • -

#13 ValeriePurvis  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 78
  • Joined: 21-September 12

Re: Endless Loop will not stop and does not increment correctly

Posted 13 October 2012 - 09:43 AM

See I am learning things! :)
Was This Post Helpful? 1
  • +
  • -

#14 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7805
  • View blog
  • Posts: 13,199
  • Joined: 19-March 11

Re: Endless Loop will not stop and does not increment correctly

Posted 13 October 2012 - 09:46 AM

That's the fun part of programming: getting to look at a program you wrote and realizing that until recently you had no idea how to make something do that.

Similar fun can be had with math - when you first look at the Seven Bridges problem (for example) and you find your way to a correct solution, suddenly you're a different person, and that's a pretty cool feeling as well.
Was This Post Helpful? 0
  • +
  • -

#15 ValeriePurvis  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 78
  • Joined: 21-September 12

Re: Endless Loop will not stop and does not increment correctly

Posted 13 October 2012 - 09:53 AM

You just ruined my day! Math != FUN :) Well I suppose I have a fair amount of fun with accounting but others might find that exceptionally boring! I will have to try to look at math a little differently then.... perhaps.

Have a question.... been thinking..... would I want to do an if numStars.equal (-1) to trigger the exit to the loop if that statement were true?
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3