-4

My programming class was instructed to make a program that would add time and converting it into the right minutes and seconds amount (no bigger than 59.)

import java.util.Scanner;

public class AddingTime {

    public static void main (String[] args) {

        Scanner kbReader = new Scanner(System.in);

        System.out.println("Welcome to AddingTime!");

        // First Addend

        System.out.println("Enter the days amount for the first addend (if none print 0): ");

        int firstAddendDays = kbReader.nextInt();

        System.out.println("Enter the hours amount for the first addend (in none print 0): ");

        int firstAddendHours = kbReader.nextInt();

        System.out.println("Enter the minutes amount for the first addend (if none print 0): ");

        int firstAddendMinutes = kbReader.nextInt();

        System.out.println("Enter the seconds amount for the first addend (if none print 0): ");

        int firstAddendSeconds = kbReader.nextInt();

        // Second Addend

        System.out.println("Enter the days amount for the second addend (if none print 0): ");

        int secondAddendDays = kbReader.nextInt();

        System.out.println("Enter the hours amount for the second addend (in none print 0): ");

        int secondAddendHours = kbReader.nextInt();

        System.out.println("Enter the minutes amount for the second addend (if none print 0): ");

        int secondAddendMinutes = kbReader.nextInt();

        System.out.println("Enter the seconds amount for the second addend (if none print 0): ");

        int secondAddendSeconds = kbReader.nextInt();

        int totalSeconds = firstAddendSeconds + secondAddendSeconds;

        if (totalSeconds >= 60) {

            // Total Seconds if totalSeconds is larger than 60

            totalSeconds = totalSeconds % 60;

            // Extra minutes from totalSeconds in a decimal form

            double minutesFromSeconds = totalSeconds / 60;

            // Changing the above into a integer form of minutes 

            int intMinutesFromSeconds = (int)minutesFromSeconds;
        }

        int totalMinutes = firstAddendMinutes + secondAddendMinutes + intMinutesFromSeconds;

        if (totalMinutes >= 60) {

            // Total minutes if totalMinutes is larger than 60

            totalMinutes = totalMinutes % 60;

            // Extra hours from totalMinutes in a decimal form

            double hoursFromMinutes = totalMinutes / 60;

            // Changing the above into an integer form of hours

            int intHoursFromMinutes = (int)hoursFromMinutes;
        }

        int totalHours = firstAddendHours + secondAddendHours + intHoursFromMinutes;

        if (totalHours >= 24) {

            // Total hours if totalHours is larger than 24

            totalHours = totalHours % 24;

            // Extra days from totalHours in a decimal form

            double daysFromHours = totalMinutes / 24;

            // Changing the above into an integer form of days

            int intDaysFromHours = (int)daysFromHours;
        }

        int totalDays = firstAddendDays + secondAddendDays + intDaysFromHours;

        System.out.println("Your total calculated time is " + totalDays + "days" + totalHours + "hours" + totalMinutes + "minutes" + totalSeconds + "seconds" );
    }
}

When I compile this code, it tells me that I have an cannot find symbol error on variable intMinutesFromSeconds, intHoursFromSeconds, and intDaysFromHours. Why?

Cœur
  • 37,241
  • 25
  • 195
  • 267

1 Answers1

0

It may be a problem with scope (what's declared inside the brackets may not be available after the brackets close). Not sure what Java is doing.

if (totalSeconds >= 60) {
    int intMinutesFromSeconds = (int) minutesFromSeconds;
}

int totalMinutes = intMinutesFromSeconds + ...

You can fix it with something like:

int intMinutesFromSeconds = 0;
if (totalSeconds >= 60) {
    intMinutesFromSeconds = (int) minutesFromSeconds;
}

int totalMinutes = intMinutesFromSeconds + ...
Abdul Ahad
  • 826
  • 8
  • 16