Search array for account number

Thread Solved

Join Date: Oct 2009
Posts: 37
Reputation: Ryujin89 is an unknown quantity at this point 
Solved Threads: 0
Ryujin89 Ryujin89 is offline Offline
Light Poster

Search array for account number

 
0
  #1
26 Days Ago
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 ] )

  1. package accountNumber;
  2.  
  3. import java.util.Scanner;
  4. import java.io.*;
  5. import java.util.ArrayList;
  6.  
  7. public class AccountNumber{
  8. public static void main(String[] args) throws IOException
  9. {
  10. ArrayList<String> accountList = new ArrayList<String>();
  11.  
  12. Scanner keyboard = new Scanner(System.in);
  13.  
  14. System.out.print("Enter the filename: ");
  15. String filename = keyboard.nextLine();
  16.  
  17. File file = new File(filename);
  18. Scanner inputFile = new Scanner(file);
  19.  
  20. while (inputFile.hasNext())
  21. {
  22. String account = inputFile.nextLine();
  23. accountList.add(account);
  24. }
  25.  
  26. inputFile.close();
  27.  
  28. for (int index = 1; index < accountList.size(); index++)
  29. {
  30. System.out.println("Index: " + index + " Account: " +
  31. accountList.get(index));
  32. }
  33.  
  34. /** [ - Syntax error on token ")", ; expected
  35. - Syntax error on token "(", ; expected
  36. - Illegal modifier for parameter isValid; only final is permitted
  37. - Syntax error on token ",", ; expected
  38. - ArrayList is a raw type. References to generic type ArrayList<E> should be
  39. parameterized ] */
  40.  
  41. public static boolean isValid(ArrayList AccountList, String target)
  42. {
  43. String test = filename;
  44.  
  45. /** [ - The method sequentialSearch(Scanner, String) is undefined for the type
  46. AccountNumber
  47. - Duplicate local variable target ] */
  48.  
  49. int target = sequentialSearch(inputFile, filename);
  50.  
  51. if (target == -1)
  52. {
  53. System.out.println("Invalid account number.");
  54. }
  55. else
  56. {
  57. System.out.println("Account number is valid." + (target + 1));
  58. }
  59. }
  60.  
  61. /**
  62.   The sequentialSearch method searches an array for
  63.   a value.
  64.   @param array The array to search.
  65.   @param value The value to search for.
  66.   @return The subscript of the value if found in the
  67.   array, otherwise -1.
  68.  */
  69.  
  70. **/ [- Syntax error on token ",", ; expected
  71. - Syntax error on token ")", delete this token
  72. - Duplicate local variable filename
  73. - Illegal modifier for parameter sequentialSearch; only final is
  74. permitted
  75. - Duplicate local variable inputFile
  76. - Syntax error on token "(", ; expected ] */
  77. public static int sequentialSearch(int[] inputFile,int filename);
  78. {
  79. int index; // Loop control variable
  80. int element; // Element the value is found at
  81. boolean found; // Flag indicating search results
  82.  
  83. // Element 0 is the starting point of the search.
  84. index = 0;
  85.  
  86. // Store the default values element and found.
  87. element = -1;
  88. found = false;
  89.  
  90. // Search the array.
  91. while (!found && index <inputFile.length)
  92. {
  93. if (inputFile[index] == filename)
  94. {
  95. found = true;
  96. }
  97. index++;
  98. }
  99. }
  100. }
  101. }
Attached Files
File Type: txt accounts.txt (164 Bytes, 2 views)
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,563
Reputation: BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all 
Solved Threads: 196
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso
 
0
  #2
26 Days Ago
Duplicate local variable 'target' means exactly what it says - you have the same variable declared twice in the same method, which is not allowed. In your method header you have "ArrayList AccountList, String target" but then below, you said "int target" which is attempting to re-declare the variable target. You can't do that, name one of them something different or get rid of one of them. PS, the reason for the "ArrayList is a raw type" message is because of Java generics (don't worry about it for now), just know that if you are expecting your method to take an ArrayList of Strings (which you are), then you would say "ArrayList<String> AccountList". And btw, variable names, by convention (i.e. people almost always do it this way) are named in camel case, starting with lower, so you should make it "ArrayList<String> accountList".
Out.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,563
Reputation: BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all 
Solved Threads: 196
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso
 
0
  #3
26 Days Ago
You also have the error message:

  1. [ The method sequentialSearch(Scanner, String) is undefined for the type AccountNumber ]

That's because your method header is defined as "public static int sequentialSearch(int[] inputFile,int filename);" ... but if you look in your code, you called it using

  1. sequentialSearch(inputFile, filename);

The problem is that the name of the parameter does not matter when you are calling a method, it is the type that matters. Since you declared "inputFile" as a Scanner (in your main method), then you called sequentialSearch using a Scanner, which it does not take, you got an error.

Again: the sequentialSearch method, according to your code, takes two parameters: int[] and int, but you passed it a Scanner, hence the compiler gave you an error.

Hope that helps
Out.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 91
Reputation: thines01 is an unknown quantity at this point 
Solved Threads: 8
thines01 thines01 is offline Offline
Junior Poster in Training
 
