Trying to write information in a text file -> Array

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Nov 2007
Posts: 3
Reputation: soulesschild is an unknown quantity at this point 
Solved Threads: 0
soulesschild soulesschild is offline Offline
Newbie Poster

Trying to write information in a text file -> Array

 
0
  #1
Nov 16th, 2007
So for a homework problem I basically need to write a program that reads data from a text file, then input it into an array which I can later use for other functions. The main problem is writing to the arrays. I can't figure out how to do it properly =/

  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class reader
  5. {
  6. public static void main (String []args)
  7. {
  8. String[][] quizScores = new String[40][6];
  9.  
  10.  
  11. {
  12. try
  13. {
  14. FileReader input = new FileReader("quizscore.txt");
  15. BufferedReader bufferInput = new BufferedReader(input);
  16.  
  17. String line;
  18. while ((line = bufferInput.readLine ()) !=null )
  19. {
  20. for (int l = 0; l <=40; l++)
  21. {
  22. for (int i = 0 ; i <=6; i++)
  23. {
  24. StringTokenizer st = new StringTokenizer(line);
  25. quizScores[l][i] = st.nextToken();
  26. }
  27. System.out.println("Read:" + line);
  28. System.out.println(quizScores[4][2]);
  29. }
  30. }
  31. }
  32. catch (IOException e)
  33. {
  34. System.out.println("Error:" + e);
  35. }
  36. }
  37. }
  38. }

Executing this code, gives an error on the line,
  1. quizScores[l][i] = st.nextToken();

Are my loops for the array increments wrong? When I try to run the program, I get an out of bounds error. So I'm kinda lost ??? Also assuming I get the loop fixed, if I wanted to find the maxs and mins and averages of each column, what kinda function would I write? From my understanding, the Math.Max and Math.Min functions only take input such as this (int a, int b). So in order to use something like that, I would have to convert the entire array from String -> Int, but then where do I go from there? Does the Math.Max function have some kinda overriding function where I can just make it read the whole arrays column? And if there's other functions I can use it'd be nice if I could be enlightened to them since our teacher has only taught us the Math.max/min/etc functions which to me, do not seem to serve this problem properly.

The file contains information like this,
4532 011 017 081 032 077
5678 020 012 045 078 034
6134 034 080 055 078 045
7874 060 100 056 078 078
8026 070 010 066 078 056
9893 034 009 077 078 020
1947 045 040 088 078 055
2877 055 050 099 078 080
Last edited by soulesschild; Nov 16th, 2007 at 7:09 am. Reason: Changed/Rewrote code a bit
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 3
Reputation: soulesschild is an unknown quantity at this point 
Solved Threads: 0
soulesschild soulesschild is offline Offline
Newbie Poster

Re: Trying to write information in a text file -> Array

 
0
  #2
Nov 16th, 2007
Apparently, I can't edit anymore, but here's my new revised code and issue.

Okay I have it compiling and running. However, one small problem. The array doesn't seem to be writing properly.

  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class reader
  5. {
  6. public static void main (String []args)
  7. {
  8. String[][] quizScores = new String[40][6];
  9. {
  10. try
  11. {
  12. FileReader input = new FileReader("quizscore.txt");
  13. BufferedReader bufferInput = new BufferedReader(input);
  14. String line;
  15. while ((line = bufferInput.readLine ()) !=null )
  16. {
  17. for (int l = 0; l <40; l++)
  18. {
  19. for (int i = 0 ; i <6; i++)
  20. {
  21. StringTokenizer st = new StringTokenizer(line);
  22. quizScores[l][i] = st.nextToken();
  23. }
  24. }
  25. System.out.println(line);
  26.  
  27. }
  28. }
  29. catch (IOException e)
  30. {
  31. System.out.println("Error:" + e);
  32. }
  33. }
  34. System.out.println(quizScores[0][0]);
  35. System.out.println(QuizScores[34][4]);
  36. }
  37. }

Now when I run the very last line,
System.out.println(quizScores[0][0]);
System.out.println(quizScores[34][4]);

