0

i don't know what's wrong. the array should be in Number or int.

here's my code:

Number[] freq;

    int place = 0;

    BufferedReader br = new BufferedReader(new FileReader(new File("this.txt")));

    String read;

    String[] temp;

    int num;

    while((read = br.readLine())!=null)
    {
        temp = read.split(",");
        for(int i = 0; i<=temp.length; i++)
        {
            String t = temp[i];
            num = Integer.parseInt(t);
            freq[place] = num;
            place++;
        }

    }

    System.out.println("done");

it shouldn't output some heavy result but i need it working. i always get the error

variable freq might not have been initialized.
freq[place] = num;
7
  • 4
    Java arrays are not dynamic, you need to actually initialize them to a length of some sort before you use them. Commented Dec 3, 2014 at 10:48
  • the values that are going to be in that array aren't always in the same size. i know arrays must be first initialized with size but in this program it must be depended on how many are in the text file Commented Dec 3, 2014 at 10:52
  • Then you need to use another type of variable, such as an ArrayList, which can be dynamic in size. Commented Dec 3, 2014 at 10:53
  • 1
    (better to update your question if you have new information) dynamic sizing is a fairly common requirement, and there are usually better data structures than arrays. Have a look at Lists for examples Commented Dec 3, 2014 at 10:56
  • 1
    sorry about that. i was searching about that then can't find answer like mine, decided to post the a question. i didn't notice at first that that make the title of this. btw, thanks everyone who answered! Commented Dec 3, 2014 at 11:07

3 Answers 3

3

You must initialise the array.

Number[] freq = new Number[someSize]

Sign up to request clarification or add additional context in comments.

Comments

2

You need to set a size for the Array. Since you indicated in the comments that it has to be dynamic, you'll have to instead use a different kind of variable that can be used with dynamic length, such as an ArrayList.

See this example:

// initialize an ArrayList:
List<Number> freq = new ArrayList<Number>();
BufferedReader br = new BufferedReader(new FileReader(new File("this.txt")));

String read;
String[] temp;

int num;

while((read = br.readLine())!=null)
{
    temp = read.split(",");
    for(int i = 0; i<=temp.length; i++)
    {
        String t = temp[i];
        num = Integer.parseInt(t);
        freq.add( num );
    }

}

System.out.println("done");

1 Comment

nice it worked! thanks! now i know that List can be like that.
0

You need to set an array size before you use an array in JAVA.

int[] someArray= new int[size]

1 Comment

There is no Int available in Java. You meant int[size]?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.