Hello good I have the following code
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
enum Perros
{
Mastin, Terrier, Buulldog, Pekines, Caniche, Galgo;
public int length() {
return 0;
}
}
public class PROG02_EJER6 {
public static void main(String[] args) {
try (Scanner sc = new Scanner(System.in)) {
String intro;
int Charlong;
Perros cont;
Map<String,Integer> map = new HashMap<>();
map.put("Mastin",1);
map.put("Terrier",2);
map.put("Bulldog",3);
map.put("Pekines",4);
map.put("Caniche",5);
map.put("Galgo",6);
Perros var1, var2;
var1 = Perros.Mastin;
var2 = Perros.Terrier;
System.out.println(var1.compareTo(var2));
System.out.println("la posicion de la variable 1 es "+ var1.ordinal());
System.out.println("la posicion de la variable 2 es "+ var2.ordinal());
System.out.println((Perros.values().length));
System.out.println("Introduce el nombre de la raza de la que quieras saber informacion (Mastin, Terrier, Buulldog, Pekines, Caniche, Galgo)");
intro = sc.next();
if(map.containsKey(intro));
map.get(intro);
cont = Perros.valueOf(intro);
Charlong = cont.length();
System.out.println(Charlong);
System.out.println("Esa raza se encuentra en la posicon "+map.get(intro)+ " Ademas ese elemento de la enumeracion contiene "+Charlong);
}
}
}
I would like that in the if part:
System.out.println("Introduce el nombre de la raza de la que quieras saber informacion (Mastin, Terrier, Buulldog, Pekines, Caniche, Galgo)");
intro = sc.next();
if(map.containsKey(intro));
map.get(intro);
cont = Perros.valueOf(intro);
Charlong = cont.length();
System.out.println(Charlong);
System.out.println("Esa raza se encuentra en la posicon "+map.get(intro)+ " Ademas ese elemento de la enumeracion contiene "+Charlong);
Get both the position within the enumeration of the element typed by keyboard and the number of characters that element has from the enumeration:
#Ejemplo: enum = hola, adios, chao
#Entrada escribe el elemento del que quieras saber la posicion en la enumeracion y el numero de caracteres que tiene: hola
#Salida Hola se encuentra en la posicion 1 y tiene 4 caracteres
I have been trying different commands as you can see in the code but it doesn't come out just for me since the output of my program in the position part works fine for me but in the number of characters part for some reason I don't know the variable which counts the characters is equal to 0 Thank you very much in advance <3
As Pablo mentions, you must take care of the syntax of the conditionals in your code if you declare a
if
or aelse
to obtain the number of characters depending on the entered race, you can make use ofname()
the method which returns the name of the enumeration constant as declared in your enum declaration.