Read Text File Array to Check Value

I created a program that has an array of values and asks the user to enter a value. It then checks the user input against the array values and displays if it's valid or not. The second part of the problem that I'm having trouble with is essentially doing the same thing, but reading the same values from a text file.

Any help is very much appreciated!

Here's my code for the first problem:

import java.util.Scanner;

public class ChargeAccount2 
{

public static void main(String[] args) 
{
    int results;
    int accountNum;

    int[] values={5658845,8080152,1005231,4520125,4562555,6545231,7895122,5552012,3852085,8777541,5050552,7576651,8451277,7825877,7881200,1302850,1250255,4581002};

    Scanner keyboard = new Scanner(System.in);
    System.out.println("Enter a charge account #:");
    accountNum = keyboard.nextInt();

    results = ChargeAccountSearchArray.sequentialSearch(values,accountNum);

    if (results == -1)
    {
        System.out.println(accountNum + " is not valid.");
    }

    else
    {
        System.out.println(accountNum + " is valid.");
    }

}

}

And here's the SearchArray program:

public class ChargeAccountSearchArray {
    public static int sequentialSearch(int[] array, int value) {
        int index, element;
        boolean found;

        index = 0;
        element = -1;
        found = false;

        while (!found && index < array.length) {
            if (array[index] == value) {
                found = true;
                element = index;
            }
            index++;
        }

        return element;
    }
}

Answers


You can use that scanner on a file too!

File file = new File(filename);
Scanner scanner = new Scanner(file);

Assuming that your input appears in a file, one on each line you'd scan the file line by line with the scanner and process the input in the same manner that you were doing before.

while (scanner.hasNextLine()) {

    String input = scanner.nextLine();
    Integer i = new Integer(input); 
    //process input here

    results = ChargeAccountSearchArray.sequentialSearch(values,accountNum);

    if (results == -1)
    {
       System.out.println(accountNum + " is not valid.");
    }

    else
    {
        System.out.println(accountNum + " is valid.");
    }     
}
scanner.close(); //when you're finished

Also, be sure to use scanner.nextLine() in place of scanner.nextInt(). The advantage of this is that it makes finding the end of the file very easy. The difference between the two is that nextLine() gets a whole line, which you can then chop up as you need (probably using String.split(","), and nextInt() gets only the next token.


Need Your Help

Cannot find symbol when replacing deprecated motionPressure to MotionEvent in Processing 2.0.3

android processing motionevent

I am following a book on "Rapid Android Development" with Processing 2.0.3 windows 7 64bit, having only api level 10 installed (android 2.3.3).

gsutil notification watchbucket returns "BadRequestException: 400 No valid domain for WebHook callback: https://https://..."

google-app-engine google-cloud-storage

Following the instructions in https://cloud.google.com/storage/docs/object-change-notification#_Authorize_Endpoint I'm trying to set a watchbucket notification.