Rias
Rias

Reputation: 3

Using a nested if and else if statement inside of a while statement

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;
        }
    }
}

}

Upvotes: 0

Views: 795

Answers (3)

Nikhil Talreja
Nikhil Talreja

Reputation: 2774

Use do while instead:

public static void main(String test[]) {

        @SuppressWarnings("resource")
        Scanner scanner = new Scanner(System.in);
        int AddName = 0;

        do  {

            System.out.print("Do you want to add a new name (Y/N)");
            String newname = scanner.next();

            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;

            }

            else if (newname.equalsIgnoreCase("n")) {

                System.out.println("Goodbye");
                AddName = AddName + 3;
            }

        } while (AddName < 3);
    }

Upvotes: 1

user3722371
user3722371

Reputation: 145

You can put the System.out.print("Do you want to add a new name (Y/N)"); on the beginning of the while loop, something like this :

import java.util.Scanner;

public class SOF {
public static void main(String test[]){
@SuppressWarnings("resource")
Scanner scanner = new Scanner (System.in);
int AddName = 0;

while (AddName < 3) {
System.out.print("Do you want to add a new name (Y/N)");
String newname = scanner.next();

    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;         

    }

    else if (newname.equalsIgnoreCase("n")) {
        System.out.println("Goodbye");
        AddName = AddName + 3;  //alternatively you can use break statement.
    }
   }
  }
 }

Upvotes: 2

user2575725
user2575725

Reputation:

Your second variable is never used:

String newnamee = scanner.next(); //<-- remove the declaration

newname = scanner.next();//<-- try this instead

Upvotes: 0

Related Questions