Update: After applying improvements suggested by @user27158 (thank you) i have come across another issue when i come to run the program.
An error pops up and essentially stops the program from continuing. After looking into this, I cannot figure out what the issue is. Once again, i am new to programming and it is likely that i am just missing something completely simple.
Error Message:
Exception in thread "main" java.lang.UnsupportedOperationException: Not supported yet. at country.game.EuropeanCountries.EuropeanCountriesList(EuropeanCountries.java:17) at country.game.Main.main(Main.java:36) Java returned: 1 BUILD FAILED (total time: 12 seconds)
The Error occurs at the line:
List<String> euCountries = EuropeanCountries.EuropeanCountriesList();
Main Class:
package country.game;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Main {
public static List<String> EuropeanCountriesList() {
List<String> EuropeanCountries = new ArrayList<>();
return EuropeanCountries;
}
public static void main(String[] args) {
boolean Running = true;
List<String> UsedCountries = new ArrayList<>();
Scanner Reader = new Scanner(System.in);
while(Running == true){
System.out.println("Pick a Country: ");
String UserChoice = Reader.next();
List<String> euCountries = EuropeanCountries.EuropeanCountriesList();
if(!euCountries.contains(UserChoice)){
System.out.println("That is not a valid country");
} else {
if(UsedCountries.contains(UserChoice)) {
System.out.println("Sorry you have already used that country");
} else {
System.out.println("Correct! That Country is in Europe!");
UsedCountries.add(UserChoice);
}
}
}
}
}
"Europe" class
package country.game;
import java.util.Arrays;
import java.util.List;
public class Europe {
private static final List<String> EuropeanCountries = Arrays.asList(
new String[]{
"Albania",
"Andorra",
"Austria",
"Belarus",
"Belgium",
"Bosnia and Herzegovina",
"Bulgaria",
"Croatia",
"Czechia",
"Denmark",
"England",
"Estonia",
"Finland",
"France",
"Germany",
"Greece",
"Hungary",
"Iceland",
"Ireland",
"Italy",
"Kosovo",
"Latvia",
"Liechtenstein",
"Lithuania",
"Luxembourg",
"Malta",
"Moldova",
"Monaco",
"Montenegro",
"Netherlands",
"Northern Ireland",
"North Macedonia",
"Norway",
"Poland",
"Portugal",
"Romania",
"San Marino",
"Scotland",
"Serbia",
"Slovakia",
"Slovenia",
"Spain",
"Sweden",
"Switzerland",
"Turkey",
"Ukraine",
"Vatican City",
"West Russia",
}
);
public static List<String> EuropeanCountriesList(){
return EuropeanCountries;
}
}
Any Help would be greatly appreciated!
To start with, there a few a few improvements that you should make in the code;
List<String> UsedCountries = new ArrayList<>();
The fix: How to access the arraylist from your other class
public static List<String> EuropeanCountriesList() {
// declaring as List as it is best to not tie yourself to a specific implementation
List<String> EuropeanCountries = new ArrayList<>();
...
return EuropeanCountries;
}
then
String UserChoice = Reader.next()
List<String> euCountries = EuropeanCountries.EuropeanCountriesList();
...
The above code allows you to use the arraylist in your current code. I would suggest a few further improvements though
List<String> euCountries = EuropeanCountries.EuropeanCountriesList();
// step 1 - check if it is one of the acceptable countries
if (!euCountries.contains(UserChoice)) {
System.out.println("That is not a valid country");
} else {
// step 2 - check if it has already been selected
if (UsedCountries.contains(UserChoice)) {
System.out.println("Sorry You Have Already Used That Country");
} else {
System.out.println("Correct! That Country is in Europe!");
UsedCountries.add(UserChoice);
}
}
public class Europe {
private static final List<String> EuropeanCountries = Arrays.asList(
new String[]{
"Albania",
"Andorra",
...
}
);
public static List<String> EuropeanCountriesList() {
return EuropeanCountries;
}
}
Hope this all helps
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.