0
  #4
25 Days Ago
1) How fancy are you allowed to get with this?
2) Since you KNOW the input file name, do you need to scan for it?
3) I would suggest multiple functions to help keep the actions separate and your design clean.

[Functions I would suggest]
1) main
2) public static boolean LoadInputFile(String strFileName, ArrayList<String> lstAccounts, String strError)
3) public static void displayAllAccounts(ArrayList<String> AccountList)
4) public static String getAccNumFromUser()
5) public static boolean isValid(ArrayList AccountList, String target)

With multiple functions, your "main" will stay really clean and debugging will be easier.
Last edited by thines01; 25 Days Ago at 1:43 pm. Reason: typo
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,563
Reputation: BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all 
Solved Threads: 196
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso
 
0
  #5
25 Days Ago
That would be nice, but if you read his code and his errors, he clearly is having problems with passing the correct arguments into his current methods. Creating more methods at this point will only add to the chaos. His problem right now is that he needs to grasp how methods work and how the compiler enforces the correct parameters being passed.
Out.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 37
Reputation: Ryujin89 is an unknown quantity at this point 
Solved Threads: 0
Ryujin89 Ryujin89 is offline Offline
Light Poster

Clean up

 
0
  #6
24 Days Ago
The instructor told us that we had to follow his guidelines (at the top of the 1st post). To clean things up and try to look at things from a somewhat different angle I have deleted back down to just printing out the list of elements from the read in file (accounts.txt) and have left the area to create the search open other than the method head he wants us to use. I emailed him and his only response was sending me an example search so I will include that code as well to show what style he is wanting us to use. As I said, I had most of the code before I posted on here and am now stuck on the search part.