It prints out the EXACT same value, 6999, which is the value in the last line in the first column

  1.  
  2. Stud Qu1 Qu2 Qu3 Qu4 Qu5
  3. 1234 052 007 100 078 034
  4. 2134 090 036 090 077 030
  5. 3124 100 045 020 090 070
  6. 4532 011 017 081 032 077
  7. 5678 020 012 045 078 034
  8. 6134 034 080 055 078 045
  9. 7874 060 100 056 078 078
  10. 8026 070 010 066 078 056
  11. 9893 034 009 077 078 020
  12. 1947 045 040 088 078 055
  13. 2877 055 050 099 078 080
  14. 3189 022 070 100 078 077
  15. 4602 089 050 091 078 060
  16. 5405 011 011 000 078 010
  17. 6999 000 098 089 078 020

So basically from what I can deduce, the array is writing itself over and over while the loop is running. Why is that? Are my for conditions not correct?
Last edited by soulesschild; Nov 16th, 2007 at 7:42 am.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Trying to write information in a text file -> Array

 
0
  #3
Nov 16th, 2007
No they are not.

Hint:
You don't need a nested for loop.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Trying to write information in a text file -> Array

 
0
  #4
Nov 16th, 2007
import java.io.*;
import java.util.*;

public class reader
{
  public static void main ( String []args )
  {
    String[][] quizScores = new String[40][6];
    {
      try
      {
        FileReader input = new FileReader ( "quizscore.txt" );
        BufferedReader bufferInput = new BufferedReader ( input ); 
        int j = 0;

        String line;
        while ( ( line = bufferInput.readLine () ) != null )
        { 
          StringTokenizer st = new StringTokenizer ( line );
          for ( int i = 0 ; i < 6; i++ ) //after
          {
            quizScores[j][i] = st.nextToken();
            //System.out.println(quizScores[j][i]);
          }

          //System.out.println(line);

          j++;        
        }
      }
      catch ( IOException e )
      {
        System.out.println ( "Error:" + e );
      }
    }
    System.out.println ( quizScores[0][0] );
    System.out.println ( quizScores[5][0] );//typo
  }
}
I'd be using string split over string tokeniser as well.
Last edited by iamthwee; Nov 16th, 2007 at 8:24 am.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 3
Reputation: soulesschild is an unknown quantity at this point 
Solved Threads: 0
soulesschild soulesschild is offline Offline
Newbie Poster

Re: Trying to write information in a text file -> Array

 
0
  #5
Nov 16th, 2007
Thanks for the help guys, I decided to completely rethink how I'm going to store the data, and switched to trying to use an ArrayList...but I'm not really clear on how it really works.



  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class ArrayList
  5. {
  6. public static void main (String[] args)
  7. {
  8. ArrayList<Student> student = new ArrayList<Student>();
  9. }
  10. }
  11.  
  12.  
  13. class Student
  14. {
  15. public int idNum;
  16. public int quiz1;
  17. public int quiz2;
  18. public int quiz3;
  19. public int quiz4;
  20. public int quiz5;
  21.  
  22. public Student(int id, int q1, int q2, int q3, int q4, int q5)
  23. {
  24. this.idNum = id;
  25. this.quiz1 = q1;
  26. this.quiz2 = q2;
  27. this.quiz3 = q3;
  28. this.quiz4 = q4;
  29. this.quiz5 = q5;
  30. }
  31.  
  32. public static void main(String args[])
  33. {
  34. ArrayList Students = new ArrayList();
  35.  
  36. }
  37. }
  38.  
  39. public class reader
  40. {
  41. public static void main (String []args)
  42. {
  43. {
  44. try
  45. {
  46. FileReader input = new FileReader("quizscore.txt");
  47. BufferedReader bufferInput = new BufferedReader(input);
  48. String line;
  49. while ((line = bufferInput.readLine ()) !=null )
  50. {
  51. System.out.println(line);
  52. String[] result = line.split("\\s");
  53. for (int x=0; x<result.length; x++)
  54. {
  55. System.out.println(result[x]);
  56. }
  57. }
  58.  
  59.  
  60. }
  61. catch (IOException e)
  62. {
  63. System.out.println("Error:" + e);
  64. }
  65. }

Is there a way to make a seperate ArrayList class that would call upon the Students? It seems like I cannot create seperate java files, one containing the student information, the other containing the ArrayList creation function.
Last edited by soulesschild; Nov 16th, 2007 at 4:43 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Trying to write information in a text file -> Array

 
0
  #6
Nov 18th, 2007
>Is there a way to make a seperate ArrayList class that would call upon the Students

Just call it as something.idNum() or whatever...
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC