Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Ok, Im working on a program which needs to read a text file like so :
0000000000
0011110000
0011110000
0010010000
0000000000
0100001000
0010010000
0001100000
0000000000
0000000000and put the numbers into a matrix of ints, so I wrote
import java.io.*;
public class IO {
public static void main(String args[]) {
int[][] background= new int[10][10];
BufferedReader input = null;
try {
FileReader file = new FileReader("myText.txt"); // Open the file.
input = new BufferedReader(file); // Tie 'input' to this file.
}
catch(FileNotFoundException x) { // The file may not exist.
System.err.println("File not found: " + args[0]);
System.exit(2);
}try {
String line;
for(int s=0;s<10;s++)
{
line = input.readLine();
for(int a=0;a<10;a++)
{
char temp=line.charAt(a);
String temp2="" + temp;
background[s][a]=Integer.parseInt(temp2);
}
}
}
catch(IOException x) {
x.printStackTrace();
}
for(int s=0;s<10;s++){
System.out.println();
for(int a=0;a<10;a++)
System.out.print(background[s][a]);}
}
}whichs works, but when I take the part out of
it that I need and put it into the class I need it for,import java.io.*;
public class envy extends envycollision
{
int[][] background;
String line;
public envy(int arraynum)
{
background =new int [10][10];
BufferedReader input = null;
try
{
FileReader file = new FileReader("C:\\myText.txt"); // Open the file.
input = new BufferedReader(file); // Tie 'input' to this file.
}
catch(FileNotFoundException x) {}
try {
for(int s=1;s<11;s++)
{
line = input.readLine();
for(int a=0;a<10;a++)
{
char temp=line.charAt(a);
String temp2="" + temp;
background[s-1][a]=Integer.parseInt(temp2);
}
}
}
catch(IOException x) {}
}
}
it compiles correctly but when I open the applet it fails to load. If I replace that code with a pre-made array the applet runs so Im wondering why the code above works in one case and not in another, any help would be great

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |