+ 12

What is error in this code ? Can anyone help me?

https://code.sololearn.com/cHX7L87qvvc6/?ref=app

25th Dec 2018, 5:52 AM
Amruta
Amruta - avatar
18 Antworten
+ 33
Amruta use it like this: public class Program { public static void main(String[] args) { System.out.println((10>9)?"10>9":"10<9"); } } Ternary operator has return type so its must return a value from ans either one of them....... Your function works on void method i.e print directly and it doesn't support void invocation inside it so it shows error.... Well I too didn't know that much.....so sorry😅 Hope this helps.....😊
25th Dec 2018, 6:14 AM
Ketan [#Be Happy 😄]
Ketan [#Be Happy 😄] - avatar
+ 20
Amruta They are ternary/conditional operators, not conditional statements ● U can replace them by if-else conditional statements in above code to make that work. Ketan U can remove 1 more bracket in condition 10>9, that will also work ☺
25th Dec 2018, 8:43 AM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 7
Ketan It works perfect But why its not working when using printing statements in ternary statement?
25th Dec 2018, 7:58 AM
Amruta
Amruta - avatar
+ 6
26th Dec 2018, 4:17 AM
Amruta
Amruta - avatar
+ 4
Ketan thanks for help
26th Dec 2018, 4:17 AM
Amruta
Amruta - avatar
+ 4
Ron Alvarez Yes I know that but I wanted to know the reason behind the error
26th Dec 2018, 4:53 AM
Amruta
Amruta - avatar
+ 4
System.out.println((10>9)?"10>9":"10<9"); Here you may put a string or a variable. I should do it this way: result = (10>9)?"10>9":"10<9" ; System.out.println(result)
26th Dec 2018, 2:39 PM
S1s
S1s - avatar
+ 3
It is a compile-time error for either operand expression to be an invocation of a void method and System.out.println is void (does not return any value). https://stackoverflow.com/questions/15977031/java-ternary-without-assignment
25th Dec 2018, 2:12 PM
Sevak Ekizyan
Sevak Ekizyan - avatar
+ 3
Sevak Ekizyan thanks for help
26th Dec 2018, 4:15 AM
Amruta
Amruta - avatar
+ 3
System.out.println((10>9)?"10>9":"10<9"); Here you may put a string or a variable. I should do it this way: result = (10>9)?"10>9":"10<9" ; System.out.println(result)
27th Dec 2018, 1:20 PM
saleh houshangi
saleh houshangi - avatar
+ 2
public class Program { public static void main(String[] args) { /*here you can declare 2 variables for 10 & 9 values*/ if (10>9) { System.out.println("10>9"); } else { System.out.println("10<9"); } } } /*I think! in java it should be like that😊*/
26th Dec 2018, 6:35 AM
Iftakher Hasan
Iftakher Hasan - avatar
+ 2
The ternary operator should always provide as a value. If you are going to make statements using ternaries you will get errors. So the ternary operator should go inside the println() method.
26th Dec 2018, 12:41 PM
Seniru
Seniru - avatar
+ 2
S1s yes its will work for ternary😀
26th Dec 2018, 5:14 PM
Iftakher Hasan
Iftakher Hasan - avatar
+ 1
I did it this way...is it okay for you...? public class Program { public static void main(String[] args) { int a=9; int b=10; System.out.println("10>9?"); if(10>9) { System.out.println("10>9"); } else{ System.out.println("10<9"); } } }
26th Dec 2018, 3:15 AM
Canche Emmanuel
Canche Emmanuel - avatar
+ 1
u can use if/else for better understanding
26th Dec 2018, 4:51 AM
Ron Alvarez
+ 1
It seems that kind of code is not used in Java...but if you try to do it that way it becomes more complicated
26th Dec 2018, 4:55 AM
Canche Emmanuel
Canche Emmanuel - avatar
+ 1
S1s your code above runs perfectly
26th Dec 2018, 6:27 PM
Canche Emmanuel
Canche Emmanuel - avatar
0
good great
31st Dec 2018, 1:57 PM
Firaol Gemechu
Firaol Gemechu - avatar