Hi i'm a student at first year.
We have the following assignment:
Create a class FavouriteCities.java that contains main method and enum
named MyFavouriteCities. It contains a list of cities ordered
according to your preference, such as BERLIN (i.e., 1st favourite),
NEW_YORK (i.e., 2nd favourite), SYDNEY (i.e., 3rd favourite), etc.;
the program should ask you to write a name of a city and then print
out the position of the city in your ranking list.
Enter a city:
New York
On the list of your cities, it is on position: 2.
We have to use the material studied so far (Chapters 1 to 3 of Java Software Solutions https://www.amazon.com/Java-Software-Solutions-John-Lewis/dp/0133594955) and I can't get a solution without using if statement.
Very glad if you helping me finding a solution.
I'm stuck after scan:
import java.util.Scanner;
public class FavouriteCities {
enum MyFavouriteCities{BERLIN, NEW_YORK, ROME, SAN_FRANCISCO}
public static void main(String [] args)
{
MyFavouriteCities city1, city2, city3, city4;
String city;
city1 = MyFavouriteCities.BERLIN;
city2 = MyFavouriteCities.NEW_YORK;
city3 = MyFavouriteCities.ROME;
city4 = MyFavouriteCities.SAN_FRANCISCO;
Scanner scan = new Scanner(System.in);
System.out.println("Enter a city:");
city = scan.nextLine();
city = city.toUpperCase();
city = city.replace(" ", "_");
System.out.println("On the list of your favourite cities, it is on position: "
+ .ordinal() +1);
}}
Thank you Erwin Bolwidt ! It works!
The solution I searched for is:
System.out.println("On the list of your favourite cities, it is on position: "
+ (MyFavouriteCities.valueOf(city).ordinal() +1));
Hi i'm a student at first year. We have the following assignment:
We have to use the material studied so far (Chapters 1 to 3 of Java Software Solutions https://www.amazon.com/Java-Software-Solutions-John-Lewis/dp/0133594955) and I can't get a solution without using if statement.
Very glad if you helping me finding a solution.
I'm stuck after scan:
Thank you Erwin Bolwidt ! It works! The solution I searched for is: System.out.println("On the list of your favourite cities, it is on position: " + (MyFavouriteCities.valueOf(city).ordinal() +1));