2

Working on a java program that reads a txt file with a bunch of account numbers. User enters account number if it matches the programs responds saying its a valid account number if its not it responds invalid. Here is my class that opens the file and checks to see if it is a valid number. When I run the code it always comes back saying the account number is invalid even though the account number I am testing is valid.

public class Validator {

    public boolean isValid(int number) throws IOException {


        final int SIZE = 17;
        int[] numbers = new int[SIZE];
        boolean found = false;
        int index = 0;


        File file = new File("src/Accounts.txt");
        Scanner inputFile = new Scanner(file);

        while (inputFile.hasNext() && index < numbers.length) {

            numbers[index] = inputFile.nextInt();
            index++;

        }

        while (!found && index < SIZE) {

            if (numbers[index] == (number))
                found = true;
            else
                index++;
        }
        return found;
    }
}
6
  • What numbers are in your txt-File? Are they of fixed lenght? Commented May 5, 2015 at 20:02
  • How is your Accounts.txt looks like? Commented May 5, 2015 at 20:03
  • Not an answer to your question, but I'd suggest you avoid reading in the file every single time you validate a number. Store the contents in-memory, use those stored values, maybe provide some sort of reset() method that reloads the file. Commented May 5, 2015 at 20:11
  • Would I load it into an array list? Commented May 5, 2015 at 20:15
  • @Nik I noticed below you said you have to use an array, I just wanted to point out that an ArrayList is not an array. An ArrayList uses arrays but I wouldn't consider it okay if you've been told you have to use an array. I'd have to see the rules to be sure though. Commented May 5, 2015 at 20:23

4 Answers 4

1

you should reset index back to 0 before your second loop. Otherwise, the second loop won't be entered, since the first loop already incremented index past the index of the last account number read from the file.

while (inputFile.hasNext() && index < numbers.length) {

    numbers[index] = inputFile.nextInt();
    index++;

}

index = 0; // add this

while (!found && index < SIZE) {

    if (numbers[index] == (number))
        found = true;
    else
        index++;
}
return found;
Sign up to request clarification or add additional context in comments.

Comments

1

I have simplified your code a bit. You can go through the file and check at the same time.

public boolean isValid(int number) {
    try (Scanner scanner = new Scanner(new File("src/Accounts.txt"))) {
        while (scanner.hasNextInt()) {
            if (scanner.nextInt() == number) {
                return true;
            }
        }
    }

    return false;
}

1 Comment

I have to use an array for this problem. It's one of the requirements for the programming challenge.
1

I simplified your code too, but I found a possible problem in your parameter type for account numbers. Can your account bigger than "2,147,483,647"? (http://www.tutorialspoint.com/java/java_basic_datatypes.htm)

If the answer is yes, so you will have a problem.

The simplified code is below:

public boolean isValid(long number) {
    long accountNumber;

    File file = new File("src/Accounts.txt");
    Scanner inputFile = null;
    try {
        inputFile = new Scanner(file);
        while (inputFile.hasNext()) {
            accountNumber = inputFile.nextLong();
            if (accountNumber == number) {
                return true;
            }
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } finally {
        if (inputFile != null) {
            inputFile.close();
        }
    }
    return false;
}

Comments

0

After you solved your problem, you can try another elegant way using java 8:

public boolean isValid(int number) throws IOException{

        return Files.lines(Paths.get("src/Accounts.txt")).anyMatch(
                    num -> num.trim().equals(String.valueOf(number)));

}

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.