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();
}
}
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.
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.
Re: serious help needed, java code
Quote:
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("[:]")