简体   繁体   中英

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

}

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.

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 (eg 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM