PDA

View Full Version : Completely Lost with this (Java)




Hylekmi
May 10, 2012, 03:50 PM
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



elppa
May 10, 2012, 04:29 PM
This will give a number format exception:
integerEquivalence=Integer.parseInt(line);

You can print out exceptions by doing this:
}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 (docs.oracle.com/javase/6/docs/api/java/util/Scanner.html):

Scanner scanner = new Scanner(line);

Then in the loop:
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.


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

Hylekmi
May 10, 2012, 04:42 PM
This will give a number format exception:
integerEquivalence=Integer.parseInt(line);

You can print out exceptions by doing this:
}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 (docs.oracle.com/javase/6/docs/api/java/util/Scanner.html):

Scanner scanner = new Scanner(line);

Then in the loop:
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.


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
May 10, 2012, 04:45 PM
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
May 10, 2012, 04:51 PM
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
May 10, 2012, 04:52 PM
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:

/Users/loginname/Documents/howework_scores.txt

Hylekmi
May 10, 2012, 04:56 PM
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:

/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
May 10, 2012, 05:07 PM
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:
javac AssignmentFive.java

To run:
java AssignmentFive

Hylekmi
May 10, 2012, 05:10 PM
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:
javac AssignmentFive.java

To run:
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
May 10, 2012, 05:36 PM
Works now?

Hylekmi
May 10, 2012, 05:45 PM
Works now?

Yes, but I need to try to get the program to work when i use the split class. Im close but if need be ill just turn in the other one.

elppa
May 10, 2012, 05:50 PM
I presume you mean the split method on the String class.

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

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

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

Do this:
array[row][col] = Integer.parseInt(scores[col]);