0

I want to write multiple statements in Ternary Operator..But dont know how to write. I want like this,

public static void main(String[] args) {

    String replaceString,newString;
    Scanner user_input=new Scanner(System.in);
    String input=user_input.next();
    boolean choice=true;
    if(choice==true){
        System.out.println("Enter string to replace");
        replaceString=user_input.next();
        System.out.println("Enter new string");
        newString=user_input.next();
        input.replace(replaceString, newString);
        }
}

I want to write the above code in java using ternary operator.Please help

5
  • Not sure what you want to do here. Is there a reason why you want to use a ternary operator (instead of your if statement, I assume)? Commented Feb 16, 2020 at 14:41
  • I think your question has been answered here: stackoverflow.com/questions/33489121/… Commented Feb 16, 2020 at 14:46
  • @andrewjames yes for some reason i want to use ternary operator. Commented Feb 16, 2020 at 15:26
  • @kosmičák no this is not my type of question..I want to take input in ternary operator. Commented Feb 16, 2020 at 15:27
  • 1
    "for some reason" - kind of explanation that doesn't help at all (code does not fit for usage of ternary operator, actually the only condition is doing nothing, the if is not doing anything) Commented Feb 16, 2020 at 15:38

1 Answer 1

2

Not entirely sure what you want to do here, or why you want to do it. Also, I will focus on your specific question, and not on your use of Scanner.

Is there something about your sample code's if statement that you don't like?

Why do you want to use a ternary operator?

Bottom line: I think a ternary operator is a bad fit for what you may be trying to do.

The Java ternary operator is a one-line way of writing a simplified if...else... assignment. In your sample code, you have an if but no else - and no obvious assignment - unless you mean to assign the result of this line...

input.replace(replaceString, newString);

...to a String variable. I may be completely wrong about your intention.

The values of a ternary operator (after the ? question-mark and separated by the : colon) can be assigned by methods, as long as the methods return the expected type.

The following uses a ternary operator:

//
// BAD use of the ternary operator - NOT RECOMMENDED:
//
public static void main(String[] args) {
    Scanner userInput = new Scanner(System.in);
    String input = userInput.next();
    boolean choice = true;
    String replaced = (choice == true) ? whenTrue(userInput, input) : null;
}

private static String whenTrue(Scanner userInput, String input) {
    System.out.println("Enter string to replace");
    String replaceString = userInput.next();
    System.out.println("Enter new string");
    String newString = userInput.next();
    return input.replace(replaceString, newString);
}

You would be better off using if, in my opinion.

Sign up to request clarification or add additional context in comments.

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.