Search code examples
javaarraysmethodswhile-loopuser-input

How to use a while loop to enter user input into an array while keeping certain numbers unchanged


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++;
        }
}

}


Solution

  • 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++;
        }