Results 1 to 10 of 10
  1. #1
    blueduiker is offline Member
    Join Date
    Nov 2009
    Location
    California
    Posts
    55
    Rep Power
    0

    Default 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];
        }
        
    }

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    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?

  3. #3
    blueduiker is offline Member
    Join Date
    Nov 2009
    Location
    California
    Posts
    55
    Rep Power
    0

    Default

    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]

  4. #4
    blueduiker is offline Member
    Join Date
    Nov 2009
    Location
    California
    Posts
    55
    Rep Power
    0

    Default

    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;
    }
    
    
    }

  5. #5
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default

    Quote Originally Posted by blueduiker View Post
    I using eclipse, so I saved the Tile.txt file in C:\Users\Chris O'Brien\workspace\Jeopardy\src
    It doesn't belong in src. Put it in C:\Users\Chris O'Brien\workspace\Jeopardy instead.

    -Gary-

  6. #6
    blueduiker is offline Member
    Join Date
    Nov 2009
    Location
    California
    Posts
    55
    Rep Power
    0

    Default

    OMG GCALVIN
    IF I WAS A GIRL YOU JUST HIT MY G SPOT
    THANK YOU SO MUCH
    LOLZ

    that helped a lot haha

  7. #7
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    Quote Originally Posted by blueduiker View Post
    OMG GCALVIN
    IF I WAS A GIRL YOU JUST HIT MY G SPOT
    I think I'm going to be ill.

  8. #8
    blueduiker is offline Member
    Join Date
    Nov 2009
    Location
    California
    Posts
    55
    Rep Power
    0

    Default

    Hey man there's nothing wrong with appreciation. ;)

  9. #9
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    Hey man there's nothing wrong with appreciation.
    A simple "thank you" will do.

    And you can even mark the question as "solved".

  10. #10
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. Reading external text files from an Applet
    By Arnold in forum New To Java
    Replies: 2
    Last Post: 01-07-2010, 07:43 PM
  2. Reg: Reading Files from Text file
    By balaji csc in forum New To Java
    Replies: 1
    Last Post: 11-09-2009, 12:06 PM
  3. Reading In Text Files
    By Dukey in forum New To Java
    Replies: 4
    Last Post: 04-05-2009, 12:53 AM
  4. Reading and Writing Text Files
    By kandt in forum New To Java
    Replies: 1
    Last Post: 11-12-2008, 04:15 AM
  5. Does OS intervene when reading Java text files
    By Tina G in forum Advanced Java
    Replies: 1
    Last Post: 04-07-2008, 03:29 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •