Results 1 to 5 of 5
Thread: am i doing this right.
- 10-29-2011, 12:23 AM #1
Member
- Join Date
- Sep 2011
- Posts
- 69
- Rep Power
- 0
am i doing this right.
hi all hope everybody is well..
i have this scenario
Write a program to input an hour and the minutes of a time and output it correctly formatted. For example, hour = 3 and minutes = 7 should be output as 03:07
Your program should use ternary operators in the output statement to determine whether or not to output a 0 before the hour and before the minutes.
this is my code so far (i think i am doing it right)
import java.io.*;
public class FormatTime
{
public static void main(String[] args)
{
//this will fetch our input values
BufferedReader usrInput = new BufferedReader( new InputStreamReader( System.in) );
System.out.println("Enter a number in seconds to format: ");
int secs = usrInput.readLine ();
int hours = secs / 3600,
remainder = secs % 3600,
minutes = remainder / 60,
seconds = remainder % 60;
string disHour = (hours < 10 ? "0" : "") + hours,
disMinu = (minutes < 10 ? "0" : "") + minutes ,
disSec = (seconds < 10 ? "0" : "") + seconds ;
System.out.println(disHour +":"+ disMinu+":"+disSec);
}
}
this is the error i am getting
H:\java practice>javac FormatTime.java
FormatTime.java:13: error: incompatible types
int secs = usrInput.readLine ();
^
required: int
found: String
FormatTime.java:20: error: cannot find symbol
string disHour = (hours < 10 ? "0" : "") + hours,
^
symbol: class string
location: class FormatTime
2 errors
can somebody help me pls
many thanks Andy...
- 10-29-2011, 12:33 AM #2
Senior Member
- Join Date
- Nov 2010
- Posts
- 210
- Rep Power
- 3
Re: am i doing this right.
If you're getting compiler errors, that's a fairly sure sign that you're not doing it right.
readLine() returns a String, not an int.Java Code:int secs = usrInput.readLine ();
Java is case-sensitive. string is not the same as String. Also, you probably meant to terminate the line with a semicolon, not a comma.Java Code:string disHour = (hours < 10 ? "0" : "") + hours,
Please use code tags when posting.
- 10-29-2011, 12:35 AM #3
Member
- Join Date
- Sep 2011
- Posts
- 69
- Rep Power
- 0
Re: am i doing this right.
sorry how do i use code tags when posting
thks andy..
- 10-29-2011, 12:57 AM #4
Member
- Join Date
- Sep 2011
- Posts
- 69
- Rep Power
- 0
Re: am i doing this right.
sorry i don't understand where i am going wrong
many thanks Andy..
import java.io.*;
public class FormatTime
{
public static void main(String[] args)
{
//this will fetch our input values
BufferedReader usrInput = new BufferedReader( new InputStreamReader( System.in));
System.out.println("Enter a number in seconds to format: ");
int = usrInput.readLine ();
int hours = secs / 3600;
remainder = secs % 3600;
minutes = remainder / 60;
seconds = remainder % 60;
int disHour = (hours < 10 ? "0" : "") + hours;
disMinu = (minutes < 10 ? "0" : "") + minutes;
disSec = (seconds < 10 ? "0" : "") + seconds ;
System.out.println(disHour +":"+ disMinu+":"+ disSec);
}
}
- 10-29-2011, 10:05 AM #5
Senior Member
- Join Date
- Nov 2010
- Posts
- 210
- Rep Power
- 3
Re: am i doing this right.
Post your code like this:
[code]
System.out.println("Hello world");
[/code]
This is invalid syntax. You're specifying the type (int), but not an identifier. Int is still the wrong type anyway, because readLine() returns a String.Java Code:int = usrInput.readLine ();
If you want to get an int from the user, look up java.util.Scanner and its nextInt() method.


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks