I have been trying to compare a string to another, or a string to a statement, but I can't find where the mistake is.
I tried with the ==
but it didnt work, then i changed to .equals()
static Scanner scan = new Scanner(System.in);
public static void main(String[] args) {
System.out.print("En este programa calcularemos el area de una figura.\n" +
"Ingresa a continuacion que figura calcularemos.");
String triangulo = "triangulo";
String figura = scan.nextLine();
if (figura.equals(triangulo)){
System.out.print("Ingrese base y altura del triangulo.\n");
double base = scan.nextDouble();
double altura = scan.nextDouble();
System.out.print("La altura es " + (base*altura)/2);
}
}
The idea here is to ask the user the name of the shape whose area will be calculated, and if it equals one of the different shapes I'm going to name, then it will use formula depending on the shape.
Replace this line: String figura = scan.nextLine();
with String figura = scan.next();
and your program will work: see image below
Also see here the difference between Scanner.next() and Scanner.nextLine()