My cleaned up code (working if take out the last method header)
  1. package assignment8;
  2.  
  3. import java.util.Scanner;
  4. import java.io.*;
  5. import java.util.ArrayList;
  6.  
  7. public class Assignment8{
  8. public static void main(String[] args) throws IOException
  9. {
  10. ArrayList<String> accountList = new ArrayList<String>();
  11.  
  12. Scanner keyboard = new Scanner(System.in);
  13.  
  14. System.out.print("Enter the filename: ");
  15. String filename = keyboard.nextLine();
  16.  
  17. File file = new File(filename);
  18. Scanner inputFile = new Scanner(file);
  19.  
  20. while (inputFile.hasNext())
  21. {
  22. String account = inputFile.nextLine();
  23. accountList.add(account);
  24. }
  25.  
  26. inputFile.close();
  27.  
  28. for (int index = 0; index < accountList.size(); index++)
  29. {
  30. System.out.println("Index: " + index + " Account: " +
  31. accountList.get(index));
  32. }
  33.  
  34. **/ take out this and the program reads out the files and only leaves the searching for specific file part to do./*
  35. public static boolean isValid(ArrayList AccountList, String target)
  36. {
  37.  
  38. }
  39. }
  40. }
  41.  

The example he sent me :

  1. /**
  2.   This program sequentially searches an
  3.   int array for a specified value.
  4. */
  5.  
  6. public class SearchArray
  7. {
  8. public static void main(String[] args)
  9. {
  10. int[] tests = { 87, 75, 98, 100, 82 };
  11. int results;
  12.  
  13. // Search the array for the value 100.
  14. results = sequentialSearch(tests, 100);
  15.  
  16. // Determine whether 100 was found and
  17. // display an appropriate message.
  18. if (results == -1)
  19. {
  20. System.out.println("You did not " +
  21. "earn 100 on any test.");
  22. }
  23. else
  24. {
  25. System.out.println("You earned 100 " +
  26. "on test " + (results + 1));
  27. }
  28. }
  29.  
  30. /**
  31.   The sequentialSearch method searches an array for
  32.   a value.
  33.   @param array The array to search.
  34.   @param value The value to search for.
  35.   @return The subscript of the value if found in the
  36.   array, otherwise -1.
  37.   */
  38.  
  39. public static int sequentialSearch(int[] array,
  40. int value)
  41. {
  42. int index; // Loop control variable
  43. int element; // Element the value is found at
  44. boolean found; // Flag indicating search results
  45.  
  46. // Element 0 is the starting point of the search.
  47. index = 0;
  48.  
  49. // Store the default values element and found.
  50. element = -1;
  51. found = false;
  52.  
  53. // Search the array.
  54. while (!found && index < array.length)
  55. {
  56. if (array[index] == value)
  57. {
  58. found = true;
  59. element = index;
  60. }
  61. index++;
  62. }
  63.  
  64. return element;
  65. }
  66. }

I have canceled my plans for Halloween X( just to give myself more time to work on this since its due noon Tue CDT.

Thanks for the assistance and I hope I can figure this out.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 37
Reputation: Ryujin89 is an unknown quantity at this point 
Solved Threads: 0
Ryujin89 Ryujin89 is offline Offline
Light Poster
 
0
  #7
24 Days Ago
I know it's probably basic errors that you have described how to fix before now but I am falling asleep at the keyboard (its 5AM here and I've been up for almost 20 hrs) and wanted to post what I had done since I "cleaned up". If it's too bad to work with, I don't mind deleting it.

Here is the code with the errors in the braces just as before.

  1. package assignment8;
  2.  
  3. import java.util.Scanner;
  4. import java.io.*;
  5. import java.util.ArrayList;
  6.  
  7. public class Assignment8{
  8. public static void main(String[] args) throws IOException
  9. {
  10. ArrayList<String> accountList = new ArrayList<String>();
  11.  
  12. Scanner keyboard = new Scanner(System.in);
  13.  
  14. System.out.print("Enter the filename: ");
  15. String filename = keyboard.nextLine();
  16.  
  17. File file = new File(filename);
  18. Scanner inputFile = new Scanner(file);
  19.  
  20. while (inputFile.hasNext())
  21. {
  22. String account = inputFile.nextLine();
  23. accountList.add(account);
  24. }
  25.  
  26. inputFile.close();
  27.  
  28. for (int index = 0; index < accountList.size(); index++)
  29. {
  30. System.out.println("Index: " + index + " Account: " +
  31. accountList.get(index));
  32. }
  33. }
  34.  
  35.  
  36. /** [ Multiple markers at this line
  37. - This method must return a result of type boolean
  38. - ArrayList is a raw type. References to generic type ArrayList<E> should be
  39. parameterized ] I understand why I got the return error, but I'm not sure how to go about keeping the header the way the instructor wants while following the example and making the program work. I remember bout the raw type part as well. */
  40.  
  41. public static boolean isValid(ArrayList AccountList, String target)
  42. {
  43. Scanner keyboard = new Scanner(System.in);
  44.  
  45. System.out.println("Enter your account number : ");
  46. String accountNumber = keyboard.nextLine();
  47.  
  48. int valid;
  49.  
  50. valid = sequentialSearch(target, accountNumber);
  51.  
  52. if (valid == -1)
  53. {
  54. System.out.println("You did not enter a valid account number");
  55. }
  56. else
  57. {
  58. System.out.println("Your account number has been verified");
  59. }
  60. }
  61.  
  62.  
  63. public static int sequentialSearch(String array, String file)
  64. {
  65. int index;
  66. int element;
  67. boolean valid;
  68.  
  69. index = 0;
  70.  
  71. element = -1;
  72. valid = false;
  73.  
  74. while (!valid && index < array.length())
  75. {
  76.  
  77. /** [The type of the expression must be an array type but it resolved to String ] referring to the array[index] of the following if statement */
  78.  
  79. if (array[index] == file)
  80. {
  81. valid = true;
  82. element = index;
  83. }
  84. index++;
  85. return element;
  86. }
  87. }
  88. }

Thanks again
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 37
Reputation: Ryujin89 is an unknown quantity at this point 
Solved Threads: 0
Ryujin89 Ryujin89 is offline Offline
Light Poster
 
0
  #8
24 Days Ago
comment code backwards on the "clean up" post. Sorry. Drowsiness showing. lol
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 37
Reputation: Ryujin89 is an unknown quantity at this point 
Solved Threads: 0
Ryujin89 Ryujin89 is offline Offline
Light Poster
 
0
  #9
24 Days Ago
I know that I'm putting in a lot before everyone get a chance to look at what I have posted, but here is what I've tried.

This is only the function used to search for the array at the bottom. I only get one error but even if it's solved, I don't know if this will work. Look back at the last few post before this one if it does not look worth working with. lol

  1. public static boolean valid(ArrayList AccountList, String target)
  2. {
  3. // check to see if the number is valid
  4. if (isValid(AccountList, target) == true )
  5. {
  6. System.out.println("That number is valid.");
  7. }
  8. else {
  9. System.out.println("That number is invalid.");
  10. }
  11. return true;
  12. }
  13.  
  14. public static boolean isValid(ArrayList AccountList, String target)
  15. {
  16. int index;
  17. int element;
  18. boolean valid;
  19.  
  20. index = 0;
  21.  
  22. element = -1;
  23. valid = false;
  24.  
  25. Scanner keyboard = new Scanner(System.in);
  26. System.out.print("Please enter an account number: ");
  27. // get the number from the user
  28. int accountNumber = keyboard.nextInt();
  29.  
  30. while (!valid && index < AccountList.size())
  31. {
  32. /** Error says that "filename" is undefined, but it is in the main function. I don't know. */
  33. if (filename(accountNumber) == true)
  34. {
  35. valid = true;
  36. element = index;
  37. }
  38. index++;
  39.  
  40. return valid;
  41. }
  42. }
  43. }
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,563
Reputation: BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all 
Solved Threads: 196
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso
 
0
  #10
23 Days Ago
You're using filename as if it was a method, but it is not a method. It is a variable of type String. And also, since filename was declared in the main method, you cannot use it in any other method. If you want to use a variable from one method in another method, you must pass it as a parameter. Examples of parameters for your isValid function are your AccountList and target.
Out.
Reply With Quote Quick reply to this message  
Reply

Tags
account, array, number

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC