I am having a problem with my code it seems to endlessly loop with user input of two temps never ending. As I am a newbie at this could someone take a look at what I've got so far and fix what's wrong here are my two classes This is my third week of class, I'd like to know how am I doing? [/TEX]


import java.util.*;
import java.text.*;
import java.math.*;
    
public class Ch9_PrExercise10 {

	/**
	 * This program will store the highest and lowest temeratures for each month for a year
	 * Output average high, average low, and highest and lowest temperatures of the year.
	 * @Written by: ellas
	 */
	public static void main(String[] args)
    {
          Statistics unc = new Statistics();
          
      
          double[][] calendarTemps = new double[2][12];
          int i, j, temp = 0;
              
          Scanner keyboard = new Scanner(System.in);
          for (i = 0; i < calendarTemps.length; i++)
          {
                for (j = 0; j < calendarTemps[i].length; j++)
              {
                	Exception exp = null; // temp Exception object, pointing to null for now
        				do{
        				    try{
        	                     exp = null;
                  System.out.println("Please input high temperature " +  (i + 1) + " ");
                  temp = keyboard.nextInt();
                  System.out.println("Please input low  temperature " +  (j + 1) + " ");
                  temp = keyboard.nextInt();
                  calendarTemps[i][j] = temp;
              }catch(Exception e){exp = e;}
        				} while(calendarTemps[i][j] == null || exp != null);
        				}
          double avgHTemp = unc.averageHigh(calendarTemps);
          double avgLTemp = unc.averageLow(calendarTemps);
          
          System.out.println("Average high temperatures of the year is: " + avgHTemp);
          System.out.println("Average low temperatures of the year is: " + avgLTemp);

      }
  }




public class Statistics 
{
    int avgLTemp = 0;
    int higHTemp;
    int loWTemp;

    public int getData()
    {
        return 0;
    }

    public double averageHigh(double[][] avgHigh)
    {
    
        int i = 0;
        double sumHTemp = 0;
        double avgHTemp = 0;

      for (i=0; i < avgHigh.length; i++)
      {
         sumHTemp = sumHTemp + avgHigh[0][i];
      }    
        avgHTemp = (sumHTemp/avgHigh.length);
        return avgHTemp;  
    }
    
    public double averageLow(double[][] avgLow)
    {
    
        int i = 0;
        double sumLTemp = 0;
        double avgLTemp = 0;

      for (i=0; i < avgLow.length; i++)
      {
         sumLTemp += avgLow[1][i];
      }    
        avgLTemp = (sumLTemp/avgLow.length);
        return avgLTemp; 
    }

}

Recommended Answers

All 8 Replies

Take a second look at where your loop ends, and what you need to include in the loop to get the desired 'output'.

-alias

Take a second look at where your loop ends, and what you need to include in the loop to get the desired 'output'.

-alias

[TEX]I'm sorry but I don't get what you are talking about I've been at this for a week now I'm getting fustrated.[/TEX]

Did you type out this code yourself?

Yes I wrote it with help form text book I said I'm learning and this is an on-line class as well so not much input from instructor. Is it that bad?

No it doesn't appear bad just want to make sure you understand what you coded here. Do you know much about do/while loops? Do you see where your loop is incorporated into the main method here?

-alias

If your still having trouble here, i'll compile the code when i get off work today and take a closer look at it. As for the problem you are experiencing above, take a closer look at the main method(the first method in the code) and look at the loops you have incorporated. Try to find what might be causing user input to loop infinitely.

-alias

If your still having trouble here, i'll compile the code when i get off work today and take a closer look at it. As for the problem you are experiencing above, take a closer look at the main method(the first method in the code) and look at the loops you have incorporated. Try to find what might be causing user input to loop infinitely.

-alias

Thanks any pointers you can give me would be greatly appreciated

I'm getting a few different compilation errors, Do you have any IDE or some sort of compiler for java? If not i would recommend NetBeans. It's free, and pretty easy to use once you get the hang of it.

-alias

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.