-6

I want to read each line from a file and store each line into an array of strings

while ((line = br.readLine()) != null) {

            int i = 0;

            String[] sArray = new String[line.length()];
            sArray[i] += line;
            System.out.println(sArray[i]);

                   i++;
        }

output:

nullKNZAVM   
nullCPKCCA  
nullKMUAXP

why it prints null before each set of letters.

Stewart
  • 17,616
  • 8
  • 52
  • 80
pteran
  • 5
  • 4
  • 1
    How do you think this code works? What do you think `String[] sArray = new String[line.length()];` does in each iteration? What happens with it after iteration? – Pshemo Nov 09 '17 at 19:56
  • do ***System.out.println(line)*** instead – ΦXocę 웃 Пepeúpa ツ Nov 09 '17 at 19:57
  • do `sArray[i] = line;` instead; sArray[i]'s default String value is null, that's why null gets printed out – Phu Ngo Nov 09 '17 at 19:57
  • 2
    I have downvoted this question because there is no evidence of any debugging performed on this code. Please [edit] your question to show us what your debugging has uncovered, as well as a specific question about a specific line of code. See: [How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve) and [How to Debug Small Programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). – Joe C Nov 09 '17 at 20:00
  • What you are looking for is something like: https://stackoverflow.com/a/12592835. – Pshemo Nov 09 '17 at 20:01
  • If any of these answers have helped you, it is protocol to select one as the accepted answer ... – Stewart Nov 10 '17 at 10:01

4 Answers4

2

why it prints null before each set of letter??

you are doing this:

 String[] sArray = new String[line.length()];
 sArray[i] += line;

so you create an array with a size of line.length() elements but all those elements are not initialized. now since String is an object the default value of a non initialized is null so every element in the array is null

doing sArray[i] += line; is the same as concatenate null to the line...

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
1

Because of this line:

sArray[i] += line;

Since the elements are null (as in, undefined or unitialised), when you use the concatenation +=, it transforms the value of null to a String "null". So you concatenate your line to the string "null", to get the output you're seeing.

If you don't want that, just assign instead:

sArray[i] = line;
AntonH
  • 6,359
  • 2
  • 30
  • 40
1

What do you think is in sArray[i] before you += the String line?

It's null of course!

Try this to see:

String N = null;
String M = N + "something";
System.out.println(M);

Using the the Java String += operator compiles to using StringBuilder underneath. But each element is run through String.valueOf() first.

And String.valueOf(null) returns "null" as a String.

So it evaluates to:

String N = null;
String M = StringBuilder.append(N).append("something").toString();
System.out.println(M);

Which in turn gives you:

String M = StringBuilder.append(null).append("something").toString();

Which ultimately gives you:

String M = StringBuilder.append("null").append("something").toString();

PS. The assignment of an incorrect length to the array is a separate issue. The length of each String and the length of the array are two different concepts.

Stewart
  • 17,616
  • 8
  • 52
  • 80
0

Why not do smth like that:

int line = 0;
for (String line = br.readLine(); line != null; line = br.readLine()) {
//...
}
Clyde
  • 83
  • 9