简体   繁体   中英

How do I take a user input and search through an arraylist in a different class for the user input?

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;

  1. Running is never incremented so your while loop will never end
  2. You only want to initialise the scanner once, so do it outside of the loop
  3. You are trying to compare a String to an int
  4. Your list 'UsedCountries' is generic. That's never a good idea as it doesn't restrict which types you can put into them which means you may have errors that only show up at runtime. It should be declared as List<String> UsedCountries = new ArrayList<>();
  5. Variable and method names should start with lowercase https://www.oracle.com/technetwork/java/codeconventions-135099.html

The fix: How to access the arraylist from your other class

  • return it from your method
  • work on the returned list
    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

  1. Once you have your list, just use the contains method instead of the for loops
    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);
        }
    }
  1. Instead of creating your list of countries every time that method is called, declare it once and return the stored list instead
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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM