| | |
Trying to write information in a text file -> Array
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Nov 2007
Posts: 3
Reputation:
Solved Threads: 0
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 =/
Executing this code, gives an error on the line,
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
Java Syntax (Toggle Plain Text)
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,
Java Syntax (Toggle Plain Text)
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
•
•
Join Date: Nov 2007
Posts: 3
Reputation:
Solved Threads: 0
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.
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
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?
Okay I have it compiling and running. However, one small problem. The array doesn't seem to be writing properly.
Java Syntax (Toggle Plain Text)
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
Java Syntax (Toggle Plain Text)
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?
Last edited by soulesschild; Nov 16th, 2007 at 7:42 am.
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
}
} Last edited by iamthwee; Nov 16th, 2007 at 8:24 am.
*Voted best profile in the world*
•
•
Join Date: Nov 2007
Posts: 3
Reputation:
Solved Threads: 0
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.
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.
Java Syntax (Toggle Plain Text)
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.
Last edited by soulesschild; Nov 16th, 2007 at 4:43 pm.
![]() |
Similar Threads
- Reading from text file into array (C++)
- Reading a text file Into an Array (C++)
- write the exceptions in a text file????????? (VB.NET)
- Text File (Python)
- Need Help in Reading characters from a text file (C++)
- Inputting text file data into an array, please help! (C++)
- Help Reading Info in Text File Into an Array (C++)
- Read and write to an ASCII Text file (Java)
Other Threads in the Java Forum
- Previous Thread: Delete button not working on my Java GUI Inventory
- Next Thread: JLable Alignement in Box
| Thread Tools | Search this Thread |
-xlint android api applet application array arrays automation bi binary blackberry block bluetooth chat class classes client code compile compiler component database developmenthelp draw eclipse error event exception fractal freeze game gameprogramming givemetehcodez graphics gui html ide image input integer j2me j2seprojects java javac javaprojects jetbrains jni jpanel jtable julia learningresources lego linux list login loop loops mac map method methods mobile netbeans newbie notdisplaying number online oracle page print problem program programming project qt recursion scanner screen server set singleton size sms sort sql string swing system template textfields threads time title tree tutorial-sample update variablebinding windows working xor






