Question

I have a project where I need to input numbers between -25 and 25 and count how many times each was inputted. The code I have only accepts positive numbers. Line 26 and 39 give me problems and I can't get my amount array to hold any negative numbers.

My code:

public class PP62{

    public static void main(String[] args) {

        char response = 'a';
        int numbers[] = new int[100], count, amount[] = new int[100], n2;
        count = 0;

        do{

        if((response == 'y')||(response == 'Y')||(count == 0)){

            count++;

            System.out.println("Enter in an integer between -25 and 25.");
            do{
            numbers[count] = SavitchIn.readLineInt();

            if((numbers[count] > 25)||(numbers[count] < -25)){
                System.out.println("Error, Invalid Input. Re-Enter integer between -25 and 25.");
            }

            else if((numbers[count] <= 25)||(numbers[count] >= -25)){
                n2 = numbers[count];
                amount[n2]++;
            }

            }while((numbers[count] > 25)||(numbers[count] < -25));

            System.out.println("Enter in another integer? (Y/N)");
            response = SavitchIn.readLineNonwhiteChar();
        }

        else if ((response == 'n')||(response == 'N')){

            for(int a = -25; a <= 25; a++){
                System.out.println(a + "'s entered: ");
                System.out.println(amount[a] + "\n");
            }
            System.exit(0);
        }

        else{
            System.out.println("Incorrect Input. Must be 'Y' (yes) or 'N' (no).");
            response = SavitchIn.readLineNonwhiteChar();
        }}while(count > 0);
    }   
}
Était-ce utile?

La solution

array indexes in java cannot be negative, so you need to fix them starting from 0

like this:

char response = 0;
int numbers[] = new int[51];

do
{
    System.out.println("Enter in an integer between -25 and 25.");
    int n = SavitchIn.readLineInt();
    if( (n > 25) || (n < -25))
        System.out.println("Error, Invalid Input. Re-Enter integer between -25 and 25.");
    else
        numbers[n+25]++;

    System.out.println("Enter in another integer? (Y/N)");
    do
    {
        response = SavitchIn.readLineNonwhiteChar();
        if ( (Character.toLowerCase(response) != 'y') && (Character.toLowerCase(response) != 'n') )
            System.out.println("Incorrect Input. Must be 'Y' (yes) or 'N' (no).");
    } while ( (Character.toLowerCase(response) != 'y') && (Character.toLowerCase(response) != 'n') );
} while (Character.toLowerCase(response) == 'y');

for(int a = -25; a <= 25; a++)
    System.out.println(a + "'s entered: " + numbers[25+a] + " times\n");

Autres conseils

In your code

else if((numbers[count] <= 25)||(numbers[count] >= -25)){
                n2 = numbers[count];
                amount[n2]++;
            }

What if n2 = numbers[count] = -25 ? n2 will be negative and amount[n2]++ accessing array with negative index. You can't access an array with negative index, doing so will result in java.lang.ArrayIndexOutOfBoundsException

Why don't we use an array with size 50 and count with a[n2+25]++ instead!

I can't test the code since I don't have the SavitchIn class, but I note that the line

amount[n2]++;

will at some point try to index an array with a negative number. Array indexes go from 0 on up. I would recommend

amount[n2+25]++;

and then

System.out.println(amount[a-25]+"\n");

on the display. That is, shift the numbers you want to represent (-25..25) to a range the array can handle (0..50).

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top