0

Hi I have a robot which I need to tell it to move a certain number of times forward by saying forward 5. I have the method, I just need to get it to work in my class. Here is the method:

public void moveNumOfTimes(int num)
   {
    //-----------------------------------------------------------------------------------------------
       int i=0;

       while(i<num)    {
        if (this.frontIsClear()){ // if the front is NOT clear the robot should not move, otherwise will collide into the wall
             this.move();
        }
        i++; // same as i=i+1;
    }
     //-----------------------------------------------------------------------------------------------
   }

How do I enter that in my program? Is it like this?

moveNumOfTimes(int num);

Hope someone can help. Thanks

dr0i
  • 2,380
  • 2
  • 19
  • 36
Tom
  • 13
  • 5
  • 1
    You call it, the same way you call the `fontIsClear()` or `move()` methods, except this time you specify the number of times as an argument (e.g. `moveNumOfTimes(5);`). If you have problems with this, you should read Java tutorials on the web before moving on with your project. – Laf Feb 23 '16 at 15:54
  • There are lots of good tutorials out there. It looks like you want to read from the command line. Give this article a read: http://alvinalexander.com/java/edu/pj/pj010005 – Joe Feb 23 '16 at 15:58
  • I understand that but I am meant to enter it in the command line to tell it to run five times instead of inputting it every time I want it to go forward a certain number of steps. – Tom Feb 23 '16 at 16:01
  • @Tom do you know how to use the Scanner class? – Kyle Feb 23 '16 at 17:05

2 Answers2

0

You should use a Scanner object to take input from the console.

Scanner sc = new Scanner(System.in);

You can implement like this.

class Robot{

public static void main(String args[]){

Scanner sc = new Scanner();
int numOfSteps = sc.nextInt(); //This line takes a integer input from the user

YourClassName r = new YourClassName();
//DO any initialization operations here

r.moveNumOfTimes(numOfSteps); 

//Post movement operations come here

}

You can learn more on scanner here

Aditya Desai
  • 415
  • 1
  • 6
  • 14
0

How do I enter that in my program? Is it like this?

moveNumOfTimes(int num);

Yes, you could use something similar to this if you were trying to pass a command in from another method call. Something like:

public void Control(int moveNumber) {
    ... some other code, do some other stuff...
    moveNumOfTimes(moveNumber); //Here you are passing a parameter value to your original method;
}

Or you could control it directly with another method like:

public void moveFive() {
    moveNumOfTimes(5);
}

More likely, however, you wouldn't want to hardcode a method but rather call your original method directly through your Main method.

public static void main(String [ ] args) {
   Robot r = new Robot();
   r.moveNumOfTimes(5); //Here you have moved your new robot!
}

And if you really want to get fancy, look into working with the System and Scanner classes so you can prompt your user to tell the robot how much to move:

 public static void main(String [ ] args) {
         Robot r = new Robot();
         Scanner reader = new Scanner(System.in);
         System.out.println("How far should the robot move?"); //Output to the console window
         int input = reader.nextInt();  //Reads the next int value
         r.moveNumOfTimes(input); //Calls your method using the scanned input
 }