Results 1 to 2 of 2
- 11-03-2009, 11:05 AM #1
Member
- Join Date
- Nov 2009
- Posts
- 3
- Rep Power
- 0
Mostly completed array search program
Most of program is complete. Much time put in on my part but now crunch time with it being due in 7.5 hrs. I would greatly appreciate assistance.
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 or other user specified file). 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 (or other user specified) 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.
The only thing I need assistance with is the search part. I have coded a binary search and I believe the only issue is the nested if statement of the while loop in lower-most function.
Any variable re-naming or other clean-up is appreciated. Too tired to think of better names.
Here is my code.
Java Code:package assignment8; import java.util.Scanner; import java.io.*; import java.util.ArrayList; import java.util.Collections; public class Assignment8{ 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 = 0; index < AccountList.size(); index++) { System.out.println("Index: " + index + " Account: " + AccountList.get(index)); } String target = "Your account number has been verified."; isvalid(AccountList, target); } public static boolean isvalid(ArrayList AccountList, String target) { Scanner keyboard = new Scanner(System.in); System.out.print("Please enter an account number: "); // get the number from the user int AccountNumber = keyboard.nextInt(); int results; results = binarySearch(AccountList, AccountNumber); if (results == -1) { System.out.println("That is not " + "a valid account number."); } else { System.out.println (target); } return true; } public static int binarySearch(ArrayList accountList, int AccountNumber) { int index; int correct = -1; boolean found; index = 0; found = false; Collections.sort(accountList); while (!found && index < accountList.size()) { /** This is where I believe the error is and why it's not working. */ if (correct != 1) { int search = Collections.binarySearch(accountList,AccountNumber); found = true; correct = 1; } index++; /** Compiler gave error messages stating that it didn't return int if I didn't repeat it. idk.*/ return correct; } return correct; } }
- 11-04-2009, 03:25 AM #2
Member
- Join Date
- Nov 2009
- Posts
- 3
- Rep Power
- 0
Please assist !!!!!!
I know that it is passed the due time, but can anyone please help me to complete this assignment for at lease partial credit? I would greatly appreciate it. I had a lot of it finished before posting and then assisted greatly by a programmer and now I need to finish it up.
Thanks and I greatly appreciate it.
Similar Threads
-
Search a string in a byte array
By 2BOrNot2B in forum New To JavaReplies: 0Last Post: 03-12-2009, 05:52 PM -
[SOLVED] Search problem - array out of bounds
By viper110110 in forum New To JavaReplies: 5Last Post: 11-26-2008, 04:26 AM -
Newbie search array question
By CirKuT in forum New To JavaReplies: 19Last Post: 09-14-2008, 06:26 AM -
Has anyone here completed the SCJP course?
By DonCash in forum Java CertificationReplies: 3Last Post: 04-29-2008, 10:24 AM -
Array Search Test
By Java Tip in forum java.langReplies: 0Last Post: 04-14-2008, 08:45 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks