I have some sort of problem even tho, idk what it's actually that's my code
package $Random_Projects;
import java.util.Scanner;
public class PhoneBook {
public static void main (String[] args){
Scanner myScan = new Scanner(System.in);
System.out.println("Enter number of entries in the phone book ");
int num= myScan.nextInt();
String PBEnteries[]=new String[num];
for(int i=0; i<PBEnteries.length;i++){
System.out.println("Enter name and telephone number: ");
PBEnteries[i]=myScan.nextLine();
}
System.out.println("Enter name: ");
String nameSearch=myScan.next();
boolean search=false;
for(int i=0;i<PBEnteries.length;i++)
for(int j=0;j<nameSearch.length();j++){
if(nameSearch.charAt(j)==PBEnteries[i].charAt(j)){
if(j == nameSearch.length()){
search=true;
System.out.println(PBEnteries[i]);}
}
}
if(search == false)
System.out.println("Invalid name.");
}
}
my issue in 21 line : if(nameSearch.charAt(j)==PBEnteries[i].charAt(j)){
it should be searching for the name so the type name and phone number, ex."Sam 0000", "Danial 1111" and in search he writes the name ex."Sam" and should post num or even the whole array as "Sam 0000", but I've a problem with equalizing each char
note: it's not a personal program, they want it all in one array with specific java knowledge, so it should compare each char in the name that he typed with the array
output :
Enter number of entries in the phone book
3
Enter name and telephone number:
Enter name and telephone number:
Mohammed 1212
Enter name and telephone number:
Ali 0000
Enter name:
Ali
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0
at java.lang.String.charAt(String.java:658)
at $Random_Projects.PhoneBook.main(PhoneBook.java:21)