I am getting an error when printing a large number, minutes and seconds become negative. I think it is due to the size of the number that is printing.
import java.util.Scanner;
public class Minutes
{
public static void main(String[] args)
{
//User information
Scanner in = new Scanner(System.in);
System.out.println("Please enter a number of hours, days, weeks or years: ");
int s = in.nextInt();
//Compute input
int hours = s * 60;
int days = s * 1440;
int weeks = s * 10080;
int years = s * 525600;
//Print results
System.out.println("------------------------------------------------");
System.out.println("Here are your results!");
System.out.println("If you entered hours, this is the number of minutes" + ": " + hours);
System.out.println("If you entered days, this is the number of minutes" + ": " + days);
System.out.println("If you entered weeks, this is the number of minutes" + ": " + weeks);
System.out.println("If you entered years, this is the number of minutes" + ": " + years);
}
}
Thats exactly it, you are having a integer overflow, when that happens, java starts the count from the Integer.MIN_VALUE that why i shows negative, convert it to long ant it should be fine.
Related
My code isn't working properly for example when a user put that right now is 5 h 43 m and 7 s and the user wanna add 3 h 50 m and 57 s the code compute and shows what will be the time adding but it shows 8 h 93 m and 64 s but I want that after 60 m it shows 9 h 34 m and 4 s so can u help me out.
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner user_input = new Scanner (System.in);
int startup_hour;
int startup_minute;
int startup_second;
int add_hours;
int add_minutes;
int add_seconds;
System.out.print("what time is it right now(hour)? \n");
startup_hour = user_input.nextInt();
System.out.print("what time is it right now(minutes? \n");
startup_minute = user_input.nextInt();
System.out.print("what time is it right now(seconds)? \n");
startup_second = user_input.nextInt();
System.out.println("The starting time is " + startup_hour
+ " hours " + startup_minute + " minutes " + "and "
+ startup_second + " seconds \n");
System.out.print("How many hours you wanna add? \n");
add_hours = user_input.nextInt();
System.out.print("How many minutes you wanna add? \n");
add_minutes = user_input.nextInt();
System.out.print("How many seconds you wanna add? \n");
add_seconds = user_input.nextInt();
System.out.println("The user wanna add " + add_hours
+ " hours " + add_minutes + " minutes "
+ "and " + add_seconds + " seconds \n");
int totalHours = (startup_hour + add_hours);
int totalMinutes = (startup_minute + add_minutes);
int totalSeconds = (startup_second + add_seconds);
if (totalSeconds == 60){
totalMinutes++;
totalSeconds = 0;
}
if (totalMinutes == 60){
totalHours++;
totalMinutes = 0;
}
System.out.println("After adding, the time would then be "
+ totalHours + " hours " + totalMinutes + " Minutes "
+ totalSeconds + " Seconds ");
*emphasized text*
}
}
thanku
The reason your program is not working because you are processing time, without using the standard unit, which is seconds.
For example:
Suppose the start up time is 1 hours 3 minutes and 57 seconds.
And the user want to add, 1 hour 57 minutes and 3 seconds.
The correct answer will be, 3 hours 1 Minutes 0 Seconds but your program will return 2 hours 61 Minutes 0 Seconds.
Now, why did this happen?
The reason are:
As already stated, you did not process time using the standard unit (seconds).
The condition in your if loop is not correct. You are only checking if the minutes/seconds are equal to 60. What if the minutes or seconds are 61 or more?
Solution:
The simplest solution is, first convert time into seconds, add how much time you want to add, then convert it back to hours:minutes:seconds. You won't even have to use if loop if you process time using seconds.
Here is the modified code, which works properly :
import java.util.Scanner;
public class Test
{
public static void main(String[] args)
{
Scanner user_input = new Scanner(System.in);
int startup_hour;
int startup_minute;
int startup_second;
int add_hours;
int add_minutes;
int add_seconds;
System.out.print("What time is it right now(hour) : ");
startup_hour = user_input.nextInt();
System.out.print("What time is it right now(minutes) : ");
startup_minute = user_input.nextInt();
System.out.print("What time is it right now(seconds) : ");
startup_second = user_input.nextInt();
System.out.println("The starting time is " + startup_hour + " hours " + startup_minute + " minutes "
+ "and " + startup_second + " seconds.");
System.out.println();
System.out.print("How many hours you wanna add : ");
add_hours = user_input.nextInt();
System.out.print("How many minutes you wanna add : ");
add_minutes = user_input.nextInt();
System.out.print("How many seconds you wanna add : ");
add_seconds = user_input.nextInt();
System.out.println("The user wanna add " + add_hours + " hours " + add_minutes + " minutes "
+ "and " + add_seconds + " seconds.");
System.out.println();
int totalSecondsAtStart = (startup_hour * 60 * 60) + (startup_minute * 60) + startup_second;
int totalSecondsToAdd = (add_hours * 60 * 60) + (add_minutes * 60) + (add_seconds);
int totalSeconds = totalSecondsAtStart + totalSecondsToAdd;
//Convert total seconds to hour, minutes and seconds;
int totalMinutes = (totalSeconds / 60);
int totalHours = (totalMinutes / 60);
int finalHours = totalHours;
int finalMinutes = totalMinutes - (totalHours * 60);
int finalSeconds = totalSeconds - (totalMinutes * 60);
System.out.println("After adding, the time would then be " + finalHours + " hours"
+ " " + finalMinutes + " Minutes " + finalSeconds + " Seconds.");
}
}
Notice how I converted time back into hours:minutes:seconds.
i have a question. When i run the code i get an error on the inputting the conversion rates. Could someone please help me in the right direction? I've copied part of the code and the error message.
Code:
public static void travelTimeAndBudget() {
Scanner input = new Scanner(System.in);
System.out.print("How many days are you going to spend travelling? ");
int days = input.nextInt();
System.out.print("How much money, in USD, are you planning to spend on your trip? ");
int usd = input.nextInt();
System.out.print("What is the three letter currency symbol for your travel destination? ");
String currency = input.next();
double currencyConvert;
System.out.print("How many " + currency + " are there in 1 USD? ");
currencyConvert = input.nextDouble();
double currencyTotal = (currencyConvert * (double)usd);
double currencyDay = (currencyTotal / (double)days);
System.out.println("If you are travelling for " + days + " days that is the same as" + (days * 24) + " or " + (days * 360) + " minutes");
System.out.println("If you are going to spend $" + usd + " USD that means per day you can spend up to $" + ((double)usd / days) + " USD");
System.out.println("Your total budget in " + currency + " is " + currencyTotal + " " + currency + ", which per day is " + currencyDay + currency);
}
Output + error:
How many days are you going to spend travelling? 14
How much money, in USD, are you planning to spend on your trip? 2300
What is the three letter currency symbol for your travel destination? MXC
How many MXC are there in 1 USD? 19.8
Exception in thread "main" java.util.InputMismatchException
at java.base/java.util.Scanner.throwFor(Scanner.java:939)
at java.base/java.util.Scanner.next(Scanner.java:1594)
at java.base/java.util.Scanner.nextDouble(Scanner.java:2564)
at TripPlanner.travelTimeAndBudget(TripPlanner.java:33)
at TripPlanner.main(TripPlanner.java:8)
Any help is very much appreciated.
try this :
Scanner scanner = new Scanner(System.in).useLocale(Locale.US);
A useful tip for tracing bugs is to check the call stack. At the bottom of the error message you have:
at TripPlanner.travelTimeAndBudget(TripPlanner.java:33)
at TripPlanner.main(TripPlanner.java:8)
This tells you lines where your exception is being caught. Line 33 of TripPlanner.java is where to start debugging your code
This question already has answers here:
How to format a number 0..9 to display with 2 digits (it's NOT a date)
(7 answers)
Closed 6 years ago.
package timeConversion;
import java.util.Scanner;
public class TimeConversion {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Please enter the time in minutes: ");
int time = input.nextInt();//time in minutes
int hour = time / 60;// hour
int minute = time % 60; //minutes
System.out.print("The time is: " + hour + ":" + minute);
}
}
So I'm having trouble with values in the minute spot being less than 10. for example, when you time in 184 minutes, the output is 3:4, but i would like it to read 3:04. how could I go about doing this ?
Just a quick hack
if(minute < 10)
System.out.println("The time is: " + hour + ":0" + minute);
else
System.out.print("The time is: " + hour + ":" + minute);
I am doing this for a programming class and I keep getting these errors when I go to compile the program in jGrasp:
TimeCalculator.java:94: error: variable dblMinutes might not have been initialized
+ timeFormat.format(dblMinutes) + " minutes, and " + timeFormat.format(dblSecondsAfterMinutes) + " seconds.");
^
TimeCalculator.java:94: error: variable dblSecondsAfterMinutes might not have been initialized
+ timeFormat.format(dblMinutes) + " minutes, and " + timeFormat.format(dblSecondsAfterMinutes) + " seconds.");
^
TimeCalculator.java:98: error: variable dblMinutes might not have been initialized
JOptionPane.showMessageDialog(null, "There are " + timeFormat.format(dblHours) + " hours, " + timeFormat.format(dblMinutes) + " minutes, and "
^
TimeCalculator.java:99: error: variable dblSecondsAfterMinutes might not have been initialized
+ timeFormat.format(dblSecondsAfterMinutes) + " seconds.");
^
TimeCalculator.java:101: error: variable dblMinutes might not have been initialized
else if (dblMinutes >= 1)
^
TimeCalculator.java:102: error: variable dblSecondsAfterMinutes might not have been initialized
JOptionPane.showMessageDialog(null, "There are " + timeFormat.format(dblMinutes) + " minutes and " + timeFormat.format(dblSecondsAfterMinutes) + " seconds.");
^
6 errors
My program is supposed to take user input (an amount in seconds) and convert it to days, hours, minutes, and seconds. It then should display the final amount.
Here is my code so far:
/*
This program prompts the user to enter an amount of seconds.
When the user enters an amount, it is stored in a variable and
calculated to how many days, hours, minutes, and seconds there
are in the given amount of seconds. The information is then
output for the user to read.
60 seconds = 1 minute
3600 seconds = 1 hour
86400 seconds = 1 day
*/
import javax.swing.JOptionPane;
import java.text.DecimalFormat;
public class TimeCalculator
{
public static void main(String[] args)
{
DecimalFormat timeFormat = new DecimalFormat("#0.#");//used for formatting output
//declaring needed variables
String strStartingSeconds;
double dblStartingSeconds, dblMinutes, dblHours, dblDays, dblSecondsAfterDays, dblSecondsAfterHours, dblSecondsAfterMinutes;
//get input from user for amount of seconds
strStartingSeconds = JOptionPane.showInputDialog("Enter the amount of seconds: ");
//convert input to a double
dblStartingSeconds = Double.parseDouble(strStartingSeconds);
if (dblStartingSeconds >= 86400)//check to see if it is one day or more
{
dblDays = dblStartingSeconds / 86400;//find how many days
dblSecondsAfterDays = dblStartingSeconds % 86400;//calculate how many seconds are left by finding the remainder
if (dblSecondsAfterDays >= 3600)//check to see if there is one hour or more
{
dblHours = dblSecondsAfterDays / 3600;//find how many hours
dblSecondsAfterHours = dblSecondsAfterDays % 3600;//calculate how many seconds are left by finding the remainder
if (dblSecondsAfterHours >= 60)//Check to see if there is one or more minutes
{
dblMinutes = dblSecondsAfterHours / 60;//Calculate how many minutes
dblSecondsAfterMinutes = dblSecondsAfterHours % 60;//calculate how many seconds are left by finding the remainder
}
else
{
dblMinutes = 0;//If there wasn't enough seconds, assign minutes as 0
}
}
else
dblHours = 0;//If there wasn't enough hours, assign hours as 0
}
else
{
dblDays = 0;//assign days as 0 since there wasn't enough seconds
if (dblStartingSeconds >= 3600)//check to see if there is one hour or more
{
dblHours = dblStartingSeconds / 3600;//find how many hours
dblSecondsAfterHours = dblStartingSeconds % 3600;//calculate how many seconds are left by finding the remainder
if (dblSecondsAfterHours >= 60)//Check to see if there is one or more minutes
{
dblMinutes = dblSecondsAfterHours / 60;//Calculate how many minutes
dblSecondsAfterMinutes = dblSecondsAfterHours % 60;//calculate how many seconds are left by finding the remainder
}
else
{
dblMinutes = 0;//assign minutes as 0 since there wasn't enough seconds
}
}
else
{
dblHours = 0;//assign hours as 0 since there wasn't enough hours
if (dblStartingSeconds >= 60)//Check to see if there is one or more minutes
{
dblMinutes = dblStartingSeconds / 60;//Calculate how many minutes
dblSecondsAfterMinutes = dblStartingSeconds % 60;//calculate how many seconds are left by finding the remainder
}
else
{
dblMinutes = 0;//Assign minutes as 0 since there wasn't enough seconds
JOptionPane.showMessageDialog(null, "There are " + timeFormat.format(dblStartingSeconds) + " seconds.");//Displays how many seconds there was
}
}
}
//Display the correct amount of time based on whether or not there was enough seconds for days, hours, or minutes
if (dblDays >= 1)
{
JOptionPane.showMessageDialog(null, "There are " + timeFormat.format(dblDays) + " days, " + timeFormat.format(dblHours) + " hours, "
+ timeFormat.format(dblMinutes) + " minutes, and " + timeFormat.format(dblSecondsAfterMinutes) + " seconds.");
}
else if (dblHours >= 1)
{
JOptionPane.showMessageDialog(null, "There are " + timeFormat.format(dblHours) + " hours, " + timeFormat.format(dblMinutes) + " minutes, and "
+ timeFormat.format(dblSecondsAfterMinutes) + " seconds.");
}
else if (dblMinutes >= 1)
JOptionPane.showMessageDialog(null, "There are " + timeFormat.format(dblMinutes) + " minutes and " + timeFormat.format(dblSecondsAfterMinutes) + " seconds.");
else
JOptionPane.showMessageDialog(null, "There are " + timeFormat.format(dblStartingSeconds) + " seconds.");
System.exit(0);//close application
}
}
I have no joke, spent hours looking through this and can't find what I'm doing wrong! I'm pretty new to programming, so anything will help (I probably did something stupid and obvious to an expert programmer).
Thanks,
-Cashe
double dblMinutes = 0;
Read about initializing fields here: http://docs.oracle.com/javase/tutorial/java/javaOO/initial.html
Aside from your initialization issue... java.util.concurrent.TimeUnit is your best friend when dealing with converting units of time.
I'm trying to write this compounding interest program with a do while loop at the end and I cannot figure out how to print out the final amount.
Here is the code I have so far :
public static void main(String[] args) {
double amount;
double rate;
double year;
System.out.println("This program, with user input, computes interest.\n" +
"It allows for multiple computations.\n" +
"User will input initial cost, interest rate and number of years.");
Scanner keyboard = new Scanner(System.in);
System.out.println("What is the initial cost?");
amount = keyboard.nextDouble();
System.out.println("What is the interest rate?");
rate = keyboard.nextDouble();
rate = rate/100;
System.out.println("How many years?");
year = keyboard.nextInt();
for (int x = 1; x < year; x++){
amount = amount * Math.pow(1.0 + rate, year);
}
System.out.println("For " + year + " years an initial " + amount + " cost compounded at a rate of " + rate + " will grow to " + amount);
String go = "n";
do{
System.out.println("Continue Y/N");
go = keyboard.nextLine();
}while (go.equals("Y") || go.equals("y"));
}
}
The trouble is, amount = amount * Math.pow(1.0 + rate, year);. You're overwriting the original amount with the calculated amount. You need a separate value to hold the calculated value while still holding the original value.
So:
double finalAmount = amount * Math.pow(1.0 + rate, year);
Then in your output:
System.out.println("For " + year + " years an initial " + amount +
" cost compounded at a rate of " + rate + " will grow to " + finalAmount);
EDIT: Alternatively, you can save a line, a variable, and just do the calculation inline, as such:
System.out.println("For " + year + " years an initial " + amount +
" cost compounded at a rate of " + rate + " will grow to " +
(amount * Math.pow(1.0 + rate, year)));