Results 1 to 3 of 3
Thread: Friends List (from text file)
- 11-11-2011, 11:46 PM #1
Member
- Join Date
- Jul 2011
- Posts
- 1
- Rep Power
- 0
Friends List (from text file)
I am getting the following error when running the file (it works fine if I implement each class separately):
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
No enclosing instance of type SocialNetwork is accessible. Must qualify the allocation with an enclosing instance of type SocialNetwork (e.g. x.new A() where x is an instance of SocialNetwork).
at SocialNetwork.main(SocialNetwork.java:20)
My code is below:
Java Code:import java.io.*; import java.util.StringTokenizer; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; public class SocialNetwork { public static void main(String[] args) throws Exception { BufferedReader keyboard = new BufferedReader(new InputStreamReader( System.in), 1); System.out .println("Hello! " + "Please enter the name of your test file: " + "\n**Hint** for this assignment the file name is: friendsFile.txt\n" String fileName = keyboard.readLine(); System.out.println(fileName);// FileLine doLine = new FileLine(); doLine.readList(fileName); } public class InStringFile { // read the file private BufferedReader in; // read each line private String nextLine; // handle exceptions public InStringFile(String filename) { // line by line input try { in = new BufferedReader(new FileReader(filename)); nextLine = in.readLine(); } catch (FileNotFoundException ee) { System.out.println("We're sorry,\n" + "File " + filename + " cannnot be found."); System.exit(0); } catch (IOException e) { System.out.println("We're sorry,\n" + "File " + filename + " cannot be read."); System.exit(0); } } // reads the file as string public String read() { String current = nextLine; try { nextLine = in.readLine(); } // catch exception catch (IOException e) { System.out.println("We're sorry, this file cannot be read."); System.exit(0); } return current; } public boolean endOfFile() { return (nextLine == null); } // close the file public void close() { try { in.close(); in = null; } // catch if file cannot be closed catch (IOException e) { System.out.println("Problem closing file."); System.exit(0); } } } public class FileLine { public void readList(String fileName) throws Exception { // opens the file and controls file reading InStringFile reader = new InStringFile(fileName); System.out.println("\nFile Found!" + " Now reading from file: " + fileName + "\n"); // line by line read String line; do { line = (reader.read()); // print the friend list System.out.println("The following friend-set exists: " + line); this.TokenizeString(line); } while (!reader.endOfFile()); reader.close(); } // number of friends public void TokenizeString(String nameList) { StringTokenizer tokens = new StringTokenizer(nameList); System.out.println("The number of friends in this friend-set is: " + tokens.countTokens()); } } }
Also, I would like to know which friend-set has the greatest number of friends (i.e. in the test file Joe would have the greatest number of friends)
The test file includes the following data:
Joe Sue Meg Ry Luke
Kay Trey Phil George
It shouldn't be limited to only two friend-sets though...
Thanks in advanced for any guidance!
-
Re: Friends List (from text file)
Why are you nesting classes inside one another? Why not instead simply give each class its own file and be done with it?
- 11-12-2011, 01:42 AM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Similar Threads
-
Reading from a text file, then writing back to Text Area in Reverse
By medic642 in forum New To JavaReplies: 8Last Post: 07-17-2011, 02:38 PM -
how to read list of name from text file into a binary search tree?
By Javanoobs in forum New To JavaReplies: 2Last Post: 04-20-2011, 05:39 PM -
Applet program to open a text file and display the content in text area
By bitse in forum Java AppletsReplies: 0Last Post: 12-09-2010, 05:56 PM -
Write a List into a Text file
By tech2000 in forum New To JavaReplies: 1Last Post: 11-13-2008, 05:09 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks