Is there another way to use this boolean in java 11?

Federico Steidl Martinez :

So, I have been learning java for two days and came into an error message while compiling this code below. What i tried to do is a simple program that gets a Name from the system input and then wants to know if that is the name you want to go by. Worked perfectly. So i wanted to modify it: If that is not the desired name, you should be able to retype the name as often as you want. That's why I have the boolean "confirmed" in there, along with the while loop. When compiled, I get the error message "The value of the local variable confirmed is not used" for the "confirmed" boolean, even though I'm clearly declaring and using it. I've tried simply moving the initial declaration around and it didn't change anything. Does anyone know how to fix that or re-do my loop so it won't be any problem?

Fyi, I'm using VS Code with the Java extension pack.

import java.util.*;

public class Name{

public static void main(String[] args){

    Scanner sc = new Scanner(System.in);

    try{

    boolean confirmed = false;

    while(confirmed = false){
    System.out.println("What's your name again?");
    String enteredName = sc.nextLine();
    System.out.println("So, your name is " + enteredName + "?\nEnter 'yes' to confirm or 'no' to type a new name.");
    String confirmation = sc.nextLine();

    if(confirmation.equalsIgnoreCase("yes") || confirmation.equalsIgnoreCase("yes.")){
    System.out.println("Confirmed, " + enteredName + ".\n\nNow launching.");
    confirmed = true;
    }

    else if(confirmation.equalsIgnoreCase("no") || confirmation.equalsIgnoreCase("no.")){
    System.out.println("Please enter a new name.");
    confirmed = false;    
           }

        }

    }
    finally{
     sc.close();
    }
}

}

Arvind Kumar Avinash :

You should not close Scanner for System.in as it also closes System.in. Apart from this, you can simplify your code as follows:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String confirmation, enteredName;
        do {
            System.out.print("What's your name again?");
            enteredName = sc.nextLine();
            System.out
                    .print("So, your name is " + enteredName + "?\nEnter 'yes' to confirm or 'no' to type a new name.");
            confirmation = sc.nextLine();
            if (confirmation.equalsIgnoreCase("yes") || confirmation.equalsIgnoreCase("yes.")) {
                System.out.println("Confirmed, " + enteredName + ".\n\nNow launching.");
            }
        } while (confirmation.equalsIgnoreCase("no") || confirmation.equalsIgnoreCase("no."));
    }
}

A sample run:

What's your name again?Arvind
So, your name is Arvind?
Enter 'yes' to confirm or 'no' to type a new name.no
What's your name again?Kumar
So, your name is Kumar?
Enter 'yes' to confirm or 'no' to type a new name.no.
What's your name again?Avinash
So, your name is Avinash?
Enter 'yes' to confirm or 'no' to type a new name.yes
Confirmed, Avinash.

Now launching.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=367101&siteId=1