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 =/

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);
			
			String line;
			while ((line = bufferInput.readLine ()) !=null )
			{
			for (int l = 0; l <=40; l++)
			{
				for (int i = 0 ; i <=6; i++)
					{
					StringTokenizer st = new StringTokenizer(line);
					quizScores[l][i] = st.nextToken();
					}
				System.out.println("Read:" + line);
				System.out.println(quizScores[4][2]);
			}
			}
		}
		catch (IOException e)
		{
			System.out.println("Error:" + e);
		}	
	}
}	
}

Executing this code, gives an error on the line,

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

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.

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);
			String line;
			while ((line = bufferInput.readLine ()) !=null )
			{
				for (int l = 0; l <40; l++)
				{
					for (int i = 0 ; i <6; i++)
					{
					StringTokenizer st = new StringTokenizer(line);
					quizScores[l][i] = st.nextToken();
					}
				}
			System.out.println(line);
			
			}
		}
		catch (IOException e)
		{
			System.out.println("Error:" + e);
		}	
	}
	System.out.println(quizScores[0][0]);
        System.out.println(QuizScores[34][4]);
}	
}

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

Stud Qu1 Qu2 Qu3 Qu4 Qu5
1234 052 007 100 078 034
2134 090 036 090 077 030
3124 100 045 020 090 070
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
3189 022 070 100 078 077
4602 089 050 091 078 060
5405 011 011 000 078 010
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?

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.

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.

import java.io.*;
import java.util.*;

public class ArrayList 
{
	public static void main (String[] args)
	{
	ArrayList<Student> student = new ArrayList<Student>();
	}
}


class Student 
{
	public int idNum;
	public int quiz1;
	public int quiz2;
	public int quiz3;
	public int quiz4;
	public int quiz5;
	
	public Student(int id, int q1, int q2, int q3, int q4, int q5)
	{
		this.idNum = id;
		this.quiz1 = q1;
		this.quiz2 = q2;
		this.quiz3 = q3;
		this.quiz4 = q4;
		this.quiz5 = q5;
	}

	public static void main(String args[])
	{
		ArrayList Students = new ArrayList();
		
	}
}

public class reader 
{
public static void main (String []args)
{
	{	
		try
		{
			FileReader input = new FileReader("quizscore.txt");
			BufferedReader bufferInput = new BufferedReader(input);
			String line;
			while ((line = bufferInput.readLine ()) !=null )
			{
			System.out.println(line);
			String[] result = line.split("\\s");
			for (int x=0; x<result.length; x++)
			{
				System.out.println(result[x]);
			}
			}
	
					
		}
		catch (IOException e)
		{
			System.out.println("Error:" + e);
		}	
	}

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.

>Is there a way to make a seperate ArrayList class that would call upon the Students

Just call it as something.idNum() or whatever...

This article has been dead for over six months. Start a new discussion instead.