In my opinion, the easiest thing is to indicate to the user the format with which you should enter the time
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Introduce la hora (HH:MM) ");
string horaCompleta = sc.nextLine();
System.out.println(horaCompleta);
sc.close();
}
If you then need to have the hour and minute data you have 2 options:
1- As you told the user to enter the time in HH: MM format and save it in a string, you can obtain the data as follows:
int hora = Integer.parseInt(horaCompleta.substring(0, 2));
int minutos = Integer.parseInt(horaCompleta.substring(3));
With that you would already have both data in int
format and you can use it in the way you need.
2- The other option is to ask you to enter 2 data instead of one:
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Introduce la hora ");
int hora = sc.nextInt();
System.out.println("Introduce los minutos ");
int minutos = sc.nextInt();
System.out.println(hora + ":" + minutos);
sc.close();
}
In this way you have the 2 data separately and shows them in the conventional 22:50 format.
I hope it helps you!