Welcome to Dream.In.Code
Become a Java Expert!

Join 149,493 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,333 people online right now. Registration is fast and FREE... Join Now!




While and If

 
Reply to this topicStart new topic

While and If

poleman
24 Mar, 2007 - 01:05 PM
Post #1

New D.I.C Head
*

Joined: 24 Mar, 2007
Posts: 6


My Contributions
CODE

public class Main
{

public String name; public int startX; public int endX; public int startY; public int endY;  //road vars
public String vname; public double speed; public double xlocation; public double ylocation;  //vehicle vars
public String set; public boolean ared; public boolean aredamber; public boolean aamber; public boolean agreen; //light vars
        public boolean bred; public boolean bredamber; public boolean bamber; public boolean bgreen;


    public static void main(String[] args)
    {
        Road h1 = new Road ("H1", 0, 600, 50, 100);
        Road h2 = new Road ("H2", 0, 600, 150, 200);
        Road h3 = new Road ("H3", 0, 600, 250, 300);
        Road v1 = new Road ("V1", 50, 100, 0, 400);
        Road v2 = new Road ("V2", 150, 200, 0, 400);
        Road v3 = new Road ("V3", 250, 300, 0, 400);
        Vehicle c1 = new Car ("Car 1", 5, 0, 52);       //test car designed to travel west to east along h1
        Vehicle c2 = new Car ("Car 2", 5, 52, 0);          //test car north to south v1
        Vehicle b1 = new Bike ("Bike 1", 15, 20, 51);
        Lights j11 = new Lights ("Set 1", false, false, false, true, false, false, false, false); //lights at junction 11
        j11.setStateAgreen(false);
        j11.setStateBgreen(false);
        
        int i=0;
        int k=0;
        
         while (c1.getXLocation() <600)
            if (j11.getStateAgreen() ==false && ((Vehicle)c1).getXLocation()<=50 & ((Vehicle)c1).getXLocation()>=47)
               {c1.xlocation+= 0; i++;        
               System.out.println("Car 1 has stopped at lights at X position " + c1.xlocation);
               break;
               }
            else {
                c1.ylocation+=((Vehicle)c1).getSpeed(); i++;
                 }
        
        
//        while (c2.getYLocation() <450)
//            if (j11.getStateBgreen() ==false && ((Vehicle)c2).getYLocation()<=50 & ((Vehicle)c2).getYLocation()>=47)
//               {c2.ylocation+= 0; k++;        
//               System.out.println("Car 2 has stopped at lights at y position " + c2.ylocation);
//               break;
//            }
//            else {
//                c2.ylocation+=((Vehicle)c2).getSpeed(); k++;
//            }
        }
}


The aim of the above code is to move a vehicle along a road. If the traffic lights are not set to green, the vehcile must stop at them. This seems to workfor c2, but not c1. Cannot understand as code is the same.
Ideally I want to add several of these but as soon as I take the commenting //'s out the program just seems to get "stuck" running with no break or output.

Any ideas what I ca do or what is wrong?
User is offlineProfile CardPM
+Quote Post

DilutedImage
RE: While And If
24 Mar, 2007 - 04:50 PM
Post #2

D.I.C Addict
Group Icon

Joined: 20 Nov, 2006
Posts: 643



Thanked: 6 times
Dream Kudos: 25
My Contributions
Perhaps some curly brackets around your while statements would be a good place to start?


User is offlineProfile CardPM
+Quote Post

poleman
RE: While And If
25 Mar, 2007 - 10:07 AM
Post #3

New D.I.C Head
*

Joined: 24 Mar, 2007
Posts: 6


My Contributions
QUOTE(DilutedImage @ 24 Mar, 2007 - 05:50 PM) *

Perhaps some curly brackets around your while statements would be a good place to start?




Thanks, code is now:
CODE

while (c1.getXLocation() <600){
            if (j11.getStateAgreen() ==false && ((Vehicle)c1).getXLocation()<=50 && ((Vehicle)c1).getXLocation()>=47){              
               System.out.println("Car 1 has stopped at lights at X position " + c1.xlocation);
            }else
               c1.ylocation+=((Vehicle)c1).getSpeed();
               }


