Hello All !

I'm trying to write a Robot Class who have several methods.
One of its methods is move() that move the robot the direction his looking at, one step ahead.

I'm getting an error on this:

switch (Direction) {

If some one can help me find the mistake, and help me improved my code, I will be more than happy !!

My code:

import java.awt.*;
public class Robot {

	private int roboId;
	private Point location;
	public static enum Direction {UP,DOWN,RIGHT,LEFT}
	private Direction dir;	// creates an object for the constructor
	public static final int DX=1;
	public static final int DY=1;
	public static final int NO_MOVEMENT=0;
	
	public Robot (int id, Point location, Direction dir){
		
		this.roboId = id;	// initialize roboID
		
		if ((location.getX() <0)||(location.getY()<0))	// initialize Point location
			this.location.setLocation(0,0);	// in case (x,y) negative coordinate, set (0,0).
		else
			this.location.setLocation(location.getX(),location.getY());

		this.dir = dir;		// initialize Direction
	}//Robot constructor

public int getId(){	//returns robot ID#
	return roboId;
}

public Point getLocation(){	//returns robot (x,y) location
	return location;		
}

public Direction getDir(){	//returns robot direction
	return dir;
}

public void move(){
	switch (Direction) {

            case Direction.RIGHT: location.translate(DX, NO_MOVEMENT); break;

            case Direction.UP: location.translate(NO_MOVEMENT, -DY); break;

            case Direction.LEFT: location.translate(-DX, NO_MOVEMENT); break;

            case Direction.DOWN: location.translate(NO_MOVEMENT, DY); break;

            default: System.out.println("Error in move(int)!");

        }
}

}//class Robot

Direction is the enumeration class. You need to switch on a variable that holds the direction.

Direction is the enumeration class. You need to switch on a variable that holds the direction.

You mean like this:

switch (dir) {

            case dir.RIGHT: location.translate(DX, NO_MOVEMENT); break;

            case dir.UP: location.translate(NO_MOVEMENT, -DY); break;

            case dir.LEFT: location.translate(-DX, NO_MOVEMENT); break;

            case dir.DOWN: location.translate(NO_MOVEMENT, DY); break;

            default: System.out.println("Error in move(int)!");

        }

??

Because now the ' switch (dir){ ' is OK but I'm getting an error on every line with this:

case dir.RIGHT: location.translate(DX, NO_MOVEMENT); break;

            case dir.UP: location.translate(NO_MOVEMENT, -DY); break;

            case dir.LEFT: location.translate(-DX, NO_MOVEMENT); break;

            case dir.DOWN: location.translate(NO_MOVEMENT, DY); break;

The error is on the 'dir' and it says: "Multiple markers at this line
- Duplicate case
- The field Robot.dir cannot be referenced from an enum case label; only enum constants can be used in enum
switch
- The static field Robot.Direction.RIGHT should be accessed in a static way".

What does it means, and how do I solve this ?

thnx...

You use a variable for the switch, but you need the constants for the cases, as in

switch (dir) {
  case Direction.RIGHT: ...

You use a variable for the switch, but you need the constants for the cases, as in

switch (dir) {
  case Direction.RIGHT: ...

I did this:

public void move(){
    switch (dir) {
     
    case Direction.RIGHT: location.translate(DX, NO_MOVEMENT); break;
     
    case Direction.UP: location.translate(NO_MOVEMENT, -DY); break;
     
    case Direction.LEFT: location.translate(-DX, NO_MOVEMENT); break;
     
    case Direction.DOWN: location.translate(NO_MOVEMENT, DY); break;
     
    default: System.out.println("Error in move(int)!");
     
    }
    }

But it still gives me the following error for the first 4 case lines:

"The qualified case label Robot.Direction.RIGHT must be replaced with the unqualified enum constant RIGHT"...

But even with "case Robot.Direction.RIGHT: location.translate(DX, NO_MOVEMENT); break;"
it still says the same...

Did you try following the instruction in the error message and changing it to

switch (dir) {
  case RIGHT:

Done.
thnx !

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.