0

Before this the user inputs an int for numOfTimes. Say it's 5. This will ask the question 5 times. But each time through it will erase the previous value in hrs1. It needs to be a separate variable. So if numOfTimes=5 Then I should get 5 different doubles for "Hour " and 5 different doubles for "Minute ". (assuming the user inputs different times) but they all need to be stored in different variables. How should I do this?

Thank you my question has been answered!

3

4 Answers 4

2

use an array ..

int a[] = new int[5];
for(int i =0;i<5;i++){
    a[i] = //your value
}
Sign up to request clarification or add additional context in comments.

Comments

1

You just need to put your "calculate average" code outside the for loop. I am not sure exactly how you want to calculate the average. But here are two simple ways.

Method one - keep track of the totals and calculate the basic average.

public class AvgTime {

  public static void main(String[] args) {

    Scanner in = new Scanner(System.in);


    System.out.print("How many times? ");
    int numOfTimes = in.nextInt();
    System.out.println("\n");

    double hrTotal = 0;
    double minTotal = 0;

    for (int i = 1; i <= numOfTimes; i++){

      System.out.println("What Time (military time):  ");
      System.out.print("Hour  ");
      double hrs1 = in.nextDouble();
      System.out.print("Minute  ");
      double min1 = in.nextDouble();

      hrTotal += hrs1;
      minTotal += min1;
    }

    //calculate average
    double avdHr1 = hrTotal/numOfTimes;
    double timeMin1 = minTotal/numOfTimes;

    System.out.println(avgHr1+":"+timeMin1 + " P.M");
  }
}

Method 2 - Use lists and iterate twice

public class AvgTime {

  public static void main(String[] args) {

    Scanner in = new Scanner(System.in);


    System.out.print("How many times? ");
    int numOfTimes = in.nextInt();
    System.out.println("\n");

    ArrayList<Double> hours = new ArrayList<>();
    ArrayList<Double> minutes = new ArrayList<>();
    double minTotal = 0;

    for (int i = 1; i <= numOfTimes; i++){

      System.out.println("What Time (military time):  ");
      System.out.print("Hour  ");
      double hrs1 = in.nextDouble();
      System.out.print("Minute  ");
      double min1 = in.nextDouble();

      hours.add(hrs1);
      minutes.add(min1);
    }

    //calculate average
    double avgHr1 = 0;
    double timeMin1 = 0:
    for (int i = 0; i < hours.size(); i++) {
      double hour = hours.get(i);
      double minute = minutes.get(i);

      //ToDo: calculate average so far

    }

    System.out.println(avgHr1+":"+timeMin1 + " P.M");
  }

1 Comment

Thank you this has helped a lot.
0

You can use arrays to store the information the user has input. Before the loop, make an array using the new keyword, e.g. double[] hrs=new double[numOfTimes]. In the loop, write to different locations in the array for each input, hrs[i]=in.nextDouble(). You can later read from a position on the array using the syntax 'name[index]', such as 'hrs[2]'. Note that for java and many other languages, arrays start at 0. This means for an array [1,2,3] named arr, arr[1] equals 2 instead of 1. This means it would be best if your for loop was changed from for(int i=1;i<=numofTimes;i++) to 'for(int i=0;i

Comments

0

<SOAPBOX,RANT,HIGHHORSE>

This is more of a code review than a straight answer, but something has been bugging me about newbie questions that I've observed on stackoverrflow.

When developing, I avoid keyboard input like the plague. It is such drudgery, especially with a loop such as in this program. So many newbie questions have user-keyboard input. Why?! It makes development so much more difficult!

I've rewritten your program to add the ability for testing data, completely avoiding the need for user-input during development. When testing is over, just switch the test/live comments around.

I'm sure there's a more elegant way, but this style has worked well for me, and I recommend it.

</SOAPBOX,RANT,HIGHHORSE>

import java.util.*;
import static java.lang.Math.abs;

public class AverageTimeWTestingData  {

   public static void main(String[] args) {

      HourMin24[] ahm = null;

      //EXACTLY ONE of the following lines must be commented out
      //Test only:
         ahm = getTestData();
      //Live only:
      // ahm = getDataFromUserInput();

      double dTotalHours = 0.0;
      for (HourMin24 hm : ahm){
         System.out.println("Time: " + hm.iHour + ":" + hm.iMin);
         dTotalHours += hm.iHour + (hm.iMin / 60);
      }

      System.out.println("Average time (" + ahm.length + "): " + (dTotalHours / ahm.length));
   }
   private static final HourMin24[] getDataFromUserInput()  {
      Scanner in = new Scanner(System.in);

      System.out.print("How many times? ");
      int numOfTimes = in.nextInt();
      ArrayList<HourMin24> al24 = new ArrayList<HourMin24>(numOfTimes);
      while(numOfTimes < 0)  {
         System.out.println("What Time (military time):  ");
         System.out.print("Hour  ");
         int iHour = in.nextInt();
         System.out.print("Minute  ");
         int iMin = in.nextInt();

         al24.add(new HourMin24(iHour, iMin));

         numOfTimes--;
      }

      return  al24.toArray(new HourMin24[al24.size()]);
   }
   private static final HourMin24[] getTestData()  {
      System.out.println("TEST MODE ON");
      return  new HourMin24[] {
         new HourMin24(13, 1),
         new HourMin24(23, 19),
         new HourMin24(0, 59),
         new HourMin24(16, 16),
      };
   }
}
class HourMin24  {
   public int iHour;
   public int iMin;
   public HourMin24(int i_hour, int i_min)  {
      iHour = i_hour;
      iMin = i_min;
   }
}

Output:

[C:\java_code\]java AverageTimeWTestingData
TEST MODE ON
Time: 13:1
Time: 23:19
Time: 0:59
Time: 16:16
Average time (4): 13.0

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.