0

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

}

1
  • Use the same newname then add && newname.equalsIgnoreCase("y") to the while test. Commented Jun 20, 2014 at 9:03

3 Answers 3

2

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.
    }
   }
  }
 }
Sign up to request clarification or add additional context in comments.

1 Comment

Instead of AddName = AddName + 3; alternatively you can use break statement.
1

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

1 Comment

I tried this and it worked. I learned something new today, never used nor tried do before. You guys respond really quickly!
0

Your second variable is never used:

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

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.