Results 1 to 7 of 7
Thread: Why can't I call readFile method
- 10-26-2012, 09:06 PM #1
Member
- Join Date
- Jul 2012
- Posts
- 93
- Rep Power
- 0
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.
Any ideas? I'm tring to print the numbers stored in the array as they are stored.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(); } }Last edited by jwl; 10-26-2012 at 09:26 PM.
- 10-26-2012, 09:20 PM #2
- 10-26-2012, 09:25 PM #3
Member
- Join Date
- Oct 2012
- Posts
- 32
- Rep Power
- 0
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)
- 10-26-2012, 09:27 PM #4
Member
- Join Date
- Jul 2012
- Posts
- 93
- Rep Power
- 0
- 10-26-2012, 09:31 PM #5
Member
- Join Date
- Jul 2012
- Posts
- 93
- Rep Power
- 0
- 10-26-2012, 09:46 PM #6
Member
- Join Date
- Oct 2012
- Posts
- 32
- Rep Power
- 0
Re: Why can't I call readFile method
This is where you retrieve all the data in the text file:
and this is how you get your data one by one:Java Code:read = new Scanner(new File("File.txt"));
The string you're giving to the readFile() method is nothing more than a string.Java Code:... numData = read.nextInt(); ...
so:
would print out:Java Code:... readFile("gibberish"); ... public int[] readFile(String File) { System.out.printf("%s\n", File); }
It's not really wrong, but it doesn't serve a real function besides spamming the string you gave.Java Code:gibberish
- 10-26-2012, 10:11 PM #7
Member
- Join Date
- Jul 2012
- Posts
- 93
- Rep Power
- 0
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
-
call a method from another method in same class
By rockstaedy in forum New To JavaReplies: 5Last Post: 10-03-2012, 02:42 PM -
Method call
By London in forum New To JavaReplies: 3Last Post: 09-15-2011, 09:02 AM -
How do I call this method
By africanhacker in forum New To JavaReplies: 1Last Post: 06-29-2011, 04:03 PM -
how to call method?
By leapinlizard in forum New To JavaReplies: 9Last Post: 04-29-2009, 11:55 PM -
cannot call private method from static method
By jon80 in forum New To JavaReplies: 3Last Post: 05-07-2008, 08:37 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks