Click here to Skip to main content
15,887,875 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
New to Java
hey in my enhanced if statement
if have this programme
in which if have written time how do I convert from num to string as you can see it cannot
write AM and giving an error

What I have tried:

public class Hello {

public static void main(String args[]) {

Scanner bucky=new Scanner(System.in);
int h,m,s,hour,miniute,second,time;
h=28;
m=33;
s=55;
hour=((h>=0&&h<12)?h:0);
miniute=((m>=0&&m<60)?m:0);
second=((s>=0&&s<60)?s:0);
time=((h<12)?"AM":"PM");
System.out.printf("%02d:%02d:%02d%s",hour,miniute,second,time);
}
}
Posted
Updated 28-Oct-17 14:16pm

1 solution

The variable time is declared as int, but you try to assign a literal string value to it in this line:

time=((h<12)?"AM":"PM");


Java enforces type safety and will not allow this.
 
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