I've been having issues getting my program to do what I need it to do. There are no syntax errors in the code.
Firstly I've got my program set to ask the user for a first and last name for a maximum of 3 times, the user should be able to end the input early if they want to.
Is there a way that I can put the System.out.print("Do you want to add a new name (Y/N)"); outside of the if statement? So it will stop after I've put in my third name, or put in 2 names and typed N.
Any help will be greatly appreciated.
import java.util.Scanner;
public class SOF {
public static void main(String test[]){
@SuppressWarnings("resource")
Scanner scanner = new Scanner (System.in);
System.out.print("Do you want to add a new name (Y/N)");
String newname = scanner.next();
int AddName = 0;
while (AddName < 3) {
if (newname.equalsIgnoreCase("y"))
{
System.out.println("Enter first name: ");
String fname = scanner.next();
System.out.println("Enter last name: ");
String lname = scanner.next();
AddName = AddName + 1;
System.out.print("Do you want to add a new name (Y/N)");
String newnamee = scanner.next(); //had to add an extra e, since I had two varibles
// with the same time. This might be the issue.
//I'm unsure though
}
else if (newname.equalsIgnoreCase("n")) {
System.out.println("Goodbye");
AddName = AddName + 3;
}
}
}
}
newnamethen add&& newname.equalsIgnoreCase("y")to the while test.