Hi!
I'm really fed up with my program. I'm trying to write a JAVA simulation of a traffic jam. In the beginning the user inputs the length a of Road n (problem with the constructor) and the number of cars c. The Road is an object- simply an array filled with 1 or 0 in the beginning. The constructor is supposed to create such a road of length n with c cars (values 1 in array) putting them on it in random places (starting from beginning).
To do this I wrote another piece of code which generates randomly 0 or 1. It works.
Then I try to do it for two cases- where there are more empty spaces and when there are more cars .... Then starting from the beginning of the array the program puts 0 or 1 into each position. When it reaches the value of cars, the for-loop terminates and leaves the rest of the road blank. This is fine for me. In this case that would be the road I would want to use. However sometimes generating a lot of 0s, the for loop could get to the end of the road not putting all the cars into their places. Thats why I use the do while loop- to check that in the end and if it is the case to try again. The problem is that it never terminates. After a road is made and "allocated" clearly prints as equal to c it goes back and starts all over. What should I do in this case?

This is the code:

class Road {

    private int n;
    private int c;
    private int road[];
    private int allocated;
    private int assigned;
   
    /// constructor for road taking length and number of cars putting them down randomly
    public Road(int n, int c){
	
	this.road = new int[n];	
	/// if c>(n/2) then allocate 0s, if c<=n/2 allocate 1s

	if(c < (n/2)){
	   
	    do{
		int allocated = 0;
		for(int i = 0; ((allocated < c) && (i < n)); i++){
		    this.road[i]=binaryGenerator();
		    if(this.road[i]==1) {
			allocated = allocated +1;
		    	}
		   }
		System.out.println("T "+ allocated +"  " + c);
	    }
	    while(allocated < c);
	   
	}

	if(c >= (n/2)){
	    do{
		int assigned = 0;
		for(int i = 0; ((assigned < (n-c)) && (i < n)); i++){
		    this.road[i]=binaryGenerator();
		    if(this.road[i] == 0) {
			assigned = assigned +1;
	       
		    }
		}
	    }
	    while(assigned < (n - c));
	}
    }

Recommended Answers

All 5 Replies

"allocated" clearly prints as equal to c

But the condition in your do-while loop is

while(allocated < c)

So definitely it wouldn't stop because technically "allocated" is not less than "c".

If you want it to terminate when "allocated" and "c" are equal also then change the "<" to "<="

Hey, but wait!

Doesn't the do- while loop work in a way that it first executes the code and at the end evaluates the boolean? If the boolean is true it should go for another round, if false it should terminate. Right?

If this is correct then my boolean should be correct. If allocated would be smaller than c it would continue, else if equal it should stop?
Is this correct?

Hey, but wait!

Doesn't the do- while loop work in a way that it first executes the code and at the end evaluates the boolean? If the boolean is true it should go for another round, if false it should terminate. Right?

If this is correct then my boolean should be correct. If allocated would be smaller than c it would continue, else if equal it should stop?
Is this correct?

Eeep !!! Sorry Confused myself on that one, heres what I think is going wrong.
Now I just went through your code the problem seems to be a tad more deeper than I thought.

You see at the class level itself you have a member called allocated.

private int allocated;

Now let us look closely at your do-while loop.

do{
  int allocated = 0;
  for(int i = 0; ((allocated < c) && (i < n)); i++){
    this.road[i]=binaryGenerator();
    if(this.road[i]==1) {
      allocated = allocated +1;
    }
  }
  System.out.println("T "+ allocated +"  " + c);
} // This is where the scope of the "allocated" variable
  // declared inside the loop ends
while(allocated < c); // The allocated being referred to here is
                                // the one declared at the class level

Now inside you are manipulating the variable "allocated" which is declared inside the do-while block and it is this variables variable that is printed in your System.out.println("T "+ allocated +" " + c); , However thats where the scope of this "allocated" variable ends. In the while condition here while(allocated < c); the "allocated" being referred to is the one that has been declared at the class level which is never manipulated (and hence "0" all the time). As a result the while condition of your do-while is always true and thats why it goes back to start of the do-while even though the inner "allocated" already is already equal to "c".
The remedy to this problem is moving the declaration of the "allocated" variable from inside the do-while loop to just before the starting of the do-while block.

commented: helpful +17

Post the code for binaryGenerator method so that we would be able to run and chekc your code, what happens exactly.

And yes BTW you are right about the do-while construct if it shows allocated as equal to c it should stop.

Yes the (edited) previous post has the answer to your question.

stephen84s beat me to the previous post and then did not beat me while editing it. So kinda my previous post looks to be asking for the method code even after the correct answer has been given. :)

I hate you - stephen84s ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.