Thursday, 5 September 2013

Trying to use "if" with a user inputted string

Trying to use "if" with a user inputted string

I am new to java and my teacher probably knows as much as i do if not
less. I am trying to make a program where I enter the name of a shape and
it tells me how many sides it has. I cannot seem to get it to work. Can
anyone explain how to do this?
import java.util.Scanner;
public class Shapes {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
String shape;
System.out.print("Enter shape: ");
shape = reader.next();
if (shape == "hexagon") {
System.out.println("A hexagon has 6 sides.");
}
if (shape == "decagon"); {
System.out.println("A decagon has 10 sides.");
}
}
}

No comments:

Post a Comment