Results 1 to 7 of 7
  1. #1
    Nazrininator is offline Touhou-Stepmania Fan
    Join Date
    May 2014
    Location
    USA
    Posts
    11
    Rep Power
    0

    Default Reading files from a text file.

    So I have a text file, and I want my Java program to store names from the text file. How do I do that? This is what I have so far.

    Java Code:
    import java.util.Scanner;
    import java.io.File;
    
    BingoCard[] cards = new BingoCard[n]; //Array of BingoCard objects, n being the length of the Array
    Scanner sc = new Scanner(File("Names")); //Names is the name of the file
    for(int c = 0; c < cards.length; c++)
       cards[c] = new BingoCard(sc.nextLine);
    Here's the constructor.

    Java Code:
    private string myName;
    public BingoCard(String name)
    {
       myName = name;
    }
    Whenever I compile, I get this error message.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,449
    Rep Power
    25

    Default Re: Reading files from a text file.

    , I get this error message.
    You left out the error message. Copy its full text and paste it here.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Nazrininator is offline Touhou-Stepmania Fan
    Join Date
    May 2014
    Location
    USA
    Posts
    11
    Rep Power
    0

    Default Re: Reading files from a text file.

    OK.

    Java Code:
    ----jGRASP exec: javac -g BingoCards.java
    
    BingoCards.java:12: error: cannot find symbol
             cards[c] = new BingoCard(sc.nextLine);
                                        ^
      symbol:   variable nextLine
      location: variable sc of type Scanner
    1 error
    
     ----jGRASP wedge2: exit code for process is 1.
     ----jGRASP: operation complete.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,449
    Rep Power
    25

    Default Re: Reading files from a text file.

    error: cannot find symbol
    symbol: variable nextLine
    location: variable sc of type Scanner
    Look at the line with the error:
    Java Code:
        cards[c] = new BingoCard(sc.nextLine);
    The compiler sees a reference to the variable: nextLine in the Scanner class and can NOT find that variable defined there.
    If you are trying to call a method, methods require () at the end of the name.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    Nazrininator is offline Touhou-Stepmania Fan
    Join Date
    May 2014
    Location
    USA
    Posts
    11
    Rep Power
    0

    Default Re: Reading files from a text file.

    Thanks, but then I encountered another error, and I don't know how to solve it.

    Java Code:
     ----jGRASP exec: javac -g BingoCards.java
    
    BingoCards.java:10: error: unreported exception FileNotFoundException; must be caught or declared to be thrown
          Scanner sc = new Scanner(new File("Names"));
                       ^
    1 error
    
     ----jGRASP wedge2: exit code for process is 1.
     ----jGRASP: operation complete.
    I tried using throws IOException, but it gave me a run-time error message.
    Java Code:
     ----jGRASP exec: java BingoCards
    
    Exception in thread "main" java.io.FileNotFoundException: Names (The system cannot find the file specified)
    	at java.io.FileInputStream.open(Native Method)
    	at java.io.FileInputStream.<init>(FileInputStream.java:146)
    	at java.util.Scanner.<init>(Scanner.java:656)
    	at BingoCards.main(BingoCards.java:10)
    
     ----jGRASP wedge2: exit code for process is 1.
     ----jGRASP: operation complete.
    Last edited by Nazrininator; 05-26-2014 at 07:42 PM.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,449
    Rep Power
    25

    Default Re: Reading files from a text file.

    java.io.FileNotFoundException: Names (The system cannot find the file specified)
    Do you understand the error message? It says what the problem is.
    Make sure there is a file named: Names for the program to read.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    Nazrininator is offline Touhou-Stepmania Fan
    Join Date
    May 2014
    Location
    USA
    Posts
    11
    Rep Power
    0

    Default Re: Reading files from a text file.

    Oh, I got it. I forgot to add .txt. Thanks!

Similar Threads

  1. Reading Text Files
    By javaman1 in forum New To Java
    Replies: 3
    Last Post: 02-27-2011, 02:13 AM
  2. Reading raw text files
    By DrSaturn in forum Android
    Replies: 0
    Last Post: 02-24-2011, 07:07 PM
  3. Reg: Reading Files from Text file
    By balaji csc in forum New To Java
    Replies: 1
    Last Post: 11-09-2009, 11:06 AM
  4. Reading In Text Files
    By Dukey in forum New To Java
    Replies: 4
    Last Post: 04-04-2009, 11:53 PM
  5. Applet - reading text files packed into JAR file
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-08-2008, 09:15 AM

Posting Permissions

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