Computing.Net > Forums > Programming > Java I/O Help

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Java I/O Help

Reply to Message Icon

Name: icebreaker9
Date: July 6, 2005 at 15:16:44 Pacific
OS: XP Pro
CPU/Ram: 2200+, 1 gig
Comment:

Ok, Im working on a program which needs to read a text file like so :
0000000000
0011110000
0011110000
0010010000
0000000000
0100001000
0010010000
0001100000
0000000000
0000000000

and 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



Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Java I/O Help

Java File I/O www.computing.net/answers/programming/java-file-io/12360.html

BlueJ I/O Language Question www.computing.net/answers/programming/bluej-io-language-question/11288.html

Simple text file I/O www.computing.net/answers/programming/simple-text-file-io-/4829.html