Results 1 to 3 of 3
  1. #1
    Kayla is offline Member
    Join Date
    Jul 2011
    Rep Power

    Default 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. A() where x is an instance of SocialNetwork).

    at SocialNetwork.main(

    My code is below:

    Java Code:
    import java.util.StringTokenizer;
    public class SocialNetwork {
    	public static void main(String[] args) throws Exception {
    		BufferedReader keyboard = new BufferedReader(new InputStreamReader(, 1);
    		.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();
    		FileLine doLine = new FileLine();
    	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.");
    			} catch (IOException e) {
    				System.out.println("We're sorry,\n" + "File " + filename
    						+ " cannot be read.");
    		// 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.");
    			return current;
    		public boolean endOfFile() {
    			return (nextLine == null);
    		// close the file
    		public void close() {
    			try {
    				in = null;
    			// catch if file cannot be closed
    			catch (IOException e) {
    				System.out.println("Problem closing file.");
    	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 = (;
    				// print the friend list
    				System.out.println("The following friend-set exists: " + line);
    			} while (!reader.endOfFile());
    		// 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!

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Blog Entries
    Rep Power

    Default 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?

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    New Zealand
    Rep Power

    Default Re: Friends List (from text file)

    I agree with Fubarable. If you are working with an assignment that makes special demands, perhaps you could post what it says. (In case you are misinterpreting what it asks you to do, eg)

Similar Threads

  1. Replies: 8
    Last Post: 07-17-2011, 03:38 PM
  2. Replies: 2
    Last Post: 04-20-2011, 06:39 PM
  3. Replies: 0
    Last Post: 12-09-2010, 06:56 PM
  4. Write a List into a Text file
    By tech2000 in forum New To Java
    Replies: 1
    Last Post: 11-13-2008, 06:09 AM

Tags for this Thread

Posting Permissions

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