1

I have a project for class where we have two arrays (high and low) that we are going to store high and low temperatures for each day during a month. We need to have a number that is out of the ordinary so if the person entering information into the arrays forgot to take a temp for a day it would be that default number (510 is what I chose). So what I currently have is a program that prints out the day but all the temperatures are 510. Can someone explain to me what I need to do to the while loop to get the info entered to go into the correct high and low arrays? Also is there a way to enter nothing (if the person recording temperatures forgot to take a temperature for the high) and have it remain 510 degrees?

    import java.util.Scanner;

import javax.swing.JOptionPane;

public class Weather {

public static void main(String[] args) {
    // TODO Auto-generated method stub



    int [] high = new int[30];
    int [] low = new int[30];
    //switch to 32
    Init (high);
    Init(low);

    Report(low,high);
    LoadData(low,high);
    Report(low, high);
}
public static void Init(int A[])
{
    for(int i = 0; i < A.length; i++)
    {
        A[i] = 510;
    }
}

public static void Report(int[] H, int[] L)
{
    System.out.println("Day    High    Low");

    for(int i = 0; i < H.length; i++)
    {
        System.out.println(i + "      " + H[i] + "      " + L[i]);
    }
}
public static void LoadData(int[] H, int[] L)
{

    int day = 0;
    while(day <= 30)
    {


        int high = Integer.parseInt(JOptionPane.showInputDialog("please enter the high"));
        int low = Integer.parseInt(JOptionPane.showInputDialog(" Please enter the low"));

        H[day] = high;
        H[day] = low;
        day++;
        }
}

}

Drizzy
  • 105
  • 1
  • 1
  • 10
  • It isnt jumping out at me, but may I suggest adding some debugging lines after the Integer.parseInt() lines? Try something like: `int high = Integer.parseInt(JOptionPane.showInputDialog("please enter the high"));` `int low = Integer.parseInt(JOptionPane.showInputDialog(" Please enter the low"));` `System.out.println("Day: " + day + " high: " +high + " low: " + low);` And see what it prints out – Jarrett Spiker Jan 22 '16 at 02:21

3 Answers3

0

I tried your code and it almost works fine.

You have two mistakes here:

int day = 0;
while(day <= 30)
{
    int high = Integer.parseInt(JOptionPane.showInputDialog("please enter the high"));
    int low = Integer.parseInt(JOptionPane.showInputDialog(" Please enter the low"));

    H[day] = high;
    L[day] = low;
    day++;
}

Your int[] array was initialized as int[30] so it only holds 30 elements, since the condition of your loop is day <= 30, it will actually try to input a 31th element, which will raise and exception. If I change the condition to day < 30, your program reports the temperatures properly.

Your other mistake is that you are assigning both low and high to the same array, just change H[day] = low; to L[day] = low;

For the 'blank input' requirement, I recommend changing the loop to a for-loop:

for(int day = 0; day < H.length; day++) {
  String low, high;

  high = JOptionPane.showInputDialog("please enter the high");
  low = JOptionPane.showInputDialog(" Please enter the low");

  if(high.equals("") || low.equals(""))
    continue;

  H[day] = Integer.parseInt(high);
  L[day] = Integer.parseInt(low);

}

What this code does is, if the user inputs a blank string, it skips the rest of the loop and goes to the next cycle, effectively leaving the default value untouched.

Chris Jaquez
  • 649
  • 1
  • 6
  • 16
0

Using try and catch, here's an example of what you can do:

while(day < 30)
{
   int highTemp = 0;
   int lowTemp = 0;
   String high = JOptionPane.showInputDialog("please enter the high");
   String low = JOptionPane.showInputDialog(" Please enter the low");
   try {
       highTemp = Integer.parseInt(high);
   } catch(NumberFormatException e){
       highTemp = 510;
   }
   try {
       lowTemp = Integer.parseInt(low);
   } catch(NumberFormatException e){
       lowTemp = 510;
   }
   H[day] = highTemp;
   L[day] = lowTemp;
   day++;
}

Basically what happens here is the input is first taken in as a string, then the program attempts to parse it. If there is a failure (e.g. empty input), then it will make the value 510 by default.

Please do note the errors in your code for the while condition:

Before:

while(day <= 30)

After

while(day < 30)

If you make it '<=", there will be an array out of bounds exception, as your temperature arrays only have a length of 30 (meaning the last indexed value is at index 29)

Also, you didn't change the array the temperature values are assigned to here:

Before:

H[day] = highTemp;
H[day] = lowTemp;

After:

H[day] = highTemp;
L[day] = lowTemp;

You are just over-riding your array value assignment if you assign the low and high temp both to the high temp array.

Also, in your 'public static void main(String[]args){}'

Before:

Report(low, high);
LoadData(low, high);
Report(low, high);

After:

Report(high, low);
LoadData(high, low);
Report(high, low);

Your methods call for the high temperature array first, not the low one. In your code you are assigning the low temperature to the high temperature.

PallyP
  • 37
  • 8
0

you wrote

while(day <= 30)
{


    int high = Integer.parseInt(JOptionPane.showInputDialog("please enter the high"));
    int low = Integer.parseInt(JOptionPane.showInputDialog(" Please enter the low"));

    H[day] = high;
    H[day] = low;
    day++;
    }

look you have no try/catches to handle exceptions, and you wrote H[day] = high; and H[day] = low; so low array will remain 510 for all elements , all time.
you can fix it simply using try catches.

note: that when length of your array is 30 , you can access to it's elements with indexes between 0 to 29 so the condition of your while loop while(day <= 30) is incorrect.

use this code:

    int day = 0;
    while (day < 30) {

        try {
            int high = Integer.parseInt(JOptionPane.showInputDialog("please enter the high"));
            H[day] = high;
        } catch (HeadlessException | NumberFormatException | NullPointerException e) {
        }

        try {

            int low = Integer.parseInt(JOptionPane.showInputDialog(" Please enter the low"));
            L[day] = low;

        } catch (HeadlessException | NumberFormatException | NullPointerException e) {
        }

        day++;
    }
Rahmat Waisi
  • 1,293
  • 1
  • 15
  • 36
  • Could you please explain what these do "HeadlessException | NumberFormatException | NullPointerException e" for my code so I know in the future. Thanks! – Drizzy Jan 22 '16 at 03:07
  • when you use `parseInt(string)` it throws `NumberFormatException` so you must catch it to avoid crashes in your app, when you use `JOptionPane.showInputDialog(...)` it throws `HeadlessException` , so you must catch it . at this time `NullPointerException ` will not accur at in your code,you can remove it. – Rahmat Waisi Jan 22 '16 at 03:31
  • The code still works when I get rid of the 'HeadlessException' why do I need that for the 'JOptionPane.showIputDialog()'? When I looked at the oracle documentation I did not really understand what it meant. – Drizzy Jan 22 '16 at 04:22
  • take look at this topic : [HeadlessException](http://stackoverflow.com/a/14038818/4101906) . – Rahmat Waisi Jan 22 '16 at 13:36