0

I am doing this program and I keep getting "null" error at the end of output when I print as you see in the code. It does read file fine, but at the end, it adds so many null. Any guidance will be appreciated! Here 's what I tried so far.

public static void main(String[] args) throws Exception
{
    Stack nifo=new Stack();
    FileReader file = new FileReader("infix.dat");
    try (BufferedReader br = new BufferedReader(file)) {
        String [] words = new String[50];
        String text="";
        int ctrl = 0;
        String Line =br.readLine();
        while (Line!= null)
            {
            words[ctrl]= Line;
            Line = br.readLine();
            ctrl = ctrl + 1;
            }//end of while loop


            for (int i = 0; i < words.length; i++)
             {
            System.out.println(words[i]);
             }
            file.close();
        } catch (IOException e) {
        e.printStackTrace();
    }//end of catch

}//end of main class

and my output is this below. As you see null is being printed at the end after I read my file.

 5 * 6 + 4
 3 - 2 + 
 ( 3 * 4 - (2 + 5)) * 4 / 2
 10 + 6 * 11 -(3 * 2 + 14) / 2
  2 * (12 + (3 + 5 ) * 2
   null
   null
   null
   null
   more nulls after that.

Thank you!

1
  • 1
    That's not an error, that's all the empty space in your array. Commented Feb 20, 2017 at 2:25

2 Answers 2

1

You declare a fixed size array:

String[] words = new String[50];

Then you store some values in it, and then you print each element:

for (int i = 0; i < words.length; i++) {
    System.out.println(words[i]);
}

All of the elements that you didn't use are null. So if your file has 6 lines it will print those 6 lines and then 44 nulls, since you have not put anything in the other 44 slots of your array. I suggest you use a different data structure, like a list. This will allow you to store only the number of values that you need.

Try this:

public static void main(String[] args) throws Exception
{
    Stack nifo=new Stack();
    FileReader file = new FileReader("infix.dat");
    try (BufferedReader br = new BufferedReader(file)) {
        List<String> words = new LinkedList<>(); //replaced your array with a list
        String text="";
        String Line =br.readLine();
        while (Line!= null)
            {
            words.add(Line);
            Line = br.readLine();
            }//end of while loop

            for (String word : words)
             {
            System.out.println(word);
             }
            file.close();
        } catch (IOException e) {
        e.printStackTrace();
    }//end of catch

}//end of main class
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much and thanks for the tip for linked list!!
@mike happy to help! please remember to upvote and accept the answer if this solved your issue =]
0

Since you are not sure of the Array size and if you want to stick to using arrays then the following might fix your problem

        for (int i = 0; i < words.length && words[i] != null; i++)
         {
            System.out.println(words[i]);
         }
        file.close();

Just make sure that your loop does not process null values for any index in words array. Because the indices that were never populated in the array were initialized to null when you declared a fix sized array

2 Comments

Thanks for the input and quick answer.
Hopefully that helped you, because I was not interested in replacing your array with some other data structure. But still the other answer is worth trying if you are looking for diversity :)

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.