Brandon Woodruff
Brandon Woodruff

Reputation: 21

How do I get my program to read my txt file in Java?

The general function of this program is to extract all the information from the txt file, input it into an array, and then compare a user inputted ID to the ones inputted into the array and return either true or false. I wrote the program, but I got an InputMismatchException. When I put the function in a try/catch statement, it returns null when I run it.

This is my Validator class:

import java.util.Scanner;
import java.io.*;

public class Validator
{
    private int[] valid = new int[18];

    public Validator(String filename) throws IOException
    {
        try
        {
            File file = new File(filename);
            Scanner inputFile = new Scanner(file);
            int index = 0;

            while (inputFile.hasNext())
            {
                valid[index] = inputFile.nextInt();

                index++;
            }

            inputFile.close();
        }

        catch (Exception e)
        {
            System.out.println(e.getMessage());
        }
    }


    public boolean isValid(int number)
    {
        int index = 0;
        boolean found = false;

        while (!found && index < valid.length)
        {
          if (valid[index] == number)
          {
              found = true;
          }

          index++;
        }

        return found;
    }
}

As for the main method:

import java.util.Scanner;
import java.io.*;

public class ChargeAccountModification
{
    public static void main(String[] args) throws IOException
    {
        int number;

        Scanner keyboard = new Scanner(System.in);

        System.out.println("Brandon Woodruff    12/3/16");

        System.out.print("Enter your charge account number: ");
        number = keyboard.nextInt();

        Validator validator = new Validator("AccountNumbers.txt");

        if (validator.isValid(number) == true)
        {
            System.out.println("That's a valid account number.");
        }

        else
        {
            System.out.println("That's an INVALID account number.");
        }
    }
}

Finally here is the txt information. The txt file is called AccountNumbers.txt by the way.

5658845 4520125 7895122 8777541 8451277 1302850 8080152 4562555 5552012 5050552 7825877 1250255 1005231 6545231 3852085 7576651 7881200 4581002

They actually each appear on their own line in a list, but I can't seem to get it to display like that.

Upvotes: 0

Views: 127

Answers (2)

davidxxx
davidxxx

Reputation: 131496

In this code :

    while (inputFile.hasNext()) {
        valid[index] = inputFile.nextInt();

        index++;
    }

Try to replace hasNext() by hasNextInt() :

    while (inputFile.hasNextInt()){
        valid[index] = inputFile.nextInt();

        index++;
    }

Otherwise it reads a whitespace and a whitespace is not a number.

If it doesn't work, you could also use a delimiter with a whitespace :

Scanner s = new Scanner(file).useDelimiter("\\s");

Upvotes: 1

hopeIsTheonlyWeapon
hopeIsTheonlyWeapon

Reputation: 567

Please provide stack trace with you input. I tried the same exact program with 5658845 . I got the below response

Brandon Woodruff 12/3/16 Enter your charge account number: 5658845 That's a valid account number.

But If I am entering "CDCD". I am getting your exception. So please check you input. It is the Scanner that is throwing this exception

Brandon Woodruff    12/3/16
Enter your charge account number: CDCD
Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Scanner.java:864)
    at java.util.Scanner.next(Scanner.java:1485)
    at java.util.Scanner.nextInt(Scanner.java:2117)
    at java.util.Scanner.nextInt(Scanner.java:2076)
    at ChargeAccountModification.main(ChargeAccountModification.java:15)

Upvotes: 0

Related Questions