Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Hylekmi

macrumors regular
Original poster
Dec 8, 2010
101
0
UPDATED

I am trying to read in a file(which was in the wrong place before) and store the contents into an array.Nothing outputs for some reason. Im new to exceptions btw.

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;


public class AssignmentFive
{
public static void main (String[]args)
{
int numOfRows=30;
int numOfColumns=6;
int[][] array= new int[numOfRows][numOfColumns];//30 rows 6 columns

int row, col;
int integerEquivalence;
String line;

FileReader fin;

try {
fin = new FileReader("homework_scores.txt");
BufferedReader buff = new BufferedReader(fin);

for(row=0;row<array.length;row++)
{
line=buff.readLine();

integerEquivalence=Integer.parseInt(line);
for(col=0;col<array[row].length;col++)
System.out.print(integerEquivalence);
}//end outer for loop

for(row=0;row < array.length; row++)
{
System.out.print("Student #"+(row+1)+":\t");
for(col=0;col<array[row].length;col++)
{
System.out.print(array[row][col]+" ");
}
System.out.println();
}
}catch (IOException e) {}
}//End Main
}//end AssignmentFive
 
Last edited:

elppa

macrumors 68040
Nov 26, 2003
3,233
151
This will give a number format exception:
Code:
integerEquivalence=Integer.parseInt(line);

You can print out exceptions by doing this:
Code:
}catch (Exception e) {
    System.out.print(e);
}

You need to read each integer in from the line. One way of doing this is using a scanner:

Code:
Scanner scanner = new Scanner(line);

Then in the loop:
Code:
array[row][col] = Integer.parseInt(scanner.next());

There is not much error checking (for example - what happens if there are only 5 scores on the line?).

I will leave you to improve it as much as you see fit.

Code:
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Scanner;

public class AssignmentFive {

    public static void main(String[] args) {
        int numOfRows = 30;
        int numOfColumns = 6;
        int[][] array = new int[numOfRows][numOfColumns];//30 rows 6 columns

        int row, col;
        int integerEquivalence;
        String line = null;
        try {
            FileReader fin = new FileReader("howework_scores.txt");
            BufferedReader buff = new BufferedReader(fin);


            for (row = 0; row < array.length; row++) {


                line = buff.readLine();
                
                // integerEquivalence = Integer.parseInt(line);
                Scanner scanner = new Scanner(line);
                
                for (col = 0; col < array[row].length; col++) {
                    array[row][col] = Integer.parseInt(scanner.next());
                }
            }//end outer for loop

            for (row = 0; row < array.length; row++) {
                System.out.print("Student #" + (row + 1) + ":\t");
                for (col = 0; col < array[row].length; col++) {
                    System.out.print(array[row][col] + " ");
                }
                System.out.println();
            }
        } catch (Exception e) {
            System.out.print(e);
        }
    }//End Main
}//end AssignmentFive
 
Last edited:

Hylekmi

macrumors regular
Original poster
Dec 8, 2010
101
0
This will give a number format exception:
Code:
integerEquivalence=Integer.parseInt(line);

You can print out exceptions by doing this:
Code:
}catch (Exception e) {
    System.out.print(e);
}

You need to read each integer in from the line. One way of doing this is using a scanner:

Code:
Scanner scanner = new Scanner(line);

Then in the loop:
Code:
array[row][col] = Integer.parseInt(scanner.next());

There is not much error checking (for example - what happens if there are only 5 scores on the line?).

I will leave you to improve it as much as you see fit.

Code:
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Scanner;

public class AssignmentFive {

