I am trying to create a program that has a class with a method that accepts a charge account number as an argument. Then the method should determine if the account number is valid by comparing it to the list of accounts in a text file (Accounts.txt). Then method should return a boolean value of true if the account is found. The method should use sequential search of ArrayList to find the target account.
I am instructed to use the following as the header to the method :
public static boolean isValid(ArrayList AccountList, String target)
and the logic should follow as :
Read the Accounts.txt file into an ArrayList object.
Loop through the ArrayList and display all accounts.
Prompt user for target Account to search for.
Search for account using the method.
Display Account number Valid or not Valid message.
Test for both conditions.
I have completed a large portion of the code but keep getting errors no matter what I edit. This is my first java course and I'm still trying to fully learn how to trace errors and such. Any help will be greatly appreciated. Thanks in advance.
Code thus far: (with general comment tags. Errors will be typed in comment tags within brackets. i.e. // [ error message ] )
package accountNumber;
import java.util.Scanner;
import java.io.*;
import java.util.ArrayList;
public class AccountNumber{
public static void main(String[] args) throws IOException
{
ArrayList<String> accountList = new ArrayList<String>();
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter the filename: ");
String filename = keyboard.nextLine();
File file = new File(filename);
Scanner inputFile = new Scanner(file);
while (inputFile.hasNext())
{
String account = inputFile.nextLine();
accountList.add(account);
}
inputFile.close();
for (int index = 1; index < accountList.size(); index++)
{
System.out.println("Index: " + index + " Account: " +
accountList.get(index));
}
/** [ - Syntax error on token ")", ; expected
- Syntax error on token "(", ; expected
- Illegal modifier for parameter isValid; only final is permitted
- Syntax error on token ",", ; expected
- ArrayList is a raw type. References to generic type ArrayList<E> should be
parameterized ] */
public static boolean isValid(ArrayList AccountList, String target)
{
String test = filename;
/** [ - The method sequentialSearch(Scanner, String) is undefined for the type
AccountNumber
- Duplicate local variable target ] */
int target = sequentialSearch(inputFile, filename);
if (target == -1)
{
System.out.println("Invalid account number.");
}
else
{
System.out.println("Account number is valid." + (target + 1));
}
}
/**
The sequentialSearch method searches an array for
a value.
@param array The array to search.
@param value The value to search for.
@return The subscript of the value if found in the
array, otherwise -1.
*/
**/ [- Syntax error on token ",", ; expected
- Syntax error on token ")", delete this token
- Duplicate local variable filename
- Illegal modifier for parameter sequentialSearch; only final is
permitted
- Duplicate local variable inputFile
- Syntax error on token "(", ; expected ] */
public static int sequentialSearch(int[] inputFile,int filename);
{
int index; // Loop control variable
int element; // Element the value is found at
boolean found; // Flag indicating search results
// Element 0 is the starting point of the search.
index = 0;
// Store the default values element and found.
element = -1;
found = false;
// Search the array.
while (!found && index <inputFile.length)
{
if (inputFile[index] == filename)
{
found = true;
}
index++;
}
}
}
}