Click here to Skip to main content
15,889,116 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Java
import java.util.Scanner;
public class Character {
	char ch;
	int i;
	
	public void acceptData() {
		Scanner in = new Scanner(System.in);
		System.out.println("Enter any character");
		ch = in.nextLine().charAt(0);
		i = (int) ch;
	//public void check() {
	boolean b1 = (91 > i > 64) ;
	if(b1){
	String str = b1 ? "upper case" : rts;	
	}
	boolean b2 = (123 > i > 95);
	
	String rts = b2 ? "lower case" : "special character";
	}
	
	public static void main(String args[]) {
		Character obj = new Character();
		obj.acceptData();
		//obj.check();
		System.out.println(str);
	}
}


What I have tried:

in this i tried to write the prgramm using the ternary operator and it shows the compilation error and i want to ignore the if else conditionals what are the corrections i should do to run the program correctly?
Posted
Updated 7-Sep-16 4:45am
v2
Comments
[no name] 7-Sep-16 10:01am    
Fix the errors would be a good start.
Maciej Los 7-Sep-16 10:34am    
What's wrong with if... else..?

1 solution

Not sure what else you wanted done as ternary operators but here you go.

Java
import java.util.Scanner;
public class Character {
	char ch;
	int i;
	
	public void acceptData() {
		Scanner in = new Scanner(System.in);
		System.out.println("Enter any character");
		ch = in.nextLine().charAt(0);
		i = (int) ch;
	
         //Ternary corrections here
	//b2 in use by rts, moved to top
	boolean b2 = (123 > i && i > 95);
	// RTS being called prior to being declared, moved to top
	String rts = b2 ? "lower case" : "special character";
	
	boolean b1 = (91 > i && i > 64) ;
	String str = b1 ? "upper case" : rts;	
	
	
	
	}
	
	public static void main(String args[]) {
		Character obj = new Character();
		obj.acceptData();
		//obj.check();
		System.out.println(str);
	}
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900