I'm encountering an issue while attempting to utilize the substring method to locate a specific character within a string variable. Currently, I have set up a for loop to iterate over the length of a string variable named name. Inside this loop, I use the substring method within an if statement to search for a particular character, denoted by a ".".
However, despite my efforts, I've been unable to achieve the desired outcome. I would greatly appreciate any assistance in resolving this issue. Here's the relevant portion of my code:
System.out.println("\nEnter name: ");
String name = in.nextLine();
int length = name.length();
for (int x = 0; x < length; x++) {
if(name.substring(x, x + 1).equals(".")) {
System.out.println("Error! - name cannot contain (.) values\n"
+ "***************************************************");
System.out.println("\nWould you like to capture another name?" +
"\nEnter (1) to continue or any other key to exit");
String opt1 = in.nextLine();
// If statement to restart the application
if (opt1.equals("1")) {
System.out.println("menu launch");
} else {
System.exit(0);
}
} else {
break;
}
}
I would appreciate any insights into why my approach isn't working as expected and how I might rectify it. Thank you in advance for your assistance!