How to properly assign text file values to array in java
I need help properly assigning the values that I fetch from the text file into the array. And then loop through it. Here's my current code,
my problem is that the buffered reader fetches null values. And when I run the program , I get index out of bounds error and null pointer exception
public class Main {
public static String[] q=new String[50];
public static String[] a=new String[50];
public static String[] b=new String[50];
public static String[] c=new String[50];
public static String[] d=new String[50];
public static char[] ans=new char[50];
public static Scanner x=new Scanner(System.in);
public static int random(int min, int max){
int xx;
xx= (int) ( Math.random() * (max-min + 1))+ min;
return xx;
}
public static void main(String[] args) {
int ii=0;
int score=0;
try {
FileReader fr;
fr = new FileReader (new File("F:\\qa.txt"));
BufferedReader br = new BufferedReader (fr);
while (br.readLine()!= null) {
for(int ox=0;ox<5;ox++){
q[ox]= new String();
a[ox]= new String();
b[ox]= new String();
c[ox]= new String();
d[ox]= new String();
Re: How to properly assign text file values to array in java
keang is talking about this piece of code
Code:
while (br.readLine()!= null) { // in the while condition, you read a line but didn't do anything.
for(int ox=0;ox<5;ox++){
q[ox]= new String();
a[ox]= new String();
b[ox]= new String();
c[ox]= new String();
d[ox]= new String();
q[ox] = br.readLine();
a[ox]=br.readLine();
b[ox]=br.readLine();
c[ox]=br.readLine();
d[ox]=br.readLine();
String strtemp=br.readLine();
ans[ox]=strtemp.charAt(0);
}
}
1. the while loop condition threw away 1 line of data
2. providing the read in file might help.
3. while loop only check for the next ONE line of data, but you have a inner for loop which reads in the next SIX lines. i suspect if EOF took place in your inner loop, it might cause a nullpointer error. if your read-in file has only 6 lines, your code actually read in 7lines, this might be the cause.
if you didn't want to throw away that line. you can try this
Code:
while(br.ready()){
...
...
}
Also do you really need 2 loops? You knew you only needed to read in 6lines 5 times. did you add the while loop simply to check for BReader status? if so, you can consider this
Code:
for(int i=0;br.ready();i++){
if(i==5) break; //exit condition
q[ox]= new String();
a[ox]= new String();
b[ox]= new String();
c[ox]= new String();
d[ox]= new String();
q[ox] = br.readLine();
a[ox]=br.readLine();
b[ox]=br.readLine();
c[ox]=br.readLine();
d[ox]=br.readLine();
String strtemp=br.readLine();
ans[ox]=strtemp.charAt(0);
}
Re: How to properly assign text file values to array in java
Code:
while(br.ready()){
...
...
}
Be careful using this approach it is not safe. The method may return false (and your loop will exit) before the end of file is reached. Admittedly it is extremely unlikely to go wrong when reading a file from a local file system but it is a bad habit to get into as it has a much higher chance of failing if reading across a network.
Bookmarks