How to properly assign text file values to array in java
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: How to properly assign text file values to array in java

  1. #1
    Join Date
    Oct 2010
    Posts
    3

    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!");
    }
    }



    }

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    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.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  3. #3
    Join Date
    Oct 2010
    Posts
    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.

  4. #4
    Join Date
    Oct 2010
    Posts
    6

    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

  5. #5
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    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.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  6. #6
    Join Date
    Oct 2010
    Posts
    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.

  7. #7
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    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();
        	}
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

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
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center