dimanche 30 novembre 2014

Null when splitting a string


Vote count:

0




I need to create my own sort method for an array, and I begin my splitting the text file into an array filled with the words. The file format is: an integer n, followed by n words.


Here's an example: 4 hello hello world hello


However, my array prints: [null4, hello, hello, world, hello]


WHY! I don't understand why there is a null before. And, if I remove the number 4 (which plays no role in my program at the moment) I get: [nullhello, hello, world, hello]


Can you please help me remove this null? Thanks in advance!



public static void main(String[] args) throws FileNotFoundException {
filePath = "***TEXT FILE HERE***";

fileInput = new Scanner(new File(filePath));
convertFile(fileInput);
}

public static void convertFile(Scanner file) {

String line;

while (fileInput.hasNextLine()) {
line = fileInput.nextLine();
fileData = fileData + line;
}

String[] array = createArray(fileData);
System.out.println(Arrays.toString(array));
}

public static String[] createArray(String data) {
String[] dataArray = data.split("\\s+");

return dataArray;
}


asked 17 secs ago







Null when splitting a string

Aucun commentaire:

Enregistrer un commentaire