What is the error in the following code

+2 Arnab Banerjee · February 2, 2015
public class loops{
public static void main(String args[]){
cl1 obj1 = new cl1();
obj1.setTime(14, 28, 56);
System.out.println("Millitary time is " + obj1.railTime());
System.out.println("The regular time is " + obj1.regularTime());
}
}

public class cl1 {
private int hour;
private int miniute;
private int second;

public void setTime(int h, int m, int s){
hour = (h>=0 && h<24 ? h:0);
miniute = (m>=0 && m<60 ? m:0);
second = (s>=0 && s<60 ? s:0);
}
public String railTime(){
return String.format("%02d,%02d,%02d", hour,miniute,second);

}
public String regularTime(){
return String.format("%d","%02d", "%02d",(hour==0 || hour==12? 12:hour%12), miniute, second, (hour>12 ? "PM":"AM"));
}
}

Help me to find the error.  :p 

Post a Reply

Replies

Oldest  Newest  Rating
+1 Arnab Banerjee · February 2, 2015
This is the error message:

Exception in thread "main" java.util.IllegalFormatConversionException: d != java.lang.String
at java.util.Formatter$FormatSpecifier.failConversion(Formatter.java:4302)
at java.util.Formatter$FormatSpecifier.printInteger(Formatter.java:2793)
at java.util.Formatter$FormatSpecifier.print(Formatter.java:2747)
at java.util.Formatter.format(Formatter.java:2520)
at java.util.Formatter.format(Formatter.java:2455)
at java.lang.String.format(String.java:2927)
at cl1.regularTime(cl1.java:16)
at loops.main(loops.java:8)
+1 Jay Deshaun · February 2, 2015
Try this:
public class loops{
public static void main(String args[]){
cl1 obj1 = new cl1();
obj1.setTime(14, 28, 56);
System.out.println("Millitary time is " + obj1.railTime());
System.out.println("The regular time is " + obj1.regularTime());
}
}
public class cl1 {
private int hour,minute,second;

public void setTime(int h, int m, int s){
hour = (h>=0 && h<24 ? h:0);
minute = (m>=0 && m<60 ? m:0);
second = (s>=0 && s<60 ? s:0);
}
public String railTime(){
return String.format("%d, %d, %d", hour,minute,second);
}
public String regularTime(){
return String.format("%d %d %d %s",(hour==0 || hour==12? 12:hour/12), minute, second, (hour>12 ? "PM":"AM"));
}
}
+1 Arnab Banerjee · February 2, 2015
Nope it is not working.  :(
+1 Jay Deshaun · February 2, 2015
Yea, I read a manual for String.format and @Sam is right.

I've edited the code again, try it!
+1 Arnab Banerjee · February 2, 2015
Yea, got it right, but the code is not working yet. :(
+1 Arnab Banerjee · February 2, 2015
Okey got its solved thanks Jay, Sam & C student. :) 
+1 Jay Deshaun · February 2, 2015
No Problem
I think it was that "%" you left while checking if the hour is equal to 0 or 12 and you tried dividing it.
+1 Arnab Banerjee · February 2, 2015
Yea,  and I had to add another string format,  :P 
+1 Jay Deshaun · February 2, 2015
Nice to see you learned from your own mistakes :)

Best thing about being a programmer!
0 Arnab Banerjee · February 2, 2015
Actually no. :P , that message just flew over my head. :P 
  • 1

Java / Android Development

107,234 followers
About

Very popular language used to create desktop applications, website applets, and Android apps.

Links
Moderators
Bucky Roberts Administrator