Zhdawson
Zhdawson

Reputation: 13

Why do I get "null" as an output string? Java

What I am trying to do is read from a file (in this case the file contains over 100,000+ lines) and store the values in an array, then print out the first 10 lines. However when I run the program I get the first line, and then followed by 9 lines of "null" which is obviously not what I want! This is the code and any tips would be appreciated.

import java.io.*;
import java.util.Scanner;

public class DawsonZachA5Q2{
  public static void main(String[] args){

Scanner keyboard = new Scanner(System.in);

 System.out.println("Enter a size for the number of letters for words: ");
int size = keyboard.nextInt();//prompts user for input
String[] array = new String[27000];

    try {
       File file = new File("big-word-list.txt");
      Scanner scanner = new Scanner(file);


        // Start a line count and declare a string to hold our current line.
        int linecount=0;

        // Tells user what we're doing
        System.out.println("Searching for words with " + size + " letters in file...");
        int wordCount=0;

        while (scanner.hasNext()){
          int i = 0;
          String word = scanner.next();

          if(size == word.length()){
            wordCount++;
            array[i]=word;
            i++;
            //add word to array
          // increase the count and find the place of the word
        }
        }

        linecount++;






            System.out.println(wordCount);

            System.out.println(wordCount+" words were found that have "+size+ " letters.");//final output


          for(int o = 0; o<10; o++){
            System.out.println(array[o]);
          }


       scanner.close();
    }// our catch just in case of error
    catch (IOException e) {
        System.out.println("Sorry! File not found!");
    }



  } // main


} // class

Upvotes: 0

Views: 4799

Answers (2)

Ra Ka
Ra Ka

Reputation: 3055

You have mistaken in the while loop. You must define 'int i = 0' before the while loop. In your case, what happen is that whenever the while loop execute, i is initialized to 0. i.e. every time, the word with required length found, that word will be stored in array[0] (Since i is initialized to 0 every iteration of while loop) replacing the previous stored value. As a result, you only get the first value and remaining displayed as null since nothing is stored after array[1]. Therefore, the actual flow should be like this.

// i is initialized outside of loop.
int i = 0;
while (scanner.hasNext()){
    //int i = 0;  this is your error
    String word = scanner.next();
    if(size == word.length()){
        wordCount++;
        array[i]=word;
        i++;
        //add word to array
        // increase the count and find the place of the word
    }
}

Upvotes: 0

Ozgen
Ozgen

Reputation: 1092

Define int i = 0; outside of the while loop. It gets set to zero each time the loop runs. That is the problem here.

Upvotes: 4

Related Questions