0
import java.util.Scanner;

public class Main {

public static void main(String[] args) {


            Scanner scan = new Scanner(System.in);

            Turtle t = new Turtle();

            while (true){

                System.out.println("Enter a command:");
                String command = scan.nextLine();
                command.toLowerCase();

                //moves the turtle forward
                if (command.equals("forward"))
                {
                    //further prompts the user for the number of steps
                    System.out.print("Number of steps: ");
                    int i = scan.nextInt();

                    t.forward(i);
                }
                else if (command.equals("right")){

                    System.out.print("Number of degrees: ");
                    double d = scan.nextDouble();

                    t.right(d);
                }
                else if (command.equals("left")){
                    System.out.print("Number of degrees: ");
                    double d = scan.nextDouble();

                    t.left(d);
                }
                //else if (command.equals("setpencolor")){

                    //System.out.print("New color: ");
                    //String c = scan.nextLine();

                    //t.setPenColor(c);
            //  }
                else if (command.equals("quit")){
                    break;
                }
                else{
                    System.out.println("That is an invalid command.");
                }

            }

        }

    }

NEXT CLASS

public class Turtle {


public final int RADIUS = 5;

private double xCoord;
private double yCoord;
private double direction;
private boolean penDown;

public Turtle(){
    int canvasSize = 400;

    StdDraw.setCanvasSize(canvasSize, canvasSize);
    StdDraw.setXscale(0, canvasSize);
    StdDraw.setYscale(0, canvasSize);


    xCoord = canvasSize/2;
    yCoord = canvasSize/2;
    direction = 90;
    StdDraw.setPenColor(StdDraw.BLACK);
    penDown = false;

    StdDraw.filledCircle(xCoord, yCoord, RADIUS);
}

//converts degrees to radians
public double convertToRadians(double degree){
    return (degree*Math.PI)/180;
}

public void forward(int i){

    double stepSize = 20;

    //draws a turtle for each step 
    for (int j = 0; j < i; j++)
    {

        //draws a line connecting the turtles if penDown is true
        if (penDown==true)
            StdDraw.line(xCoord, yCoord, (j*stepSize*Math.cos(convertToRadians(direction))+xCoord), (j*stepSize*Math.sin(convertToRadians(direction))+yCoord));

        xCoord = j*stepSize*Math.cos(convertToRadians(direction)+xCoord);
        yCoord = j*stepSize*Math.sin(convertToRadians(direction)+yCoord);
        StdDraw.filledCircle(xCoord, yCoord, RADIUS);
    }

}

//turns the turtle a degrees to the right
public void right(double a){
    direction -= a;
}

//turns the turtle a degrees to the left
public void left(double a){
    direction += a;
}

//makes it so a line will not be drawn between turtles
public void penUp(){
    penDown = false;
}

//makes it so a line will be drawn between turtles
public void penDown(){
    penDown = true;
}

This is my code I have and I am stuck on one thing. When you play the code it asks for the user input this is how it goes:

Enter a command:
left
Number of degrees:

But when I type in any number it just comes with

Enter a command: That is an invalid command.

I don't know what I am supposed to type for the degree to make it listen.

Jarvis
  • 8,020
  • 3
  • 23
  • 51
  • Side note: `command.toLowerCase();` just returns a `String` and does not alter `command` itself. Instead use `command = command.toLowerCase();`. – Jyr Feb 25 '17 at 01:48
  • no but when it is asking for the user input it says enter a command: then the person will type "left" and then it asks for ho many degrees you want to move the dot left. like this "Number of degrees:" and if they type in say "90" it comes up with "this is an invalid command" – jack.clearry Feb 25 '17 at 01:50
  • That is because you use `nextLine()`, instead use `next()`. – Jyr Feb 25 '17 at 01:54
  • Possible duplicate of [Scanner is skipping nextLine() after using next(), nextInt() or other nextFoo() methods](http://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-nextint-or-other-nextfoo) – Jyr Feb 25 '17 at 02:13

2 Answers2

0

As I noted in the comments, you should use next() instead of nextLine(). The problem is that nextInt() and nextDouble() do not consume the newline character, so on the next nextLine() method call the remainder gets consumed and obviously it will not correspond to any of your entries.

Ergo, either use next(), parse the integer using nextLine() or fire nextLine() after reading the integer.

So three possible solutions:

1:

String command = scan.next();

2:

if (command.equals("forward"))
{
    //further prompts the user for the number of steps
    System.out.print("Number of steps: ");
    int i = scan.nextInt();
    scan.nextLine();

    t.forward(i);
}

3:

if (command.equals("forward"))
{
    //further prompts the user for the number of steps
    System.out.print("Number of steps: ");
    int i = Integer.parseInt(scan.nextLine());

    t.forward(i);
}
Jyr
  • 623
  • 5
  • 16
0

Once you resolve your input issue, your next hurdle is your forward() method. Because you included j itself in the calculation two bad things happen: 1) the first step is multiplied by 0 so the turtle doesn't actually move; 2) the turtle accelerates instead of moving in a linear fashion.

Below is a rework of your code with forward() fixed as well as @Jyr's excellent suggestions regarding .next() vs. .nextLine() and saving the result of .toLowerCase() back into command. I've folded main() into the Turtle object to simplify this example code:

import java.util.Scanner;

public class Turtle {

