Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Analyzing Input Files, Code Runs Forever

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Analyzing Input Files, Code Runs Forever

    So, my assignment is to write a program that takes a file as an input and give some statistics on the file. Right now, I'm just trying to find the average number of words per sentence.

    I extended the Echo class,

    import java.util.Scanner;
      import java.io.*;
     
      public class Echo{
        String fileName; // external file name
        Scanner scan; // Scanner object for reading from external file
     
        public Echo(String f) throws IOException
        {
         fileName = f;
         scan = new Scanner(new FileReader(fileName));
       }
     
       public void readLines(){ // reads lines, hands each to processLine
         while(scan.hasNext()){
           processLine(scan.nextLine());
         }
         scan.close();
       }
     
       public void processLine(String line){ // does the real processing work
         System.out.println(line);
       }
    }

    With my class WorldMap,

     import java.io.*;
    import java.util.*;
     
    public class WorldMap extends Echo{
      int numWords = 0; int numSentence = 0;
     
      public WorldMap(String f) throws IOException{
        super(f);
      }
     
      public double getAvg(){
        return ((double)numWords/numSentence);
      }
     
      public void processLine(String line){
        try{
        StringTokenizer sentence = new StringTokenizer(line, ".?,"); // Breaks line by punctuation, into sentences
        StringTokenizer words = new StringTokenizer(line, " "); // Breaks line by spaces, into words
        while (words.hasMoreTokens()){
          numWords++;
        }
        while (sentence.hasMoreTokens()){
          numSentence++;
        }
    }
        catch (Exception e){
          System.out.println("Invaid line: Line " + line);
        }
    }
    }

    & Here is my main, driver class

    import java.util.*;
    import java.io.*;
     
    public class WorldMapDriver{
      public static void main(String[] args) throws IOException{
        try{
          Scanner scan = new Scanner(System.in);
          System.out.println("Enter the name of a text file");
          String file = scan.next();
          WorldMap world = new WorldMap(file);
          world.readLines();
          world.getAvg();
        }
        catch (FileNotFoundException e){
          System.out.println("File not found");
        }
      }
    }

    I don't know much about reading input files, but I can't see why this program won't work. When I enter the location of a file, nothing happens... the client just sort of freezes like its still thinking.

    The code compiles fine, with no errors or warnings.

    Any help is greatly appreciated!


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Analyzing Input Files, Code Runs Forever

    Try debugging the code by adding some println() statements to print out messages as the executes. Be sure to put one inside any loops that could be long running. The print out will show you where the code is executing. If you also print the values of the variables the code uses, that will help you understand what the program is doing.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Nov 2013
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Analyzing Input Files, Code Runs Forever

    Quote Originally Posted by Norm View Post
    Try debugging the code by adding some println() statements to print out messages as the executes. Be sure to put one inside any loops that could be long running. The print out will show you where the code is executing. If you also print the values of the variables the code uses, that will help you understand what the program is doing.
    Ah, good idea. The problem I found is that it doesn't stop counting the number of words, or it is taking too long.
    Is
    StringTokenizer words = new StringTokenizer(line, " ");
    The write code to use here to break a line into words? I think it might just be breaking into individual characters which is taking too long.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Analyzing Input Files, Code Runs Forever

    it doesn't stop counting the number of words, or it is taking too long.
    Have you located a loop that is going forever?

    breaking into individual characters which is taking too long.
    Probably not a problem. Try testing with a short file.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Nov 2013
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Analyzing Input Files, Code Runs Forever

    Quote Originally Posted by Norm View Post
    Have you located a loop that is going forever?


    Probably not a problem. Try testing with a short file.
    Yes, this loop is running forever:
    while (words.hasMoreTokens()){
          numWords++;
          System.out.println("Word Count: " + numWords);
        }

    I also tried using
    StringTokenizer words = new StringTokenizer(line, "\\W");
    instead of
    (line, " ");

    But the same thing happens

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Analyzing Input Files, Code Runs Forever

    Where is the code reading any tokens from the StringTokenizer? It asks if there is one, but never gets it so there is always one ready to be read.
    If you don't understand my answer, don't ignore it, ask a question.

  7. The Following User Says Thank You to Norm For This Useful Post:

    jwr (November 23rd, 2013)

  8. #7
    Junior Member
    Join Date
    Nov 2013
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Analyzing Input Files, Code Runs Forever

    Quote Originally Posted by Norm View Post
    Where is the code reading any tokens from the StringTokenizer? It asks if there is one, but never gets it so there is always one ready to be read.
    Ahh, I see. So now I just need to make use of
    words.nextToken()
    somehow... Is there a way I can do this without having the nextToken printed every time?

    --- Update ---

    Ah, nevermind. That was a silly question, I just have to write words.nextToken() in there. Thanks again Norm!

Similar Threads

  1. A different code runs
    By chized in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 25th, 2013, 09:31 PM
  2. My code runs forever without output
    By jbinsomnia in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 8th, 2011, 07:53 AM
  3. while(true){ section of code runs 200 times}
    By jack_nutt in forum Java Theory & Questions
    Replies: 7
    Last Post: June 23rd, 2011, 07:06 AM
  4. while(true){ section of code runs only once}
    By jack_nutt in forum Java Theory & Questions
    Replies: 5
    Last Post: June 19th, 2011, 06:15 PM