| | |
Search array for account number
Thread Solved |
•
•
Join Date: Oct 2009
Posts: 37
Reputation:
Solved Threads: 0
0
#11 25 Days Ago
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.
(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; 25 Days Ago at 3:31 am.
•
•
Join Date: Oct 2009
Posts: 37
Reputation:
Solved Threads: 0
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.
•
•
Join Date: Sep 2008
Posts: 1,566
Reputation:
Solved Threads: 196
0
#13 24 Days Ago
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.
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; 24 Days Ago at 1:46 am.
Out.
•
•
Join Date: Oct 2009
Posts: 37
Reputation:
Solved Threads: 0
0
#14 24 Days Ago
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.
•
•
•
•
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)
package assignment8; import java.util.Scanner; import java.io.*; import java.util.ArrayList; 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)); } **/ take out this and the program reads out the files and only leaves the searching for specific file part to do./* public static boolean isValid(ArrayList AccountList, String target) { } } }
The example he sent me :
Java Syntax (Toggle Plain Text)
/** This program sequentially searches an int array for a specified value. */ public class SearchArray { public static void main(String[] args) { int[] tests = { 87, 75, 98, 100, 82 }; int results; // Search the array for the value 100. results = sequentialSearch(tests, 100); // Determine whether 100 was found and // display an appropriate message. if (results == -1) { System.out.println("You did not " + "earn 100 on any test."); } else { System.out.println("You earned 100 " + "on test " + (results + 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. */ public static int sequentialSearch(int[] array, int value) { 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 < array.length) { if (array[index] == value) { found = true; element = index; } index++; } return element; } }
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.
•
•
Join Date: Oct 2009
Posts: 37
Reputation:
Solved Threads: 0
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 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.
•
•
Join Date: Oct 2009
Posts: 37
Reputation:
Solved Threads: 0
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.
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)
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; } }
Last edited by Ryujin89; 24 Days Ago at 6:13 am. Reason: comment tags wrong.
•
•
Join Date: Oct 2009
Posts: 37
Reputation:
Solved Threads: 0
0
#17 23 Days Ago
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.
Thanks and I greatly appreciate it.
•
•
Join Date: Sep 2008
Posts: 1,566
Reputation:
Solved Threads: 196
0
#18 23 Days Ago
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.
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.
Out.
![]() |
Other Threads in the Java Forum
- Previous Thread: Merging two files
- Next Thread: scroll text with many link
| Thread Tools | Search this Thread |
access account append arc arithmetic array arrays assembly autododesk basic beginner binarysearchtree buttons c# c++ cast char character class click college conversion convert coordinates count counttheoccurenceofanintegerinthe10inputs data database dynamic file frequency function game graph guess guessing high histogram image index input inputs int integer java line list listbox loops maximum memory menu ms mssqlbackend multidimensional mysql number numbertoword occurence output parameter perl php physics pointer prime program programming python query random read recursion reference result scanner school scrolledtext search sequential sets sql string student table text trim troubleshoot txt upload user useraccounts variable vb vbnet visual visualbasic.net visualstudio2008 winforms word







