Search for a string array name

So, I have a file in which there are all presidents - their name, average initial (if any) and last name.

The file must be read, and the user can enter the name of the president to search for, and this president must be displayed.

I have a president mapping if the user searches by name or by name, but not both.

For example, an external file contains:

George,Washington,(1789-1797) Franklin,D.,Roosevelt,(1933-1945) ... and so on with all the presidents 

I need the user to either enter the first name, last name, or first and last name, and get the desired result (the date does not matter for the most part).

I tried a lot of different things, but didn’t get to the president’s show if the user searches by name and surname.

Here is what I got so far:

 public class NameSearch { public static void main(String[] args) throws IOException { try { // read from presidents file Scanner presidentsFile = new Scanner(new File("Presidents.txt")); // scanner for user input Scanner keyboard = new Scanner(System.in); // create array list of each line in presidents file ArrayList<String> presidentsArrayList = new ArrayList<String>(); // prompt user to enter a string to see if it matches with a president name System.out.println("Enter a search string of letters to find a president match: "); // store user input String userInput = keyboard.nextLine(); // add president file info to array list linesInPresidentFile while (presidentsFile.hasNextLine()) { presidentsArrayList.add(presidentsFile.nextLine()); } // end while loop String presidentNamesArray[] = presidentsArrayList.toArray(new String[presidentsArrayList.size()]); String results = searchArray(presidentNamesArray, userInput); //System.out.println("\nThe presidents who have \"" + userInput + "\" as part of their name are: "); } catch (FileNotFoundException ex) { // print out error (if any) to screen System.out.println(ex.toString()); } // end catch block } // end main // method to search for a specific value in an array public static String searchArray(String array[], String value) { for (int i = 0; i < array.length; i++) { if (array[i].toLowerCase().contains(value.toLowerCase())) { String splitter[] = array[i].split(" ,"); System.out.println(Arrays.toString(splitter)); } } return Arrays.toString(array); } } 
+6
source share
2 answers

There is another way that I could implement this. Read the input files and save them as objects (e.g. a class with lname, fname and year). Thus, you can search for lname from user input, match it with the corresponding fname (like the same objects). Creation can be performed once, and the search can be performed in a while loop, which implements the consent of users to continue the search.

 //define your class like this: static int i; //to keep a track of number of objects public class dummy{ string fname; string lname; string year; }; while the file content exists: read the line dummy dobj[i++] = new dummy();//allocate memory for the object split the different parameters (fname, lname, year) from the read line put these read parameters into the object dobj[i].fname = first; dobj[i].lname = second; dobj[i].year = y; //ask your user to enter the query in a specified format //if he enters lname, compare your input to all the object lname, and so on //in case of lname && fname, compare your input to the lname first and then check for the corresponding objects fname, if they match.. display 

There are actually many ways in which you can achieve what you want to program. You may be asked to use array list indices to solve it. If you take input from a user in a specific format, you can match it with the index in this list. In addition, if you want to use the first and last name together, you can use this index representing the first and last name, which should come from the same list.

+2
source

The reason you might have trouble finding your first and last names is because you have to match your input exactly (ignoring the case, of course). I mean, if you use George Washington as input, your program will not find a match for the string George,Washington,(1789-1797) . This is because your program treats George Washington as one line. Note: there is no comma at the input, so it will not be considered a substring of George,Washington,(1789-1797) . If you used George,Washington as an input line, your program printed the George Washington line. Your program just looks if the input line is a substring of any of the lines in your file. He does not specifically search for a first or last name. If you used in as your input string, then you will get a match with both George Wash in gton and Frankl in D. Roosevelt.

What you can do is take your input and break it down and search for each condition. You can accept strings matching all of the conditions provided, or at least one of the conditions provided.

 public static String searchArray(String array[], String value) { // Uses both blank spaces and commas as delimiters String[] terms = value.toLowerCase().Split("[ ,]"); for (int i = 0; i < array.length; i++) { String line = array[i].toLowerCase(); boolean printIfAllMatch = true; boolean printIfAtLeastOneMatches = false; for(int j = 0 ; j < terms.length; j++) { // Check that all terms are contained in the line printIfAllMatch &= line.Contains(terms[j]); // Check that at least one term is in the line printIfAtLeastOneMatches |= line.Contains(terms[j]); } String splitter[] = array[i].split(" ,"); if (printIfAllMatch) { System.out.println(Arrays.toString(splitter)); } if(printIfAtLeastOneMatches) { System.out.println(Arrays.toString(splitter)); } } //I'm not sure why you're returning the original array as a string //I would think it would make more sense to return an Array of filtered data. return Arrays.toString(array); } 

This does not take into account the name order. If you do this, I would suggest creating a class and analyzing each line in the file as a new object and trying to combine the first term with the first name and the second term with a last name or something for that purpose.

0
source

All Articles