Im trying to read a file from "contacts" and putting it in a 2d array called sData. Im trying to print the content on the console window but however i am receving null on my output. Am i trying to print the content in an incorrect way or some? The code was delivered to us however it seems to fail on me any help?
Joe - 1111245678
Tom - 3431234567
Lom - 7771234568
King - 76681234567
Dom - 6842234567
import java.io.*;
import java.util.Scanner;
public class phonenumbers {
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
File f= new File ("Contacts.txt");
ReadData(f);
}
public static void printArr (String [][] hex) throws Exception{
for (int row = 0; row < hex.length; row++) {
for (int column = 0; column < hex[row].length; column++) {
System.out.print(hex[row][column] + " ");
}
System.out.println();
}
}
public static int CountLines(File f) throws Exception {
int lines = 0;
Scanner reader = new Scanner (f);
while (reader.hasNextLine()) {
String s = reader.nextLine();
s = s.trim();
if (s.length() == 0) {
break;
}
lines++;
}
reader.close();
return lines;
}
public static String [][] ReadData(File f) throws Exception {
Scanner reader = new Scanner (f);
int numLines = CountLines(f);
String [][] sData = new String [numLines] [];
for (int line = 0; line <numLines; line++ ) {
String l = reader.nextLine();
l = l.trim();
String [] temp = l.split(" ");
sData [line] = new String [temp.length];
}
printArr(sData);
reader.close();
return sData;
}
}
Output
null null null
null null null
null null null
null null null
null null null
replace
sData [line] = new String [temp.length];
with
sData [line] = temp;
you currently are just putting a empty array the size of the intended array.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.