944,148 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 4069
  • Java RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Nov 2nd, 2009
0
Re: Search array for account number
I don't want to use it as a method. I'm was using the example he provided and tried to copy how it was laid out. How would I get the variable passed to that method while still keeping the method header that the instructor told us to use?

(from first post)
"I am instructed to use the following as the header to the method :
public static boolean isValid(ArrayList AccountList, String target)"

I don't understand what he wants us to use "target" for either. That may be my problem.

I posted the clean up post so to take it back to the basic working printing out the elements in the file for the array. Even with the example he sent me that I posted, I am still having problems understanding how to do it. I'm not asking for you to do it for me, but I guess the phrase "dumb it down" from what he is wanting us to do to my level applies. lol. I'm still working on it but I don't see how I can pass as a parameter without adding it to the header he told us to use.

I appreciate your help. I'm going to work on it some more before I head to bed.
Last edited by Ryujin89; Nov 2nd, 2009 at 3:31 am.
Reputation Points: 10
Solved Threads: 0
Light Poster
Ryujin89 is offline Offline
48 posts
since Oct 2009
Nov 3rd, 2009
0

Due in bout 13 hrs!!!

Ignoring the last few post, how would i modify the example to make it search the input file instead of a defined list such as it does in the example? In short, I want to figure out how to modify the example. I have and am working on it now but I can't figure it out. The assignment is due roughly 13 hrs from the time this is posted (noon central time US). I would greatly appreciate any assistance possible.
Reputation Points: 10
Solved Threads: 0
Light Poster
Ryujin89 is offline Offline
48 posts
since Oct 2009
Nov 3rd, 2009
0
Re: Search array for account number
If you ask a more specific question, or tell me exactly what isn't working, I'll help you out. After 12 straight hours of coding an iphone project that uses the google maps API, kinda tired though.

edit: when I say specific, that isn't what I meant, you were plenty specific, sorry. Its just that there are a lot of examples in this thread, so if you want help with a particular thing, you should repost the specific method/code you are talking about, say what you want to accomplish (i.e. what the code is supposed to do) and tell us any other info that would be helpful. Otherwise, if you're just stuck in general (i.e. you don't understand some concept) please restate it a little differently and I'll help you.
Last edited by BestJewSinceJC; Nov 3rd, 2009 at 1:46 am.
Reputation Points: 874
Solved Threads: 352
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008
Nov 3rd, 2009
0
Re: Search array for account number
This is the post I was referring to. Sorry. I'm just frustrated at myself for not being able to figure this out. Thank you for putting up with all this and assisting me.

Click to Expand / Collapse  Quote originally posted by Ryujin89 ...
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)
Java Syntax (Toggle Plain Text)
  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 :

Java Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Light Poster
Ryujin89 is offline Offline
48 posts
since Oct 2009
Nov 3rd, 2009
0

The original instructions mentioned in previous post.

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.
Reputation Points: 10
Solved Threads: 0
Light Poster
Ryujin89 is offline Offline
48 posts
since Oct 2009
Nov 3rd, 2009
0

Please dont get too upset. lol

I know you wanted me to simplify and "put straight forward" what I wanted, which I did. However, I was working on it since I made that last post and I believe I might have it now (close but not working yet).

The program will run and print out the account numbers in the input-ed (word? lol) file. Then ask for the account name and then stops there. I believe it has a lot to do with the "if" statement in the lower most function that actually does the search.

Any variable re-naming or other clean-up is appreciated. Too tired to think of better names.


Here is my code.

