Reputation: 55
I'm trying to write a simple console application that will accept a string from the keyboard that represents hours and minutes in the HH:MM form and then the string must then be put into two integers for hours and mins before showing the total minutes so 02:20 would add up to 140 minutes. I feel like I'm close but I'm not entirely positive I'm doing it right. I'm very new to Java so if there's anything I'm doing wrong please let me know and any help would be very appreciated! As of now my code is presenting a number far too long?
import java.util.Scanner;
public class TimeAssignment {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
final double minConv = 0.0166667;
final double hourConv = 60;
int hours;
System.out.print("Enter the hours to convert:");
hours = input.nextInt();
int minutes;
System.out.print("Enter the minutes to convert:");
minutes = input.nextInt();
System.out.println("Total minutes: "+hours/hourConv+minutes/minConv);
}
}
Upvotes: 0
Views: 7697
Reputation: 957
Fixed: Multiply hours by 60 then add your minutes.
import java.util.Scanner;
public class TimeAssignment {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
final double minConv = 0.0166667;
final double hourConv = 60;
int hours;
System.out.print("Enter the hours to convert:");
hours = input.nextInt();
int minutes;
System.out.print("Enter the minutes to convert:");
minutes = input.nextInt();
System.out.println("Total minutes: "+((hours * 60)+minutes));
}
}
Upvotes: 3
Reputation: 649
If you want the result of 02:20 to 140 then you have just have to do this:
System.out.println("Total minutes: "+(hours*hourConv+minutes));
Upvotes: 2
Reputation: 11
you can use simple conversion of hours to minutes by just multiplying with 60 and add this with user entered minutes .
Upvotes: 1