CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jan 2013
    Posts
    20

    serious help needed, java code

    I am new to Java and this site. My assignment is to write a program that displays a time in this format(00:00:00) in one line of code. can anyone help me with this. the following is what i have started......
    Please help!!!!!

    import java.util.Scanner;

    public class time {

    public static void main(String[] args){


    Scanner time = new Scanner(System.in);


    String hour = time.next();


    System.out.print("" + hour + ":";");"

    String min = time.next();
    String sec = time.next();



    }
    }

  2. #2
    Join Date
    Nov 2006
    Location
    Barcelona - Catalonia
    Posts
    364

    Re: serious help needed, java code

    I think you almost get it, just fix syntax in System.out.print, and do it after read all variables, because you can't use a variable before being declared.

    For example, assuming you already read all variables, next piece of code prints "hour:min"
    Code:
    System.out.print(hour + ":" + min);
    You only have to add "sec" variable. Easy, right?

    By the way, class name should start with an uppercase letter, by convention. Ie, Time better than time.

    Regards,

    Albert.
    Please, correct me. I'm just learning.... and sorry for my english :-)

  3. #3
    Join Date
    Jan 2013
    Posts
    20

    Re: serious help needed, java code

    Thank You AlbertGM for your response, I really do appreciate your assistance. Now i need to find out how to make this work with one entry instead of three. Here is the question/Assignment.....
    Create a modified version of this project as follows:

    Allow the user to enter the hours, minutes, and seconds in one prompt instead of three separate prompts. That is, the user would be allowed to represent 1 hour 28 minutes and 42 seconds by entering:

    1:28:42

    This will require you to use the colon ( as a delimeter in the scanned input.

    Can someone please help and guide me to the right solution? Thanks.

  4. #4
    Join Date
    Apr 2004
    Posts
    102

    Re: serious help needed, java code

    This will require you to use the colon ( as a delimeter in the scanned input.

    Can someone please help and guide me to the right solution? Thanks.
    IMHO, you should read the input using readLine. Thus, your input string would look as follows: 10:13:21

    You would then parse out the hours, minutes and seconds using the Scanner useDelimiter. For example useDelimiter("[:]")

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured