2

Looking to do something like in C#:

bool walkable = t.Type == TileType.Green ? true : false;

but in Java

Boolean international_F = (in.next() == 'Y') ? true : false;

The above is what I've tried so far. Wondering if it's even possible.

EDIT: I just noticed .nextChar() doesn't exist. Edited snippet to reflect that.

Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121
Edward Suzuki
  • 103
  • 2
  • 9

4 Answers4

2

"nextChar": Assuming in is a Scanner, your issue is that Scanner doesn't have a nextChar() method. You could read a whole word, and then take it's first char:

char theChar = in.next().charAt(0)

boolean vs ternery: If your outputs are true/false, then you don't need an if. You can just write:

bool walkable = t.Type == TileType.Green; // C#
boolean international_F = in.next().charAt(0) == 'Y'` // Java

boolean vs Boolean: Please also note that boolean is the primitive boolean type in Java. Using Boolean will force it to be wrapped as the Boolean class.

case sensitivity: If you want to allow 'y' or 'Y', force the input to a known case first. Since charAt() returns primitive char, you need to use the static Character.toUpperCase().

Solution:

boolean isY = Character.toUpperCase(in.next().charAt(0)) == 'Y'
// - OR - 
boolean isY = in.next().startsWith("Y") // not case-insensitive
charles-allen
  • 3,891
  • 2
  • 23
  • 35
1
Boolean international_F = "Y".equals(in.next()); // next  returns a string
Boolean international_F =in.next().charAt(0) == 'Y';
Syed Afzal
  • 259
  • 2
  • 12
1

You do not need a ternary operator to simply assign the result (true/false) of the evaluation of the condition. You need a ternary operator if you want to do something based on the result of the evaluation of the condition e.g.

import java.util.Scanner;

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner in = new Scanner(System.in);
        System.out.print("Do you want to continue? [Y/N]: ");
        boolean yes = in.nextLine().toUpperCase().charAt(0) == 'Y';
        if (yes) {
            System.out.println("You have chosen to continue");
        } else {
            System.out.println("You have chosen to stop");
        }

        // Or simply
        System.out.print("Do you want to continue? [Y/N]: ");
        if (in.nextLine().toUpperCase().charAt(0) == 'Y') {
            System.out.println("You have chosen to continue");
        } else {
            System.out.println("You have chosen to stop");
        }

        // You can use ternary operator if you want to do something based on the result
        // of evaluation of the condition e.g.
        System.out.print("Do you want to continue? [Y/N]: ");
        String response = in.nextLine().toUpperCase().charAt(0) == 'Y' ? "Yes" : "No";
        System.out.println(response);

        // Without a ternary operator, you would write it as:
        System.out.print("Do you want to continue? [Y/N]: ");
        String res;
        char ch = in.nextLine().toUpperCase().charAt(0);
        if (ch == 'Y') {
            res = "Yes";
        } else {
            res = "No";
        }
        System.out.println(res);
    }
}

A sample run:

Do you want to continue? [Y/N]: y
You have chosen to continue
Do you want to continue? [Y/N]: n
You have chosen to stop
Do you want to continue? [Y/N]: y
Yes
Do you want to continue? [Y/N]: n
No
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
0

This is an example demonstrating what you want to do:

char a = 'a';
char b = 'b';

Boolean b1 = (a == 'a') ? true : false;
Boolean b2 = (a == b) ? true : false;

System.out.println(b1);
System.out.println(b2);

The output will be:

true
false
Pritam Banerjee
  • 17,953
  • 10
  • 93
  • 108