0

I'm currently writing this program that I require to read info from a text file and to then compare the info read to a user input and output a message saying if it was a match or not.

Currently have this. The program is sucessfully reading the data specified but I can't seem to compare the strings correctly at the end and print a result.

Code is below any help would be greatly appreciated.

import java.util.Scanner;      // Required for the scanner
import java.io.File;               // Needed for File and IOException 
import java.io.FileNotFoundException; //Required for exception throw

// add more imports as needed

/**
 * A starter to the country data problem.
 * 
 * @author phi 
 * @version starter
 */
public class Capitals
{
    public static void main(String[] args) throws FileNotFoundException // Throws Clause Added
    {
        // ask the user for the search string
        Scanner keyboard = new Scanner(System.in);
        System.out.print("Please enter part of the country name: ");
        String searchString = keyboard.next().toLowerCase();

        // open the data file
        File file = new File("CountryData.csv");

        // create a scanner from the file
        Scanner inputFile = new Scanner (file);

        // set up the scanner to use "," as the delimiter
        inputFile.useDelimiter("[\\r,]");

        // While there is another line to read.
        while(inputFile.hasNext())
        {
            // read the 3 parts of the line
            String country = inputFile.next(); //Read country
            String capital = inputFile.next(); //Read capital
            String population = inputFile.next(); //Read Population

            //Check if user input is a match and if true print out info.
            if(searchString.equals(country))
            {
                System.out.println("Yay!");
            }
            else
            {
                System.out.println("Fail!");
            }
        }

        // be polite and close the file
        inputFile.close();
    }
}

3 Answers 3

1

You should try reading the input from a textField in an user interface(visible window) where the user puts the country and getting that as raw input shortens the code.(Only if you have a visible window on screen)

I don't have that good experience with scanners, because they tend to crash my applications when I use them. But my code for the same test does only include a scanner for the file which does not crash my application and looks like following:

    Scanner inputFile = new Scanner(new File(file));

    inputFile.useDelimiter("[\\r,]");
    while (inputFile.hasNext()) {
        String unknown = inputFile.next();
        if (search.equals(unknown)) {
            System.out.println("Yay!");
        }
    }

    inputFile.close();


I think the easiest way to compare string against a file is to add a visible window where the user types the country, and reading the input to a string with String str = textField.getText();

Sign up to request clarification or add additional context in comments.

Comments

0

I am guessing that your comparison is failing due to case-sensitivity.

Should your string comparison not be CASE-INSENSITIVE?

Comments

0

There are a few possible issues here. First, you're converting the searchString to lower case. Are the data in the CSV also lower case? If not, try using equalsIgnoreCase instead. Also, it seems to me like you should be able to match parts of the country name. In that case, equals (or equalsIgnoreCase) would only work if the user inputs the complete country name. If you want to be able to match only a part, use contains instead.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.