David Micallef
David Micallef

Reputation: 13

Compare input to a specific line saved in a text file JAVA

In order to validate a country entered by the user, I'm trying to have that country input compared to a list of countries stored in a text file. If the input matches a country stored in the text file, the validCountry would be set to 'true' and the program would be able to proceed.This is what I've got so far:

    Scanner sc = new Scanner (System.in);
    String country = "";
    boolean validCountry = false;

    while (!validCountry)
    {
        System.out.print("Country: ");
        String countryIn = sc.next();
        try{
            Scanner scan = new Scanner(new File("countries.txt"));
            while (scan.hasNext()) {
                String line = scan.nextLine().toString();
                if(line.contains(countryIn))
                {
                    country = line; 
                    validCountry = true;

                }
            }
        }catch(Exception e)
        {
            System.out.print(e);
        }
    }      

The above simply loops for me to re-input the country (implying that it is invalid).

This is what the countries.txt file looks like (obviously contains all the countries of the world not just the first few starting with 'A' :

Afghanistan
Albania
Algeria
American Samoa  
Andorra 
Angola  
Anguilla
...

I'm sure it's a very simple and minor error which I can't seem to find; but I've been trying to detect it for a while but to no avail. I've checked multiple other stackoverflow answers but they didn't seem to work either. I truly appreciate any form of help :)

Please let me know if my question needs further clarification.

Upvotes: 0

Views: 799

Answers (3)

David Micallef
David Micallef

Reputation: 13

Problem solved, the countries.txt file I had was encoded in UNICODE. All I had to do was change it to ANSI.

Upvotes: 0

D. Campa
D. Campa

Reputation: 61

Assuming that in String countryIn = sc.next(); the sc is a scanner that use System.in, change the .next() into nextLine():

String countryIn = sc.nextLine();

then, you should also change if(line.contains(countryIn)) because it will return true even if the given line is a substring of a country (afg will be found in afghanistan even though afg is not in the country list. use equalsIgnoreCase instead:

if (line.equalsIgnoreCase(countryIn)) {
...
}

Try if this class works:

import java.util.Scanner;
import java.io.File;

public class Country {

    public static void main(String[] args) {
        Scanner sc = new Scanner (System.in);
        String country = "";
        boolean validCountry = false;

        while (!validCountry)
        {
            System.out.print("Country: ");
            String countryIn = sc.nextLine();
            try{
                Scanner scan = new Scanner(new File("countries.txt"));
                while (scan.hasNext()) {
                    String line = scan.nextLine();
                    if(line.equalsIgnoreCase(countryIn))
                    {
                        country = line; 
                        validCountry = true;
                        break;
                    }
                }
            }catch(Exception e)
            {
                e.printStackTrace();
            }
        }   
    }
}

Upvotes: 1

user7291698
user7291698

Reputation: 1990

I tested the code and it works for me. I initialized your variable sc like this:

Scanner sc = new Scanner(System.in);

Note that it would be better to load the file outside the while loop (for better performance)

Upvotes: 1

Related Questions