Java Syntax (Toggle Plain Text)
  1. package assignment8;
  2.  
  3. import java.util.Scanner;
  4. import java.io.*;
  5. import java.util.ArrayList;
  6. import java.util.Collections;
  7.  
  8. public class Assignment8{
  9. public static void main(String[] args) throws IOException
  10. {
  11. ArrayList<String> AccountList = new ArrayList<String>();
  12.  
  13. Scanner keyboard = new Scanner(System.in);
  14.  
  15. System.out.print("Enter the filename: ");
  16. String filename = keyboard.nextLine();
  17.  
  18. File file = new File(filename);
  19. Scanner inputFile = new Scanner(file);
  20.  
  21. while (inputFile.hasNext())
  22. {
  23. String account = inputFile.nextLine();
  24. AccountList.add(account);
  25. }
  26.  
  27. inputFile.close();
  28.  
  29. for (int index = 0; index < AccountList.size(); index++)
  30. {
  31. System.out.println("Index: " + index + " Account: " +
  32. AccountList.get(index));
  33. }
  34.  
  35. String target = "Your account number has been verified.";
  36.  
  37. isvalid(AccountList, target);
  38.  
  39.  
  40. }
  41.  
  42.  
  43. public static boolean isvalid(ArrayList AccountList, String target)
  44. {
  45. Scanner keyboard = new Scanner(System.in);
  46.  
  47. System.out.print("Please enter an account number: ");
  48. // get the number from the user
  49. int AccountNumber = keyboard.nextInt();
  50.  
  51. int results;
  52.  
  53. results = binarySearch(AccountList, AccountNumber);
  54.  
  55. if (results == -1)
  56. {
  57. System.out.println("That is not " +
  58. "a valid account number.");
  59. }
  60. else
  61. {
  62. System.out.println (target);
  63. }
  64.  
  65. return true;
  66. }
  67.  
  68. public static int binarySearch(ArrayList accountList, int AccountNumber)
  69. {
  70. int index;
  71. int correct = -1;
  72. boolean found;
  73.  
  74. index = 0;
  75.  
  76. found = false;
  77.  
  78. Collections.sort(accountList);
  79.  
  80. while (!found && index < accountList.size())
  81. {
  82.  
  83. /** This is where I believe the error is and why it's not working. */
  84.  
  85. if (correct != 1)
  86. {
  87. int search = Collections.binarySearch(accountList,AccountNumber);
  88. found = true;
  89. correct = 1;
  90. }
  91. index++;
  92.  
  93. /** Compiler gave error messages stating that it didn't return int if I didn't repeat it. idk.*/
  94.  
  95. return correct;
  96. }
  97. return correct;
  98. }
  99. }
Last edited by Ryujin89; Nov 3rd, 2009 at 6:13 am. Reason: comment tags wrong.
Reputation Points: 10
Solved Threads: 0
Light Poster
Ryujin89 is offline Offline
48 posts
since Oct 2009
Nov 3rd, 2009
0
Re: Search array for account number
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.
Reputation Points: 10
Solved Threads: 0
Light Poster
Ryujin89 is offline Offline
48 posts
since Oct 2009
Nov 3rd, 2009
0
Re: Search array for account number
I'm sure I can help you fix your binarySearch method (it looks like everything else in your program is correct). However, your instructions specifically state: "The method should use sequential search of ArrayList to find the target account." Your program doesn't use a sequential search, it uses a binary search. A sequential search would go through the ArrayList one element at a time, checking to see if each element is the one you're looking for. A binary search starts at the middle, checks to see if the element you're looking for is greater than or less than the middle. If it is greater than the middle, you now know that the element you're looking for is to the right of the middle, so now you have to look through everything to the right of the middle (using the same procedure). Your binarySearch method uses a while loop, and inside that while loop, it continuously calls Collections.binarySearch. Doing this doesn't make sense because Collections.binarySearch only needs to be called once . . and it returns either the index where your element was found ( >= 0 obviously) or it returns an integer less than 0 if your element wasn't in the array.

Now, you weren't completely off - the fact that you were using a while loop and going through your array means that you were probably trying to do a sequential search (if you look at each index, 0 through the end of the array, in order, that's sequential). But all you need to do to see if the element is in your array is this:

int index = yourList.indexOf(yourElement);

if index >= 0, the element was found in the array. If index == -1, then the element was not found in the array.

hope that helps.
Reputation Points: 874
Solved Threads: 352
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Merging two files
Next Thread in Java Forum Timeline: scroll text with many link





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC