• Post Reply Bookmark Topic Watch Topic
  • New Topic

Having Trouble with my while loop  RSS feed

 
James Sea
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to race these two cars to 500 but the loop never executes, does anyone know why ? I already have Vehicle class thats is where these methods are coming from if you're wondering.

 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have added code tags to your post and removed the worst of the too many blank lines, which don't help legibility. I didn't correct the lines that are too long (13 and 15). You appear to know how to deal with lines too long; you simply need to be a bit more aggressive about it.
I am afraid you have some non‑object‑oriented style there. There is something wrong with printing the details of the cars like that. You sho‍uld override the Car's toString method and simply write
System.out.println(car1);
There is also something wrong with those multiple if blocks with random numbers. They would probably be better as switch-case statements.
Why are all those methods static? Except before the main method, you shou‍ld regard any occurrences of the keyword static without a good explanation as a serious mistake. And the fact that there are error messages about non‑static does not constitute a good explanation. Those methods shou‍ld all be non‑static members of a Race class.
You also have repeated code; I think you would be better off with an array of Cars and take turns. You can simply take turns like this:-
Car nextCar = myCarArray[index++ % myCarArray.length];
That code will of course cope with arrays of any length but will fall over horribly when index passes 2147483647 I shall leave you to work out whether you shou‍ld start with index at 1, 0 or −1.
What does line 18 do? What does distance print out? Why isn't distance a field in the Car class?
Why have you got two loops? You will run car1 to the end of the race before car2 even starts.
 
Junilu Lacar
Sheriff
Posts: 11493
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Besides what Campbell said about having two loops, are you sure the variables that are checked in your loop conditions on lines 22 and 41 will ever change?  If you think they are, can you tell us the exact line numbers where they are changed to a value that will make the loop condition false?
 
James Sea
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I have changed my code to make it one loop but it still does not execute, line 18 was just a test to see if I called the method right.

 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Start by adding some print statements. Start the loop with
System.out.println("\u2691 They\u2019re off!");
No, that is what they say in horseracing, not motor racing .
At the end of each loop print out the status of both cars, which shou‍ld include speed and distance. Then see if you can work out what I missed and Junilu noticed.
 
Junilu Lacar
Sheriff
Posts: 11493
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
James Sea wrote:
I have changed my code...

Stop. Think. Analyze that line of code.

When either vehicle reaches 500, will the loop terminate when the other one hasn't yet gotten to 500?

This section of your code in particular is very poorly formatted:

Again, stop. Think. Analyze the code and understand the flow of this logic.  Braces group several statements together. The braces on line 46 and line 59 bookend a group of statements that will be executed in sequence only when the condition on line 46 is met. Is this really what you want to do?
 
James Sea
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want it to end the loop when the first car reaches 500 and then to print out the winner and how far the loser went
 
James Sea
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I had it print out distance and speed after every loop and I figured out that its not doing anything it looks like something with my random number is messed up because it never calls the accelerate or brake methods
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
James Sea wrote:. . .  something with my random number is messed up because it never calls the accelerate or brake methods
Put print methods in the accelerate and brake methods and you will see they are indeed being called. What did it print for distance? Was it the same every time?
 
Junilu Lacar
Sheriff
Posts: 11493
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't have to create a Random object every time you go through the loop. That's what you've coded though. Put the new Random() statement outside of your loop.

Also, with nextInt(n), the return values won't include n; the range of return values is (0 .. n-1) inclusive.  So nextInt(3) will only return one of these: 0, 1, 2

You could also use nextBoolean() instead:

Don't name your instance of Random() as num. That's misleading. It's a pseudo random generator, it's not a number.  Name it rand instead. That's a very common name to give a Random object and everybody recognizes it as such right away.
 
Junilu Lacar
Sheriff
Posts: 11493
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And you still haven't answered my previous questions:

1. Where in your code are you changing the value of your loop variables such that the loop will eventually terminate?

2. When either vehicle reaches 500, will your while-loop terminate if the other vehicle still hasn't reached 500?
 
James Sea
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:And you still haven't answered my previous questions:

1. Where in your code are you changing the value of your loop variables such that the loop will eventually terminate?

2. When either vehicle reaches 500, will your while-loop terminate if the other vehicle still hasn't reached 500?


1. I have it where the distance equals the speed the car is going (get speed) divided by 60 so every loop is a min. By that I was thinking every time the loop executes generates a random number decides whether to accelerate or brake it would get the speed and divide the it by 60 keep looping and eventually get to 500.

2. Yes it will terminate because I put the "||" known as "or" in the while loop so once one of the distances gets to 500 it should stop
 
James Sea
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
James Sea wrote:. . .  something with my random number is messed up because it never calls the accelerate or brake methods
Put print methods in the accelerate and brake methods and you will see they are indeed being called. What did it print for distance? Was it the same every time?


yes it prints 0 for everything continuously
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please go through your code and tell us all the lines where distance is being changed.

And why isn't distance a field of the Car class?
 
James Sea
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Please go through your code and tell us all the lines where distance is being changed.

And why isn't distance a field of the Car class?


I just made distance a field of the Car class but I must be confused because Im thinking every time it goes through the loop it updates the distance because i have distance = get speed / 60. 60 being how many seconds are in a minute
 
Junilu Lacar
Sheriff
Posts: 11493
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
James Sea wrote:2. Yes it will terminate because I put the "||" known as "or" in the while loop so once one of the distances gets to 500 it should stop

Well, you're mistaken. That's not how logical OR works. Go back and review https://docs.oracle.com/javase/tutorial/java/nutsandbolts/op2.html and check your understanding.

As a reminder, this is what you wrote:

This loop will NOT terminate as soon as one of the variables checked reaches 500.
 
James Sea
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Updated Code
 
Carey Brown
Saloon Keeper
Posts: 3323
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I find your use of blank lines and indentation to be misleading. Only use single blank line instead of double (or more) lines.
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
James Sea wrote:. . . I just made distance a field of the Car class but I must be confused because Im thinking every time it goes through the loop it updates the distance because i have distance = get speed / 60. . . .
Now that is new. You are going places () if you can change the loop header like that. But why are you using the || operator. Read the loop header carefully pronouncing || “OR”.

What is speed? Is it an int or something? What will speed / 60 evaluate to? If speed is 99999999999? If speed is 119? If speed is 60? If speed is 59?
Please show us how you are updating distance in the Car class.
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
CB makes a good point: please look at this part of our formatting suggestions. What is written there looks picky, but when you look at your code, you find it makes a very significant difference.
 
Junilu Lacar
Sheriff
Posts: 11493
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
James Sea wrote:Updated Code

That still won't work. Go back and check your understanding of how logical OR works versus logical AND.
 
Carey Brown
Saloon Keeper
Posts: 3323
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:I find your use of blank lines and indentation to be misleading. Only use single blank line instead of double (or more) lines.

Is this what your really meant to do?

or are you missing braces and it should really look like this?

Without the braces you will have the second car break (line 32) and then accelerate (line 37).
 
Junilu Lacar
Sheriff
Posts: 11493
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
James Sea wrote:Updated Code

Why did you move line 19 outside of your while loop? This will cause the num1 variable to have only one value the entire time.  That line should go back inside the loop if you want to have a different value every time through. Line 18 is what can be outside the loop. Like I said before, you only need to create the random generator once. Putting line 18 outside the loop achieves that.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!