Results 1 to 7 of 7
  1. #1
    jwl
    jwl is offline Member
    Join Date
    Jul 2012
    Posts
    93
    Rep Power
    0

    Default Why can't I call readFile method

    I have a readFile() method that creates an array of the size of the first number in the file being read. In this case I have a list of 14 numbers like this in a .txt file:

    13
    100
    111
    112
    113
    115
    ...
    ...
    ... (until there are 14 numbers)

    So it creates an array of size 13 and puts the following numbers in the array. What I need help with is I am trying to test to see if the numbers after 13 print out so i can know that they were stored in the array. The problem occurs at line 39 when I try to call the readFile() method from the main method.


    Java Code:
    package readfile;
    
    import java.io.File;
    import java.io.IOException;
    import java.util.Scanner;
    
    public class ReadFile {
        private Scanner read;
        private int numData;
    
       private void openFile() {
           try {
               read = new Scanner(new File("File.txt"));
           }
           catch (IOException e) {
               System.out.println("Could not open file input");
           }
       }
      
       private int[] readFile(String File) {
           numData = read.nextInt();
           read.nextLine();
           int[] data = new int[numData];
           for (int i = 0; i < numData; i++){
                data[i] = read.nextInt();
                System.out.printf("%s\n", File);
                read.nextLine();
           }
           return data;
       }
       
       private void closeFile() {
           read.close();
       }
       
        public static void main(String[] args) {
            ReadFile file = new ReadFile();
            file.openFile();
            file.readFile(File);  //line with error cannot find symbol: variable File
            file.closeFile();
        }
    }
    Any ideas? I'm tring to print the numbers stored in the array as they are stored.
    Last edited by jwl; 10-26-2012 at 09:26 PM.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: Why can't I call readFile method

    Quote Originally Posted by jwl View Post
    The problem occurs at line 39 when I try to call the readFile() method from the main method.
    Java Code:
            file.readFile(File);  //line with error
    What error? Compiler error? Post the error message. Run time exception? Post the stack trace.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    JBelg is offline Member
    Join Date
    Oct 2012
    Posts
    36
    Rep Power
    0

    Default Re: Why can't I call readFile method

    On the line with the error you're using "File", this variable is never declared anywhere, it's also never used anywhere.
    Just remove "File" on the line with the error and remove the arguments of the method "readFile". (String File)

  4. #4
    jwl
    jwl is offline Member
    Join Date
    Jul 2012
    Posts
    93
    Rep Power
    0

    Default Re: Why can't I call readFile method

    Quote Originally Posted by DarrylBurke View Post
    What error? Compiler error? Post the error message. Run time exception? Post the stack trace.

    db
    Java Code:
    file.readFile(File);  //line with error cannot find symbol: variable File
    fixed it

  5. #5
    jwl
    jwl is offline Member
    Join Date
    Jul 2012
    Posts
    93
    Rep Power
    0

    Default Re: Why can't I call readFile method

    Quote Originally Posted by JBelg View Post
    On the line with the error you're using "File", this variable is never declared anywhere, it's also never used anywhere.
    Just remove "File" on the line with the error and remove the arguments of the method "readFile". (String File)
    I need (String File) arguments because that is how I access the content in File.txt in the readFile() method.

  6. #6
    JBelg is offline Member
    Join Date
    Oct 2012
    Posts
    36
    Rep Power
    0

    Default Re: Why can't I call readFile method

    This is where you retrieve all the data in the text file:
    Java Code:
    read = new Scanner(new File("File.txt"));
    and this is how you get your data one by one:
    Java Code:
    ...
    numData = read.nextInt();
    ...
    The string you're giving to the readFile() method is nothing more than a string.

    so:

    Java Code:
    ...
    readFile("gibberish");
    ...
    
    public int[] readFile(String File) {
    System.out.printf("%s\n", File);
    }
    would print out:
    Java Code:
    gibberish
    It's not really wrong, but it doesn't serve a real function besides spamming the string you gave.

  7. #7
    jwl
    jwl is offline Member
    Join Date
    Jul 2012
    Posts
    93
    Rep Power
    0

    Default Re: Why can't I call readFile method

    I'm still fresh on these concepts so it's easy to get my head spinning but I got it to work.

    Java Code:
    private int[] readFile() {
           numData = read.nextInt();
           read.nextLine();
           int[] data = new int[numData];
            for (int i = 0; i < numData; i++) {
                data[i] = read.nextInt();
                System.out.printf("%s\n", data[i]);
           }
           return data;
       }

Similar Threads

  1. call a method from another method in same class
    By rockstaedy in forum New To Java
    Replies: 5
    Last Post: 10-03-2012, 02:42 PM
  2. Method call
    By London in forum New To Java
    Replies: 3
    Last Post: 09-15-2011, 09:02 AM
  3. How do I call this method
    By africanhacker in forum New To Java
    Replies: 1
    Last Post: 06-29-2011, 04:03 PM
  4. how to call method?
    By leapinlizard in forum New To Java
    Replies: 9
    Last Post: 04-29-2009, 11:55 PM
  5. cannot call private method from static method
    By jon80 in forum New To Java
    Replies: 3
    Last Post: 05-07-2008, 08:37 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
  •