but still having problems. In order for it to compile I had to remove the extra "actions" that I had requested as a result of the if statement (i++; break;). Not sure if there is another easy way to get my program to perform these in addition to the system.out.

Still seems to just execute and run and run and run and run........

User is offlineProfile CardPM
+Quote Post

William_Wilson
RE: While And If
25 Mar, 2007 - 10:33 AM
Post #4

lost in compilation
Group Icon

Joined: 23 Dec, 2005
Posts: 4,101



Thanked: 25 times
Dream Kudos: 3275
Expert In: Java, C, Javascript

My Contributions
you are still missing a closing bracket } on your while loop, you can only leave out the curly brackets if there is only 1 line inside the loop, but even so it is not recommended to leave them out.

just a thought, but this doesn't do anything:
CODE

c1.xlocation+= 0; i++;        
               System.out.println("Car 1 has stopped at lights at X position " + c1.xlocation);
               break;

i++? i isn't used for anything, so incementing it does nothing.
you are adding nothing to c1.xlocation, so this is probably not needed either.
The break statement should end the loop, but sometimes it only jumps to an end } and not leave a for/while loop.

There must be a problem with the code, because the system.out will not affect the running of any other ocode.
User is offlineProfile CardPM
+Quote Post

poleman
RE: While And If
25 Mar, 2007 - 11:01 AM
Post #5

New D.I.C Head
*

Joined: 24 Mar, 2007
Posts: 6


My Contributions
CODE

public static void main(String[] args)
    {
        Road h1 = new Road ("H1", 0, 600, 50, 100);             //create instances of horizontal road
        Road h2 = new Road ("H2", 0, 600, 150, 200);
        Road h3 = new Road ("H3", 0, 600, 250, 300);
        Road v1 = new Road ("V1", 50, 100, 0, 400);             //create some vertical roads
        Road v2 = new Road ("V2", 150, 200, 0, 400);
        Road v3 = new Road ("V3", 250, 300, 0, 400);
        Vehicle c1 = new Car ("Car 1", 5, 0, 52);       //test car designed to travel west to east along h1
        Vehicle c2 = new Car ("Car 2", 5, 52, 0);          //test car north to south v1
        Vehicle b1 = new Bike ("Bike 1", 15, 20, 51);   //test bike
        Lights j11 = new Lights ("Set 1", false, false, false, true, false, false, false, false); //lights at junction 11
        j11.setStateAgreen(false);                      //initialise state of horizontal lights to not green
        j11.setStateBgreen(false);                      //initialise state of vertical lights to not green
        
        c1.xlocation+=((Vehicle)c1).getSpeed();
        
        while (c1.getXLocation()<=600)
        {          
            if (j11.getStateAgreen() ==false && ((Vehicle)c1).getXLocation()<=50 && ((Vehicle)c1).getXLocation()>=45)
            {              
               System.out.println("Car 1 has stopped at lights at X position " + ((Vehicle)c1).getXLocation());
            }
            else if (j11.getStateAgreen() ==true)
            {
               c1.xlocation+=((Vehicle)c1).getSpeed();
            }
            else
            break;
        }
    }


When I run this it now breaks straight away. Cannot work out why as my method should get the vehicle moving, and it shoudl then reach a position where it stops at the lights. ie 1 of the conditions should be met!

Any ideas anyone?

This post has been edited by poleman: 25 Mar, 2007 - 11:03 AM
User is offlineProfile CardPM
+Quote Post

DilutedImage
RE: While And If
25 Mar, 2007 - 06:31 PM
Post #6

D.I.C Addict
Group Icon

Joined: 20 Nov, 2006
Posts: 643



Thanked: 6 times
Dream Kudos: 25
My Contributions
What is output when you place this before your while loop?
CODE
System.out.println(c1.getXLocation() + ", " + j11.getStateAgreen() + ", " + ((Vehicle)c1).getXLocation());

??
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 05:46PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month