0
votes

I have a Java code that checks input from the keyboard. There is a loop of if that for specific character returns True. The else returns false for any other character.

How to write a code that identifies which of the above two (True or False) was chosen by the program and print it? The boolean value of the instance connectionStatus

import java.util.Scanner;

public class BlueTooth3 {
    public boolean connectionStatus;

     boolean connectBlueTooth () {
        System.out.println("Enter connecting code Baby3");
        Scanner keyboard = new Scanner(System.in);
        String conCode = keyboard.next();
        System.out.println("You Entered " + conCode);
        keyboard.close();
        if (conCode.equals ("c")){
        System.out.println(conCode + " Is a true Code");
        System.out.println("This is Boolean " +Boolean.TRUE);

            return connectionStatus;
        } 

        if (conCode.equals ("C")){
            System.out.println(conCode + " Is a true Code");
            System.out.println("This is Boolean " +Boolean.TRUE);

                return connectionStatus;
            }
        else {
            System.out.println( conCode + " Is a false Code" );
            System.out.println("This is Boolean " + Boolean.FALSE);
            return connectionStatus;
        }
        }
     }
1
Why not simply use "System.out.println(connectionStatus);"? It will print the value of the variable and if it's boolean it'll show true or false..John Ephraim Tugado

1 Answers

0
votes

Unless my eyes are closed, you don't seem to be doing anything to the connectionStatus variable. You can change the value of that variable so that the function that receives that variable can do things accordingly. For example:

    if (conCode.equals ("C")){
        System.out.println(conCode + " Is a true Code");
        System.out.println("This is Boolean " +Boolean.TRUE);
        connectionStatus = true; // means that the code went in this if
            return connectionStatus;
    }
    else {
        System.out.println( conCode + " Is a false Code" );
        System.out.println("This is Boolean " + Boolean.FALSE);
        connectionStatus = false; // means that the code went in this else
        return connectionStatus; 
    }

And your code should be something like this:

public void myFunc(){
    System.out.println(connectBlueTooth);
}

This just simply prints out the outcome of the connectBlueTooth function, which should be True if it went in the if, and False if it went in the else.