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.

4
  • 4
    That should work. Why not just bool international_F = n.next() == 'Y'; though? Commented Feb 8, 2020 at 0:53
  • 2
    What is in? Otherwise the syntax is correct. You can totally do that. Commented Feb 8, 2020 at 0:53
  • 1
    Ok, let me try it again. 'in' was uh my Scanner. I think it's supposed to be for 'input'. Commented Feb 8, 2020 at 0:54
  • 3
    next returns a string not a char in Java. Change 'Y' to "Y" Commented Feb 8, 2020 at 1:00

4 Answers 4

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

Comments

1
Boolean international_F = "Y".equals(in.next()); // next  returns a string
Boolean international_F =in.next().charAt(0) == 'Y';

2 Comments

Might be worth noting these aren't equivalent. If the user types "yes" the former is false and the latter is true. You could use in.next().startsWith("Y").
maybe equalsIgnoreCase() ?
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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.