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

    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. 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!

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    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
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    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, 02:38 PM
  2. Replies: 2
    Last Post: 04-20-2011, 05:39 PM
  3. Replies: 0
    Last Post: 12-09-2010, 05:56 PM
  4. Write a List into a Text file
    By tech2000 in forum New To Java
    Replies: 1
    Last Post: 11-13-2008, 05: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
  •