Endless Loop will not stop and does not increment correctly

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

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

#31 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 13 October 2012 - 11:59 PM

From the first snippet of code you posted, move line 3 to right after line 12.

Keep in mind that Java is "testing" the variables as they look at the end of the while loop to decide whether it should run another iteration or not.

Try to run the code in debug mode and step through it line by line to look at the value of numStars for every iteration of the loop.

Once you validate the basics (entering 2 or 3 valid values, then entering -1), I would see what happens if I entered -1 as the first entry, then what happens if I entered 0.
Was This Post Helpful? 0
  • +
  • -

#32 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 14 October 2012 - 06:44 AM

Got it! I did not understand going through debug mode. I am using jGrasp and did not understand how that feature worked. So I learned another thing new! Geesh I hate being a newbie! :)
Was This Post Helpful? 0
  • +
  • -

#33 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 14 October 2012 - 07:39 AM

EUREKA!!!! Actually I had to move it down one more line than you recommended and I had to reposition the first instance of it as well, or it would not add the numbers together properly, it would make me enter the number twice!

All you guys are fantastic! Thank you for guiding me to the solution without really just giving it to me, although I probably needed a "Gibbs Slap" to the back of the head a couple times. It is appreciated!
Was This Post Helpful? 0
  • +
  • -

#34 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 14 October 2012 - 08:23 AM

OK SO now I feel like I slept at a Holiday Inn last night! I even figured out that in order to get it to not accept a value over 4 for the rating, I had to put an if statement along with another Double.parseDouble statement inside the while loop BEFORE closing it, and before my if numStars == -1. All because you guys made me think and helped me to understand what I was thinking! Now program works the way I wanted it to and the way it was prescribed to do so! THANKS!!!

So, Question...... Why did I have to put another Double.parseDouble in the if statement? It seems redundant to me to keep telling it the same thing.

I am thinking that it is because it needs to be reset after each time I tell it to do a different thing?
Was This Post Helpful? 0
  • +
  • -

#35 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 8001
  • View blog
  • Posts: 13,710
  • Joined: 19-March 11

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

Posted 14 October 2012 - 08:44 AM

Quote

So, Question...... Why did I have to put another Double.parseDouble in the if statement? It seems redundant to me to keep telling it the same thing.

I am thinking that it is because it needs to be reset after each time I tell it to do a different thing?


All user input comes in as a String. The parseDouble method is what's used to convert a String to a double, so you can treat it as a number (do math with it, compare it numerically to other numbers, etc)


So every time you get an input from the user, you need to convert it to a number. It would be nice if you could just call one thing to do this - maybe it would be called getUserInputAsDouble(). And you can, by writing a method:

private double getUserInputAsDouble(String mesg)
{
  numStarsString = JOptionPane.showInputDialog (mesg);
  return Double.parseDouble (numStarsString);
}


This takes your two steps and encapsulates them into one, which has a few advantages. For one thing, every time you need to get user input, you can just call this. Also, you don't need to have that extra holding variable in your program, which is one less thing to keep track of. But it also shows you how these two steps relate, which helps you later when you come back to this program: now you can see much more easily how the functionality "chunks" into logical pieces.

If you have a spare few minutes, read here and play with these ideas a little - see if you can clean up your code a little more! (you might have to start from the beginning of that trail, depends what you're ready for)

This post has been edited by jon.kiparsky: 14 October 2012 - 08:49 AM

Was This Post Helpful? 2
  • +
  • -

#36 YasuoDancez  Icon User is offline

  • D.I.C Head

Reputation: 20
  • View blog
  • Posts: 135
  • Joined: 30-September 09

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

Posted 14 October 2012 - 11:17 AM

What jon.kiparsky mentioned about separating code in that way is a great way to keep code organized, clean and readable. Reorganizing your code this way is associated with what is known as refactoring and encapsulation.

Cohesion is when one allows code portions to do only one task and do it well. methods and functions such as the getUserInputAsDouble(String msg) that jon mentioned should just do that one task that it describes, which it does just that.
Was This Post Helpful? 1
  • +
  • -

#37 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 14 October 2012 - 11:26 AM

COOL FREAKIN' BEANS!!! (sorry) Too excited.... So this new method would be at the beginning of the program, before the main method or just before housekeeping?
Was This Post Helpful? 0
  • +
  • -

#38 YasuoDancez  Icon User is offline

  • D.I.C Head

Reputation: 20
  • View blog
  • Posts: 135
  • Joined: 30-September 09

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

Posted 14 October 2012 - 11:35 AM

It can be before or after main. It is not executed in a sequential order. That's the nature of Object Oriented Programming. C++ is another story.
Was This Post Helpful? 2
  • +
  • -

#39 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 8001
  • View blog
  • Posts: 13,710
  • Joined: 19-March 11

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

Posted 14 October 2012 - 05:26 PM

Well, technically we're still in procedural programming as yet. This is just an ordinary function call, like you'd find in any other program.

We'll hold off on the OO tricks for now, I think.



Quote

So this new method would be at the beginning of the program, before the main method or just before housekeeping?


It can be defined anywhere in the class body. A class body, briefly, declares a set of class members - these are fields (class-level variables) and methods (subroutines - little bits of named functionality).

Read up that link for more of the details on the rules and regulations of methods. There's a lot to learn there, and it would be silly for me to type it all in when it's already there for you.
Was This Post Helpful? 1
  • +
  • -

#40 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 20 October 2012 - 09:56 AM

Do any of you guys do anything with visual logic? I keep going round and round and while I have the thing at least not erroring it's not giving me the output I want. It's only going through the loop once.... I have followed the instructions for reading from a txt file but it's just not doing it... not sure what the problem is really. we've only used Visual Logic one other time.
Was This Post Helpful? 0
  • +
  • -

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