简体   繁体   中英

I'm trying to create an error message if the data entered in an array is less than zero, input validation

This method allows the user to input the rainfall for every month of the year. I'm trying to prevent data less than zero from being stored in the array. I'm using a do-while loop, but I can't seem to figure out how to check if the input is less than zero. Thanks for your help guys, cheers!

public static double[] getRainFall()
    {
        double[] rainfallMonths = new double[12];
        double[] rainfall = new double[12];

        do
        {
            for(int x = 0; x < rainfallMonths.length; x++)
            {
                    System.out.print("What is the rainfall for month #" + (x + 1) + ": ");
                rainfallMonths[x] = keyboard.nextDouble();
                rainfall[x] = rainfallMonths[x];

                if(rainfallMonths < 0)
                {
                    System.out.println("Input is Invalid");
                }
            }
        }while(rainfallMonths < 0);


        for(int count = 0; count < rainfallMonths.length; count++)
        {
            System.out.println("Rainfall Month #" + (count + 1) + ": " + rainfall[count]);
        }

        return rainfall;
    }

Your logic is a little off, not to mention that you're trying to compare an array to an int ...

First, the logic...

do 
    for x = 0 to rainfallMonths.length -1 do
        ... get input...
while value < 0

The problem here is, you've already assigned the input to all the elements of the array in the for-next loop, but then you are trying to validate the value that was input outside of the for-next which is likely never to return a valid result...and it's too late...

Instead, you want to reverse the logic...

for x = 0 to rainfallMonths.length -1 do
    do 
        value = get input from user
    while value < 0
    rainfallMonths[x] = value

Next, rainfallMonths is a reference to an array, this isn't actually what you want to be checking against, you need to be checking against one it's values or elements, for example...

while (rainfallMonths[x] < 0);

And if none of that made sense...

public static double[] getRainFall()
{
    double[] rainfallMonths = new double[12];
    double[] rainfall = new double[12];

    for(int x = 0; x < rainfallMonths.length; x++)
    {
        double input = 0;
        System.out.print("What is the rainfall for month #" + (x + 1) + ": ");
        do {
            rainfallMonths[x] = keyboard.nextDouble();
            rainfall[x] = rainfallMonths[x];
            if(input < 0)
            {
                System.out.println("Input is Invalid");
            }
        } while (rainfallMonths[x] < 0);    
    }


    for(int count = 0; count < rainfallMonths.length; count++)
    {
        System.out.println("Rainfall Month #" + (count + 1) + ": " + rainfall[count]);
    }

    return rainfall;
}

You might want to take a refresher on Arrays which should help ;)

double temp = -1;
for(int x = 0; x < rainfallMonths.length; x++)
        {
            System.out.print("What is the rainfall for month #" + (x + 1) + ": ");
            temp = keyboard.nextDouble();
            if(temp < 0)
            {
                System.out.println("Input is Invalid");
                x--; //if you want the user to maybe try to repeat this month again?
            }
            else
            {
                rainfallMonths[x] = keyboard.nextDouble();
                rainfall[x] = rainfallMonths[x];
            }
        }  

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