Вопрос

I am having an incredibly difficult time trying to figure out why I am getting this error. When I use a driver file to test the program it fails horribly.

Here's my code:

import java.util.Scanner;
import java.lang.Math.*;
public class Histogram
{
private int[] arrayData;
private int[] arrayRange;
private final int LOW = 1;
private final int HIGH = 100;

    public Histogram()
     {
        int[] arrayData = new int[11];
     }

    public void getInput()
    {
        int[] arrayRange = new int[11];
        for(int count = 1; count < arrayRange.length; count++)
            {
                arrayRange[count] = count * 10;
            }
        Scanner input = new Scanner(System.in);
        System.out.println("Enter numbers from 1 to 100, Type -999 to quit.");
        int nextNumb = input.nextInt();
        while(nextNumb != -999)
        {
            if(nextNumb >= LOW && nextNumb <= HIGH)
            {
                for(int i = 0; i <= arrayRange.length; i++)
                {
                if(nextNumb > arrayRange[i] && nextNumb <= arrayRange[i+1])
                    arrayData[i]++;
                }
                nextNumb = input.nextInt();
            }
            else arrayData[10]++;
            nextNumb = input.nextInt();
        }
    }

    public String starPrint(double count)
    {
        String star = "";
        count = (Math.round(count) / 5);
        for(int i = 1; i <= count; i++)
        {
            star = star + "*";
        }

        return star;
    }

    public String toString()
    {
        String results = " Range   |   Histogram"  + "\n";
        results = results + "1 - 10 | " + starPrint(arrayData[0]) + "\n";
        results = results + "11 - 20 | " + starPrint(arrayData[1]) + "\n";
        results = results + "21 - 30 | " + starPrint(arrayData[2]) + "\n";
        results = results + "31 - 40 | " + starPrint(arrayData[3]) + "\n";
        results = results + "41 - 50 | " + starPrint(arrayData[4]) + "\n";
        results = results + "51 - 60 | " + starPrint(arrayData[5]) + "\n";
        results = results + "61 - 70 | " + starPrint(arrayData[6]) + "\n";
        results = results + "71 - 80 | " + starPrint(arrayData[7]) + "\n";
        results = results + "81 - 90 | " + starPrint(arrayData[8]) + "\n";
        results = results + "91 - 100 | " + starPrint(arrayData[9]) + "\n";
        results = results + "Outliers: " + starPrint(arrayData[10]) + "\n";
        return results;
    }
}

I believe that the problem is in my getInput method right here to be precise:

if(nextNumb > arrayRange[i] && nextNumb <= arrayRange[i+1])
    arrayData[i]++;

I have no idea what's wrong with it though I am a beginner programmer and couldn't find a solution to this particular problem. Thanks for any help you're able to give!

Это было полезно?

Решение

public Histogram()
{
   int[] arrayData = new int[11];
}

You're shadowing your arrayData field in the constructor. This is creating a local variable with the same name as your class's arrayData field, initializing it, then immediately discarding it. When you try to use the field later in your code, it's null. Get rid of the int[] part.

Note that your next exception will be an ArrayIndexOutOfBoundsException ... you should look at your loop ;)

Другие советы

this: for(int i = 0; i <= arrayRange.length; i++) wont work since you are trying to access arrayRange[i] and arrayRange[i+1]

which dont exists for i = arrayRange.length-1 and further

so change it to:

for(int i = 0; i < arrayRange.length-1; i++)
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top