    public static void main(String[] args) {
        int numOfRows = 30;
        int numOfColumns = 6;
        int[][] array = new int[numOfRows][numOfColumns];//30 rows 6 columns

        int row, col;
        int integerEquivalence;
        String line = null;
        try {
            FileReader fin = new FileReader("howework_scores.txt");
            BufferedReader buff = new BufferedReader(fin);


            for (row = 0; row < array.length; row++) {


                line = buff.readLine();
                
                // integerEquivalence = Integer.parseInt(line);
                Scanner scanner = new Scanner(line);
                
                for (col = 0; col < array[row].length; col++) {
                    array[row][col] = Integer.parseInt(scanner.next());
                }
            }//end outer for loop

            for (row = 0; row < array.length; row++) {
                System.out.print("Student #" + (row + 1) + ":\t");
                for (col = 0; col < array[row].length; col++) {
                    System.out.print(array[row][col] + " ");
                }
                System.out.println();
            }
        } catch (Exception e) {
            System.out.print(e);
        }
    }//End Main
}//end AssignmentFive

My issue is that I need it to print out the numbers in the text file into the array. The numbers in the text file are separated by a tab. the issue is to get the compiler to read all of the numbers into the array then output the array. i know i need to use the split method but i have no idea where to start
 

elppa

macrumors 68040
Nov 26, 2003
3,233
151
My issue is that I need it to print out the numbers in the text file into the array. The numbers in the text file are separated by a tab. the issue is to get the compiler to read all of the numbers into the array then output the array. i know i need to use the split method but i have no idea where to start

Yes, I'm not stupid.

The code I have pasted above solves your issue. Run it and you will see.
 

Hylekmi

macrumors regular
Original poster
Dec 8, 2010
101
0
Yes, I'm not stupid.

The code I have pasted above solves your issue. Run it and you will see.

I did and it just printed out the exception. not the array

----------

Yes, I'm not stupid.

The code I have pasted above solves your issue. Run it and you will see.

Wait its a file not found exception
Im using eclipse and its not finding the file for some reason. you wouldnt know if there is a special way to save the file in eclipse would you?

----------

I did and it just printed out the exception. not the array

----------



Wait its a file not found exception
Im using eclipse and its not finding the file for some reason. you wouldnt know if there is a special way to save the file in eclipse would you?

It does it in TextWrangler too.

----------

I did and it just printed out the exception. not the array

----------



Wait its a file not found exception
Im using eclipse and its not finding the file for some reason. you wouldnt know if there is a special way to save the file in eclipse would you?

----------



It does it in TextWrangler too.

im a Friggen idiot for the last 2 hours i had a w instead of a M
 

elppa

macrumors 68040
Nov 26, 2003
3,233
151
This is because Eclipse will compile your class to a different directory.

The simplest thing to do for now is to put the full path to the file in:

Code:
/Users/loginname/Documents/howework_scores.txt
 

Hylekmi

macrumors regular
Original poster
Dec 8, 2010
101
0
This is because Eclipse will compile your class to a different directory.

The simplest thing to do for now is to put the full path to the file in:

Code:
/Users/loginname/Documents/howework_scores.txt

I cant do that becuase im only turning in the sourcecode and that would not work with the teachers file name.
 

elppa

macrumors 68040
Nov 26, 2003
3,233
151
I cant do that becuase im only turning in the sourcecode and that would not work with the teachers file name.

Check it works that way.

If you are only turning in the source code then the teacher will probably make sure the file is in the same place as the compiled java class.

Unless you have been given instructions to the contrary.

Compiling and running a project as simple as this outside of Eclipse is easy:
Code:
javac AssignmentFive.java

To run:
Code:
java AssignmentFive
 

Hylekmi

macrumors regular
Original poster
Dec 8, 2010
101
0
Check it works that way.

If you are only turning in the source code then the teacher will probably make sure the file is in the same place as the compiled java class.

Unless you have been given instructions to the contrary.

Compiling and running a project as simple as this outside of Eclipse is easy:
Code:
javac AssignmentFive.java

To run:
Code:
java AssignmentFive

I got it after screwing around with the directories for a little bit. you just have to put it between the SRC and BIN files in the project folder
 

elppa

macrumors 68040
Nov 26, 2003
3,233
151
I presume you mean the split method on the String class.

Instead of:
Code:
Scanner scanner = new Scanner(line);

Do this:
Code:
String[] scores = line.split("\t");

Instead of:
Code:
array[row][col] = Integer.parseInt(scanner.next());

Do this:
Code:
array[row][col] = Integer.parseInt(scores[col]);
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.