Results 1 to 10 of 10
Thread: Reading in Text Files
- 05-17-2010, 01:29 AM #1
Member
- Join Date
- Nov 2009
- Location
- California
- Posts
- 55
- Rep Power
- 0
Reading in Text Files
Does anyone know why I can't read Tile.txt???????
I get this error from the code below
Exception in thread "main" java.io.FileNotFoundException: Tile.txt (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileReader.<init>(Unknown Source)
at GameBoard.<init>(GameBoard.java:33)
at GameBoardTester.main(GameBoardTester.java:15)
Java Code:import java.io.FileReader; import java.io.FileWriter; import java.io.BufferedReader; import java.io.PrintWriter; import java.io.IOException; import java.io.FileNotFoundException; public class GameBoard { private BufferedReader inputStream; private Tile [][] gameTiles; public GameBoard (String fileName) throws IOException { gameTiles = new Tile [5][5]; String[] data = new String[100]; /*for (int row = 0; row < 5; row++) for (int col = 0; col < 5; col++) { Tile temp = new Tile ("question", "answer", 99); gameTiles[row][col] = temp; } */ try { inputStream = new BufferedReader(new FileReader("Tile.txt")); String line; int lineCount = 0; while ((line = inputStream.readLine()) != null) { System.out.println ("\nLine: " + lineCount + ": " + line); String thisLine = line; data[lineCount] = line; lineCount++; } /*} catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }*/ } finally { if (inputStream != null) { inputStream.close(); } System.out.println ("\n\nQuestion: " + data[0]); System.out.println ("Answer: " + data[1]); System.out.println ("Value: " + data[2]); } }// end constructor public Tile getTile (int r, int c) { return gameTiles [r] [c]; } }
- 05-17-2010, 01:42 AM #2
The java program does not find the file: Tile.txt in the current directory.
Where are the files: .class and data located?
How are you executing the program?
- 05-17-2010, 01:46 AM #3
Member
- Join Date
- Nov 2009
- Location
- California
- Posts
- 55
- Rep Power
- 0
I using eclipse, so I saved the Tile.txt file in C:\Users\Chris O'Brien\workspace\Jeopardy\src
The other files are in there as well.
I have 3 java files and that Tile.txt file. The two other files are:
[CODE]
import java.io.IOException;
public class GameBoardTester {
public static void main(String[] args) throws IOException {
Tile theTile;
GameBoard gb = new GameBoard ("Tile.txt");
for (int i = 0; i < 5; i++)
{
System.out.println("\nNEW COLUMN\n");
for (int j = 0; j < 5; j++)
{
theTile = gb.getTile(i,j);
System.out.println ("\n" + theTile);
}
}
}
}
[\CODE]
[CODE]
public class Tile {
private String question;
private String answer;
private int value;
private boolean isAnswered;
public Tile(String q, String a, int v) {
isAnswered = false;
question = q;
answer = a;
value = v;
}
public String toString ()
{
String s = question;
s+= "\n" + answer;
s+= "\nScore: " + value;
return s;
}
}
[\CODE]
- 05-17-2010, 02:00 AM #4
Member
- Join Date
- Nov 2009
- Location
- California
- Posts
- 55
- Rep Power
- 0
sorry the code tags didn't work
Java Code:import java.io.IOException; public class GameBoardTester { public static void main(String[] args) throws IOException { Tile theTile; GameBoard gb = new GameBoard ("Tile.txt"); for (int i = 0; i < 5; i++) { System.out.println("\nNEW COLUMN\n"); for (int j = 0; j < 5; j++) { theTile = gb.getTile(i,j); System.out.println ("\n" + theTile); } } } }Java Code:public class Tile { private String question; private String answer; private int value; private boolean isAnswered; public Tile(String q, String a, int v) { isAnswered = false; question = q; answer = a; value = v; } public String toString () { String s = question; s+= "\n" + answer; s+= "\nScore: " + value; return s; } }
- 05-17-2010, 02:00 AM #5
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
- 05-17-2010, 02:06 AM #6
Member
- Join Date
- Nov 2009
- Location
- California
- Posts
- 55
- Rep Power
- 0
OMG GCALVIN
IF I WAS A GIRL YOU JUST HIT MY G SPOT
THANK YOU SO MUCH
LOLZ
that helped a lot haha
- 05-17-2010, 03:06 AM #7
Senior Member
- Join Date
- May 2010
- Posts
- 436
- Rep Power
- 4
- 05-17-2010, 04:53 AM #8
Member
- Join Date
- Nov 2009
- Location
- California
- Posts
- 55
- Rep Power
- 0
Hey man there's nothing wrong with appreciation. ;)
- 05-17-2010, 05:04 AM #9
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
A simple "thank you" will do.Hey man there's nothing wrong with appreciation.
And you can even mark the question as "solved".
- 05-17-2010, 05:45 AM #10
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
If you are satisfied with the solution, please mark the thread solved from tools menu.
Similar Threads
-
Reading external text files from an Applet
By Arnold in forum New To JavaReplies: 2Last Post: 01-07-2010, 06:43 PM -
Reg: Reading Files from Text file
By balaji csc in forum New To JavaReplies: 1Last Post: 11-09-2009, 11:06 AM -
Reading In Text Files
By Dukey in forum New To JavaReplies: 4Last Post: 04-04-2009, 11:53 PM -
Reading and Writing Text Files
By kandt in forum New To JavaReplies: 1Last Post: 11-12-2008, 03:15 AM -
Does OS intervene when reading Java text files
By Tina G in forum Advanced JavaReplies: 1Last Post: 04-07-2008, 02:29 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks