Author Topic: Help me with Java.  (Read 353 times)

Code: [Select]
import java.util.Scanner;

public class CoordSpace {
// Defining class "Point"
public static class Point {
int x;
int y;
int z;
String name;
// Creating void method called "translate" used to add to the x, y, and z variables
void translate(int px, int py, int pz) {
x += px;
y += py;
z += pz;
}
// Creating void method called "coordinates" used to print the Point's current location
void coordinates() {
String orderedTriple = "(" + x + ", " + y + ", " + z + ")";
System.out.println(orderedTriple);
}
}
// Defining method "InputLine"
public static void InputLine() {
// Prompting the user to input a command.
System.out.println("Please input command. Type 'help' to see a list of commands.");
// Assigning user input to string "command"
Scanner input = new Scanner(System.in);
String command = input.nextLine();
if (command == "help") {
System.out.println("add -- Adds a point to the space.");
System.out.println("remove -- Removes a point from the space");
System.out.println("list -- Lists all points");
System.out.println("exit -- Exits the program");
} else if (command == "add") {
System.out.println("Please enter the name of the newly created point");
command = input.nextLine();
// PLACEHOLDER create a point and set variable name to the previous input
System.out.println("Please enter x axis coordinate for this point");
command = input.nextLine();
// PLACEHOLDER add an x coordinate to the previously mentioned point
System.out.println("Please enter y axis coordinate for this point");
command = input.nextLine();
// PLACEHOLDER add a y coordinate to the previously mentioned point
System.out.println("Please enter z axis coordinate for this point");
command = input.nextLine();
// PLACEHOLDER add a z coordinate to the previously mentioned point.
System.out.println("Your point has been added to the space");
} else {
System.out.println("Invalid command");
}
}
// Defining main method
public static void main(String[] args) {
// Creating new Point called "origin" that lies at the coordinates (0, 0, 0)
Point origin = new Point();
origin.x = 0;
origin.y = 0;
origin.z = 0;
origin.name = "Origin";
InputLine();
}
}
Okay, so I made this little class called CoordSpace that is supposed to keep a database of points with three coordinates and a name. The problem that I'm having is that when I tested it, both the "help" and "add" commands return the "invalid command" output. But when I have it output the command variable to the console, it outputs the last input just like it should. I don't know what the problem is here, and I've been looking around the internet for a solution and haven't found anything.

I have a feeling that I'm just being stupid and forgot something important.


With strings, you need to use the .equals() method.

if (command.equals("help")) {

The reason for this is that strings in Java are objects, not values. When using the equality symbol '==', you're checking if your string and the other string you're creating and comparing it to are the same objects, which will almost never be true.