0

I'm new to programming java so i'm not sure if this is the easiest way to do things and have spent a few hours trying to figure this out with no luck.

The

System.out.println("Respond (Yes) or (No) ...");

part of the program doesn't initially respond to an empty user input and displays the above text twice. After the 1st incorrect entry it behaves as it should??

`package qaCoursework;

import java.util.Scanner;

public class Test2 {
    public static void main(String[] args) {
        Scanner Input = new Scanner(System.in);

        System.out.println("Enter Name...");
        String name = Input.nextLine();

        System.out.println("Enter age...");
        int age = Input.nextInt();

        System.out.println("Do you like programming ...?");

        while (true) {
            String Response = Input.nextLine();
            if (Response.equalsIgnoreCase("yes")) {
                System.out.println(name + " is " + age + " and loves programming");
                break;
            }

            else if (Response.equalsIgnoreCase("no")) {
                System.out.println(name + " is " + age + " and hates computers ...!");
                break;
            }

            else {
                System.out.println("Respond (Yes) or (No) ...");

            }
        }

    }
}`
  • welcome to stackoverflow – Lova Chittumuri Nov 27 '18 at 11:43
  • how do you execute it? can you update your question with the commandline you run, and check that you do not have any trailing whitespaces. – JoSSte Nov 27 '18 at 11:45
  • The reason for it being displayed twice is your `do ... while (...)` statement that executes the body once without checking the conditions and then another time checking the conditions. Replacing it with a simple `while` loop may resolve your issue. – deHaar Nov 27 '18 at 11:48
  • Thanks for the responses I still can't fix the issue but decided to get rid of the `do while` loop in exchange for an infinite loop – Benjaaaamin Nov 27 '18 at 13:19

0 Answers0