Scanner For Console Input Error

566 views Asked by At

Given the following as console input:

F 6 100 

I want the 6 to be assigned to the duration, and the 100 assigned to speed:

if(command.equalsIgnoreCase("F"))
myf.setWheelVelocities(speed,speed,duration);

The above code does not seem to work, however. The full code is below: how do I correctly assign the console input to the setWheelVelocities method parameters of the Finch robot?

import edu.cmu.ri.createlab.terk.robot.finch.Finch;
import java.util.Scanner;
public class CS1810 {

    public static void main(String args[])
    {
        Scanner scan = new Scanner(System.in);
        Finch myf = new Finch();

        // Welcome Message
        System.out.println("Welcome to Finch Robot Remote Navigation!!!");
        System.out.println();

        // Instructions
        System.out.println("Instructions on how to navigate and commands to be used:");
        System.out.println("To move forward type in 'F' followed by a space and then enter an integer representing 'duration' followed by a space and then enter an integer representing 'speed'.");
        System.out.println("To move left or right type in 'L' or 'R' followed by a space and then enter an integer representing 'duration' followed by a space and then an integer representing 'speedLeft' followed by a space and then an integer representing 'speedRight'.");
        System.out.println("To backtrack type in 'B' followed by a space and then enter an integer representing 'track' which is the number of commands you want to backtrack.");
        System.out.println("To stop the program type in 'S'.");
        System.out.println();

        // Rules
        System.out.println("Rules for commands and values entered:");
        System.out.println("You can only enter 'F,L,R,B and S' as commands.");
        System.out.println("You can only enter a 'duration' value which is less than or equal to 6.");
        System.out.println("You can only enter a 'speed','speedLeft' and 'speedRight' value between -100 and 100.");
        System.out.println("You can only enter a 'backtrack' value equal to or less than the number of commands you have entered before:");
        System.out.println();

        //Enter Command
        System.out.println("Enter your command below:");
        String command = ("");
        int duration = scan.nextInt();
        int speed = scan.nextInt();

        // Ignore Cases
        while((command.equalsIgnoreCase("F"))||(command.equalsIgnoreCase("L"))||(command.equalsIgnoreCase("R"))||(command.equalsIgnoreCase("B"))||(command.equalsIgnoreCase("S")))
        {
           if(command.equalsIgnoreCase("F"))
           myf.setWheelVelocities(speed,speed,duration);
           System.out.println("Enter a new command");
           command = scan.nextLine();      
        }
        myf.quit();
     }
}
1

There are 1 answers

2
i3rd On BEST ANSWER

You have to split , and in this link provided is the answer how to split by whitespaces in strings : How do I split a string with any whitespace chars as delimiters?