javacountfinch

Java counting obstacles


I am making a program for a robot that will count how many times it hits an obstacle for the seconds that is running. I want to know what can I use to count the times.

For example;

if (robot.isObstacle);

start counting every time isObstacle occurs and stop when there is no obstacle.

Finch fRobot = new Finch();


    fRobot.setWheelVelocities(250,250);
    long before = System.currentTimeMillis();
    while (System.currentTimeMillis() - before < msFinch*1000)



     if(fRobot.isObstacle() == true);{
         System.out.println(obstacleCount);
         obstacleCount++;

     }

         if(fRobot.isObstacle() == false);{
         System.out.println("No Obstacles hit");
     }

This doesn't seem to work I know I have done something wrong.


Solution

  • Your While loop seems fishy. You probably want to start a new block there.

    while (System.currentTimeMillis() - before < msFinch*1000) {
    

    Your if syntax is wrong. Remove the ; after the condition:

     if(fRobot.isObstacle() == true) {
         System.out.println(obstacleCount);
         obstacleCount++;
    
     }
    
         if(fRobot.isObstacle() == false) {
         System.out.println("No Obstacles hit");
     }