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 need some tips on how to get my 2nd and 4th percentage in a ".5%" format because in my result I get the result correct but it prints in a whole percentage. So instead of it showing 4%, 4.5%, 5%, 5.5%, and 6% it gives. 4%, 4%, 5%, 6%, 6% but it still calculates the result correctly. Its like it doesn't show the 4.5% and 5.5%. Heres the code, it may seem long but its just repetitive numbers. Note aIR2 and aIR4, those are the targets
import java.util.Scanner;
import java.text.NumberFormat;
import java.text.DecimalFormat;
public class MortgageCalculator {
public static void main(String[] args) {
double aIR, mortgageAmount;
int noY;
double aIR1, aIR2, aIR3, aIR4, aIR5;
double mIR1, mIR2, mIR3, mIR4, mIR5;
double mPayment1, mPayment2, mPayment3, mPayment4, mPayment5;
double totAmount1, totAmount2, totAmount3, totAmount4, totAmount5;
double oPay1, oPay2, oPay3, oPay4, oPay5;
double oPaypercent1, oPaypercent2, oPaypercent3, oPaypercent4, oPaypercent5;
//This establishes formats for values
NumberFormat fmt1 = NumberFormat.getPercentInstance();
NumberFormat fmt2 = NumberFormat.getCurrencyInstance();
DecimalFormat fmt3 = new DecimalFormat("0.##");
//This asks user for inputs
Scanner scan = new Scanner(System.in);
System.out.println("Enter the Annual Interest Rate: ");
aIR= scan.nextDouble();
System.out.println("Enter the Number of Years you will Pay: ");
noY = scan.nextInt();
System.out.println("Enter Amount Borrowed from the Bank: ");
mortgageAmount = scan.nextInt();
//These will give annual interest rates depending on range
aIR1 = (aIR - 1)/100;
aIR2 = (aIR - 0.5)/100;
aIR3 = (aIR)/100;
aIR4 = (aIR + 0.5)/100;
aIR5 = (aIR + 1)/100;
//These give rates by month according to which rate being taken
mIR1 = (aIR1 / 12);
mIR2 = (aIR2 / 12);
mIR3 = (aIR3 / 12);
mIR4 = (aIR4 / 12);
mIR5 = (aIR5 / 12);
//This takes the amounts per month
mPayment1 = (mIR1 * mortgageAmount)/(1-(1/Math.pow((1+mIR1),12*noY)));
mPayment2 = (mIR2 * mortgageAmount)/(1-(1/Math.pow((1+mIR2),12*noY)));
mPayment3 = (mIR3 * mortgageAmount)/(1-(1/Math.pow((1+mIR3),12*noY)));
mPayment4 = (mIR4 * mortgageAmount)/(1-(1/Math.pow((1+mIR4),12*noY)));
mPayment5 = (mIR5 * mortgageAmount)/(1-(1/Math.pow((1+mIR5),12*noY)));
//This takes the total amount per year
totAmount1 = mPayment1 * (noY*12);
totAmount2 = mPayment2 * (noY*12);
totAmount3 = mPayment3 * (noY*12);
totAmount4 = mPayment4 * (noY*12);
totAmount5 = mPayment5 * (noY*12);
//This is the overpayment because of interest
oPay1 = totAmount1 - (mortgageAmount);
oPay2 = totAmount2 - (mortgageAmount);
oPay3 = totAmount3 - (mortgageAmount);
oPay4 = totAmount4 - (mortgageAmount);
oPay5 = totAmount5 - (mortgageAmount);
//This is the overpayment percentage
oPaypercent1 = (oPay1/mortgageAmount);
oPaypercent2 = (oPay2/mortgageAmount);
oPaypercent3 = (oPay3/mortgageAmount);
oPaypercent4 = (oPay4/mortgageAmount);
oPaypercent5 = (oPay5/mortgageAmount);
//Begins printing the results in a line
System.out.println("The Mortgage Amount is: "
+ fmt2.format(mortgageAmount));
System.out.println("The Number of Years the Mortgage is Held: " + noY);
System.out.println("Range of Interest Rates: " + fmt1.format(aIR1)
+ " - " + fmt1.format(aIR5));
System.out.println("Interest Rate Monthly Payment Total Payment"
+ " $ Overpayment % Overpayment");
//Prints first interest rate
System.out.println("" + fmt1.format(aIR1) + " " +
fmt2.format(mPayment1) + " " + fmt2.format(totAmount1)
+ " " + fmt2.format(oPay1) + " "
+ fmt1.format(oPaypercent1) + "");
//Prints second interest rate
System.out.println("" + fmt1.format(aIR2) + " " +
fmt2.format(mPayment2) + " " + fmt2.format(totAmount2)
+ " " + fmt2.format(oPay2) + " "
+ fmt1.format(oPaypercent2) + "");
//Prints third interest rate
System.out.println("" + fmt1.format(aIR3) + " " +
fmt2.format(mPayment3) + " " + fmt2.format(totAmount3)
+ " " + fmt2.format(oPay3) + " "
+ fmt1.format(oPaypercent3) + "");
//Prints fourth interest rate
System.out.println("" + fmt1.format(aIR4) + " " +
fmt2.format(mPayment4) + " " + fmt2.format(totAmount4)
+ " " + fmt2.format(oPay4) + " "
+ fmt1.format(oPaypercent4) + "");
//Prints fifth interest rate
System.out.println("" + fmt1.format(aIR5) + " " +
fmt2.format(mPayment5) + " " + fmt2.format(totAmount5)
+ " " + fmt2.format(oPay5) + " "
+ fmt1.format(oPaypercent5) + "");
}
}
If you want a NumberFormat that displays 4% and 4.5%, i.e. percentage values with a single optional fraction digit, do one of the following:
// Option 1
NumberFormat fmt = NumberFormat.getPercentInstance();
fmt.setMaximumFractionDigits(1);
// Option 2
NumberFormat fmt = new DecimalFormat("0.#%");
Formatting the interest rate should be changed to handle decimal percentage values.
import java.util.Scanner;
import java.text.NumberFormat;
import java.text.DecimalFormat;
public class HelloWorld {
public static void main(String[] args) {
double aIR, mortgageAmount;
int noY;
double aIR1, aIR2, aIR3, aIR4, aIR5;
double mIR1, mIR2, mIR3, mIR4, mIR5;
double mPayment1, mPayment2, mPayment3, mPayment4, mPayment5;
double totAmount1, totAmount2, totAmount3, totAmount4, totAmount5;
double oPay1, oPay2, oPay3, oPay4, oPay5;
double oPaypercent1, oPaypercent2, oPaypercent3, oPaypercent4, oPaypercent5;
//This establishes formats for values
NumberFormat fmt1 = new DecimalFormat("0.#%");
//NumberFormat fmt1 = NumberFormat.getPercentInstance();
NumberFormat fmt2 = NumberFormat.getCurrencyInstance();
DecimalFormat fmt3 = new DecimalFormat("0.##");
//This asks user for inputs
Scanner scan = new Scanner(System.in);
System.out.println("Enter the Annual Interest Rate: ");
aIR= scan.nextDouble();
System.out.println("Enter the Number of Years you will Pay: ");
noY = scan.nextInt();
System.out.println("Enter Amount Borrowed from the Bank: ");
mortgageAmount = scan.nextInt();
//These will give annual interest rates depending on range
aIR1 = (aIR - 1)/100;
aIR2 = (aIR - 0.5)/100;
aIR3 = (aIR)/100;
aIR4 = (aIR + 0.5)/100;
aIR5 = (aIR + 1)/100;
//These give rates by month according to which rate being taken
mIR1 = (aIR1 / 12);
mIR2 = (aIR2 / 12);
mIR3 = (aIR3 / 12);
mIR4 = (aIR4 / 12);
mIR5 = (aIR5 / 12);
//This takes the amounts per month
mPayment1 = (mIR1 * mortgageAmount)/(1-(1/Math.pow((1+mIR1),12*noY)));
mPayment2 = (mIR2 * mortgageAmount)/(1-(1/Math.pow((1+mIR2),12*noY)));
mPayment3 = (mIR3 * mortgageAmount)/(1-(1/Math.pow((1+mIR3),12*noY)));
mPayment4 = (mIR4 * mortgageAmount)/(1-(1/Math.pow((1+mIR4),12*noY)));
mPayment5 = (mIR5 * mortgageAmount)/(1-(1/Math.pow((1+mIR5),12*noY)));
//This takes the total amount per year
totAmount1 = mPayment1 * (noY*12);
totAmount2 = mPayment2 * (noY*12);
totAmount3 = mPayment3 * (noY*12);
totAmount4 = mPayment4 * (noY*12);
totAmount5 = mPayment5 * (noY*12);
//This is the overpayment because of interest
oPay1 = totAmount1 - (mortgageAmount);
oPay2 = totAmount2 - (mortgageAmount);
oPay3 = totAmount3 - (mortgageAmount);
oPay4 = totAmount4 - (mortgageAmount);
oPay5 = totAmount5 - (mortgageAmount);
//This is the overpayment percentage
oPaypercent1 = (oPay1/mortgageAmount);
oPaypercent2 = (oPay2/mortgageAmount);
oPaypercent3 = (oPay3/mortgageAmount);
oPaypercent4 = (oPay4/mortgageAmount);
oPaypercent5 = (oPay5/mortgageAmount);
//Begins printing the results in a line
System.out.println("The Mortgage Amount is: "
+ fmt2.format(mortgageAmount));
System.out.println("The Number of Years the Mortgage is Held: " + noY);
System.out.println("Range of Interest Rates: " + fmt1.format(aIR1)
+ " - " + fmt1.format(aIR5));
System.out.println("Interest Rate Monthly Payment Total Payment"
+ " $ Overpayment % Overpayment");
//Prints first interest rate
System.out.println("" + fmt1.format(aIR1) + " " +
fmt2.format(mPayment1) + " " + fmt2.format(totAmount1)
+ " " + fmt2.format(oPay1) + " "
+ fmt1.format(oPaypercent1) + "");
//Prints second interest rate
System.out.println("" + fmt1.format(aIR2) + " " +
fmt2.format(mPayment2) + " " + fmt2.format(totAmount2)
+ " " + fmt2.format(oPay2) + " "
+ fmt1.format(oPaypercent2) + "");
//Prints third interest rate
System.out.println("" + fmt1.format(aIR3) + " " +
fmt2.format(mPayment3) + " " + fmt2.format(totAmount3)
+ " " + fmt2.format(oPay3) + " "
+ fmt1.format(oPaypercent3) + "");
//Prints fourth interest rate
System.out.println("" + fmt1.format(aIR4) + " " +
fmt2.format(mPayment4) + " " + fmt2.format(totAmount4)
+ " " + fmt2.format(oPay4) + " "
+ fmt1.format(oPaypercent4) + "");
//Prints fifth interest rate
System.out.println("" + fmt1.format(aIR5) + " " +
fmt2.format(mPayment5) + " " + fmt2.format(totAmount5)
+ " " + fmt2.format(oPay5) + " "
+ fmt1.format(oPaypercent5) + "");
}
Output
Interest Rate Monthly Payment Total Payment $ Overpayment % Overpayment
4% $1,841.65 $110,499.13 $10,499.13 10.5%
4.5% $1,864.30 $111,858.12 $11,858.12 11.9%
5% $1,887.12 $113,227.40 $13,227.40 13.2%
5.5% $1,910.12 $114,606.97 $14,606.97 14.6%
6% $1,933.28 $115,996.81 $15,996.81 16%
so I'm writing a program that gives the number of minutes for every input of x seconds... now the issue is that once I type in the first value, it asks for another value and divides that....and another value...and another...and so on... how can I get it to only give me one value and finish with that one value instead of a never-ending thing?
import javax.swing.JOptionPane;
class TimeCalculator {
public static void main(String[] args) {
double seconds;
String input;
input = JOptionPane.showInputDialog("Enter any number of seconds");
seconds = Double.parseDouble(input);
if (seconds >= 60);
JOptionPane.showInputDialog(null, "There are " + (seconds/60) + " minutes in " + seconds + " seconds.");
if (seconds >= 3600);
JOptionPane.showInputDialog(null, "There are " + (seconds/60) + " minutes in " + seconds + " seconds.");
if (seconds >= 86400);
JOptionPane.showInputDialog(null, "There are " + (seconds/60) + " minutes in " + seconds + " seconds.");
System.exit(0);
}
}
First of all - remove the semicolons after each if statement. Secondly, change the showInputDialog to showMessageDialog when you are not asking for input. Thirdly, correct the logic of your code:
class TimeCalculator{
public static void main(String[] args) {
double seconds;
String input;
input = JOptionPane.showInputDialog("Enter any number of seconds");
seconds = Double.parseDouble(input);
if (seconds >= 60)
JOptionPane.showMessageDialog(null, "There are " + (seconds / 60) + " minutes in " + seconds + " seconds.");
if (seconds >= 3600)
JOptionPane.showMessageDialog(null, "There are " + (seconds / 3600) + " hours in " + seconds + " seconds.");
if (seconds >= 86400)
JOptionPane.showMessageDialog(null,
"There are " + (seconds / 86400) + " days in " + seconds + " seconds.");
System.exit(0);
}
}
I am trying to write a program in which the console tells a person the difference between two times WITHOUT IF STATEMENTS, in "military time" or 24 hr time. So far, I have:
import java.util.Scanner;
public class MilTimeDiff {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.print("Enter the first time: ");
String time1 = s.next();
System.out.print("Enter the second time: ");
String time2 = s.next();
String tm1 = String.format("%02d", Integer.parseInt(time1));
String tm2 = String.format("%02d", Integer.parseInt(time2));
int t1 = Integer.parseInt(tm1);
int t2 = Integer.parseInt(tm2);
int difference = t2 - t1;
while (t1 < t2) {
String tmDif = Integer.toString(difference);
System.out.println("The difference between times is " + tmDif.substring(0, 1) + " hours " +
tmDif.substring(1) + " minutes.");
break;
}
}
}
But I have two issues: one: if I make time one 0800, and time two 1700, it gives me the correct 9 hours. But if the difference is 10 hours or more, it gives 1 hour and a lot of minutes. I thought using the String.format method would help, but it doesn't do anything.
two: I'm not sure how to approach a situation where time 1 is later than time 2.
Thanks!
You can try below code which will give Time difference in military format :
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.print("Enter the first time: ");
String time1 = s.next();
System.out.print("Enter the second time: ");
String time2 = s.next();
String tm1 = String.format("%02d", Integer.parseInt(time1));
String tm2 = String.format("%02d", Integer.parseInt(time2));
String hrs1 = time1.substring(0, 2);
String min1 = time1.substring(2, 4);
String hrs2 = time2.substring(0, 2);
String min2 = time2.substring(2, 4);
// int difference = t2 - t1;
if (Integer.parseInt(time1) < Integer.parseInt(time2)) {
int minDiff = Integer.parseInt(min2) - Integer.parseInt(min1);
int hrsDiff = Integer.parseInt(hrs2) - Integer.parseInt(hrs1);
if (minDiff < 0) {
minDiff += 60;
hrsDiff--;
}
System.out.println("The difference between times is " + hrsDiff + " hours " + minDiff + " minutes.");
} else {
int minDiff = Integer.parseInt(min1) - Integer.parseInt(min2);
int hrsDiff = Integer.parseInt(hrs1) - Integer.parseInt(hrs2);
if (minDiff < 0) {
minDiff += 60;
hrsDiff--;
}
System.out.println("The difference between times is " + hrsDiff + " hours " + minDiff + " minutes.");
}
}
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)));