Quindi ho un file che contiene tutti i presidenti: il loro nome, iniziale iniziale (se presente) e cognome.Ricerca nome array stringa
Il file deve essere letto e un utente può immettere il nome di un presidente per cercarlo e il presidente deve essere visualizzato.
Ho visualizzato il presidente se un utente cerca per nome o per cognome, ma non per entrambi.
Ad esempio, il file esterno contiene:
George,Washington,(1789-1797)
Franklin,D.,Roosevelt,(1933-1945)
... and so on with all the presidents
Ho bisogno che l'utente sia in grado di entrambi i tipi nel nome, il cognome, o entrambi nome e cognome e di ottenere il risultato desiderato (la data è irrilevante per la maggior parte).
Ho provato un sacco di cose diverse, ma non ci sono arrivato per quanto riguarda la visualizzazione del presidente se l'utente cerca per nome e cognome.
Ecco quello che ho ottenuto finora:
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's 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);
}
}
vuoi chiedere? – sunysen
Umm ... cosa? Sì, voglio chiedere lol ... – rockymontana