Results 1 to 5 of 5
  1. #1
    andnlou2678 is offline Member
    Join Date
    Sep 2011
    Posts
    69
    Rep Power
    0

    Default 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...

  2. #2
    Iron Lion is offline Senior Member
    Join Date
    Nov 2010
    Posts
    210
    Rep Power
    5

    Default 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.

    Java Code:
    int secs = usrInput.readLine ();
    readLine() returns a String, not an int.

    Java Code:
    string disHour = (hours < 10 ? "0" : "") + hours,
    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.

    Please use code tags when posting.

  3. #3
    andnlou2678 is offline Member
    Join Date
    Sep 2011
    Posts
    69
    Rep Power
    0

    Default Re: am i doing this right.

    sorry how do i use code tags when posting

    thks andy..

  4. #4
    andnlou2678 is offline Member
    Join Date
    Sep 2011
    Posts
    69
    Rep Power
    0

    Default 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);
    }

    }

  5. #5
    Iron Lion is offline Senior Member
    Join Date
    Nov 2010
    Posts
    210
    Rep Power
    5

    Default Re: am i doing this right.

    Post your code like this:

    [code]
    System.out.println("Hello world");
    [/code]


    Java Code:
    int = usrInput.readLine ();
    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.

    If you want to get an int from the user, look up java.util.Scanner and its nextInt() method.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •