Sorry about the formatting disaster, I’m new to stackoverflow and Java. I have two issues inside of a code that is supposed to contain two lists of names and corresponding catalog #s. Inside the code I am supposed to be able to search, display, edit, and quit. Display and quit already work, but I have an issue with search and edit. I have highlighted information where the issue is. I would appreciate any help.
public static void Search(String[] arr, String find) {
for (int i = 0; i < 10; i++) {
//First issue!! This is my method to call upon later that searches within a String Array for bits of code. I need to be able to search within the array for certain characters (the arrays are names, so “mith” would bring up “smith”. Later in the code I call on this inside of a switch. I think I need something like “arr[i].equals(find)” but I can’t figure out the syntax
}
}
public static void main(String[] args) {
Scanner inp = new Scanner(System.in);
String userInput1 = "";
String userInput2 = "";
String userInput3 = "";
// Ask User to chose between faculty and students
System.out.println("Are you searching for Students or Faculty? ");
userInput1 = inp.nextLine().toLowerCase();
if (userInput1.startsWith("f")) {
//Block 1 for faculty
do {
String[] Faculty = {"0. Jack Kerouac", "1. Les Claypool", "2. Tom Waits", "3. Kurt Vonnegut",
"4. Hunter Thompson", "5. Princess Bubblegum", "6. John Smith", "7. Jack Thompson", "8. Jeff Dahmer", "9. Martin King"};
System.out.println("What would you like to do?");
System.out.println("S: Search");
System.out.println("E: Edit");
System.out.println("D: Display");
System.out.println("Q: Quit");
// user input
userInput2 = inp.nextLine().toLowerCase();
System.out.println("");
// Respond based on user input
switch (userInput2.charAt(0)) {
case 's':
System.out.println("Type part of the persons name: ");
String x = inp.nextLine().toLowerCase();
String[] y = Faculty;
Search(y, x);
break;
case 'e':
//This is my editing arrays issue comes in. I’ve attempted a couple different methods to get this to work but in each one I run into a problem when reassigning the Faculty[q]. When you run the program it skips from “Enter new catalog information” back to the main loop. How do I change the string inside of my array?
System.out.println("Enter the catalog number of the entry trying to edit: ");
int q = inp.nextInt();
System.out.println(Faculty[q]);
System.out.println("Enter new catalog information: ");
String NewFac = inp.nextLine();
Faculty[q] = String NewFac;
System.out.println(Faculty[q]);
break;
case 'd':
System.out.println("Enter a catalog number between 0-9: ");
int o = inp.nextInt();
System.out.println(Faculty[o]);
break;
}
// Blank line, for formatting
System.out.println("");
} while (!userInput2.equals("q"));
System.out.println("Done.");
} else {
//block 2 for students
do {
String[] Student = {"0. Alice Johnson", "1. Johnny Marr", "2. Johnny Johnson", "3. Robert Smith",
"4. Ian Curtis", "5. Luke Shapiro", "6. David Newman", "7. Ren Newman", "8. Camille Kaslan", "9. Alexander Shulgin"};
System.out.println("What would you like to do?");
System.out.println("S: Search");
System.out.println("E: Edit");
System.out.println("D: Display");
System.out.println("Q: Quit");
// user input
userInput3 = inp.nextLine().toLowerCase();
System.out.println("");
// Respond based on user input
switch (userInput3.charAt(0)) {
case 's':
System.out.println("Type part of the persons name: ");
String x = inp.nextLine().toLowerCase();
String[] y = Student;
Search(y, x);
break;
case 'e':
System.out.println("Enter the catalog number of the entry trying to edit: ");
int r = inp.nextInt();
System.out.println(Student[r]);
System.out.println("Enter new catalog information: ");
Student[r] = inp.nextLine();
System.out.println(Student[r]);
break;
case 'd':
System.out.println("Enter a catalog number between 0-9: ");
int n = inp.nextInt();
System.out.println(Student[n]);
break;
}
// Blank line, for formatting
System.out.println("");
} while (!userInput3.equals("q"));
System.out.println("Done.");
}
In case it was unclear, the firsts issue is what to put in the for-loop inside of my search method called “search”. The second question is how to take the new info from the user and replace it in the array of names (the edit feature).
First question, maybe something like this?
Second question is a common problem that comes up all the time on SO, you need to follow your nextInt with a nextLine to eat the newline character: