-
October 7th, 2010, 09:33 AM
#1
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();
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);
}
}
br.close();
} catch (Exception e) { e.printStackTrace();}
for(int yy=0;yy<5;yy++){
int ran= random(1,4);
System.out.println(q[ran] + "\n" + a[ran] + "\n" + b[ran] + "\n" + c[ran] + "\n" + d[ran] + "\n" + "\n" + "Answer: ");
String strans=x.nextLine();
char y=strans.charAt(0);
if(y==ans[ran]){
System.out.println("Msg1!");
score++;
System.out.println("Score:" + score);
}else{
System.out.println("Msg2!");
}
}
}
-
October 7th, 2010, 10:08 AM
#2
Re: How to properly assign text file values to array in java
When posting code please use code tags.
Your while loop is reading a line from the file and then throwing the line away - are you sure you want to do this?
And then you attempt to read 6 lines without checking to see if any data is returned.
my problem is that the buffered reader fetches null values
No it doesn't, it returns null when the end of the file is reached.
-
October 7th, 2010, 08:01 PM
#3
Re: How to properly assign text file values to array in java
How do I do it?Sorry, but I'm just a beginner, and I don't know exactly what part of the code you're referring to and how do I fix it.
-
October 8th, 2010, 02:33 AM
#4
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);
}
if i am wrong somewhere feel free to point out
-
October 8th, 2010, 05:17 AM
#5
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.
-
October 8th, 2010, 07:43 AM
#6
Re: How to properly assign text file values to array in java
so, what is a better approach to scanning buffer? when i look up java api, that is the only method available.
-
October 8th, 2010, 07:59 AM
#7
Re: How to properly assign text file values to array in java
There are 2 standard approaches to repeatedly reading until the end of file is reached that I know of:
This approach harks back to the days of coding in C and whilst it is compact it's difficult to read and so is generally frowned upon these days.
Code:
while ( (myline = br.readLine()) != null )
{
...
}
This approach is the recommended way of doing it
Code:
myline = br.readLine();
while ( myLine != null )
{
...
myline = br.readLine();
}
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|