    public final int RADIUS = 5;
    public final double STEP_SIZE = 20;
    public final int CANVAS_SIZE = 400;

    private double xCoord;
    private double yCoord;
    private double direction;
    private boolean penDown;

    public Turtle() {

        StdDraw.setCanvasSize(CANVAS_SIZE, CANVAS_SIZE);
        StdDraw.setXscale(0, CANVAS_SIZE);
        StdDraw.setYscale(0, CANVAS_SIZE);

        xCoord = CANVAS_SIZE / 2;
        yCoord = CANVAS_SIZE / 2;
        direction = 90;
        StdDraw.setPenColor(StdDraw.BLACK);
        penDown = false;

        StdDraw.filledCircle(xCoord, yCoord, RADIUS);
    }

    // converts degrees to radians
    public static double convertToRadians(double degree) {
        return (degree * Math.PI) / 180;
    }

    public void forward(int i) {

        double directionInRadians = convertToRadians(direction);

        // draws a turtle for each step 
        for (int j = 0; j < i; j++) {

            double new_xCoord = STEP_SIZE * Math.cos(directionInRadians) + xCoord;
            double new_yCoord = STEP_SIZE * Math.sin(directionInRadians) + yCoord;

            // draws a line connecting the turtles if penDown is true
            if (penDown) {
                StdDraw.line(xCoord, yCoord, new_xCoord, new_yCoord);
            }

            xCoord = new_xCoord;
            yCoord = new_yCoord;

            StdDraw.filledCircle(xCoord, yCoord, RADIUS);
        }
    }

    // turns the turtle a degrees to the right
    public void right(double angle) {
        direction -= angle;
    }

    // turns the turtle a degrees to the left
    public void left(double angle) {
        direction += angle;
    }

    // makes it so a line will not be drawn between turtles
    public void penUp() {
        penDown = false;
    }

    // makes it so a line will be drawn between turtles
    public void penDown() {
        penDown = true;
    }

    public static void main(String[] args) {

        Scanner scan = new Scanner(System.in);

        Turtle t = new Turtle();

        while (true) {

            System.out.print("Enter a command: ");
            String command = scan.next().toLowerCase();

            // moves the turtle forward
            if (command.equals("forward")) {
                // further prompts the user for the number of steps
                System.out.print("Number of steps: ");
                t.forward(scan.nextInt());

            } else if (command.equals("right")) {
                System.out.print("Number of degrees: ");
                t.right(scan.nextDouble());

            } else if (command.equals("up")) {
                t.penUp();

            } else if (command.equals("down")) {
                t.penDown();

            } else if (command.equals("left")) {
                System.out.print("Number of degrees: ");
                t.left(scan.nextDouble());

            } else if (command.equals("quit")){
                break;

            } else {
                System.out.println("That is an invalid command.");
            }
        }

        System.exit(0);
    }
}

USAGE

> java Turtle
Enter a command: forward
Number of steps: 5
Enter a command: right
Number of degrees: 120
Enter a command: forward
Number of steps: 5
Enter a command: right
Number of degrees: 120
Enter a command: forward
Number of steps: 5
Enter a command: quit
> 

OUTPUT

enter image description here

cdlane
  • 33,404
  • 4
  • 23
  • 63