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: My code runs forever without output

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default My code runs forever without output

    There is probably a real easy solution for this but I am just missing it. The program is supposed to take an input file and then compare it to another file with a list of keywords and output the list of keywords with the number of times they occur in the test file (along with other tasks already achieved). The test file would have punctuation as well and I haven't quiet figured out how to omit those characters yet either. Any advise would be appreciated, assuming I am not completely off and need to rewrite the whole thing


    import java.util.*;
    import java.io.*;
    import java.lang.*;

    public class Project4 {



    public static void main(String[] args) throws Exception
    {


    java.io.File file = new java.io.File("keywords.txt");
    java.io.File testFile = new java.io.File("TestFile.txt");

    Set<String> set = new HashSet<String>();

    List<String> testList = new ArrayList<String>();




    Scanner input = new Scanner(file);
    Scanner testInput = new Scanner(testFile);

    while (input.hasNext())
    {
    set.add(input.next());
    // System.out.println(set);
    }

    TreeSet<String> treeSet = new TreeSet<String>(set);


    //System.out.println(treeSet);

    while (testInput.hasNext())
    {
    testList.add(testInput.next());

    }

    input.close();

    TreeMap<String, Integer> map = new TreeMap<String, Integer>();

    System.out.println(testList);


    Iterator<String> iteratorSet = set.iterator();
    Iterator<String> iteratorTest = testList.iterator();


    while (iteratorSet.hasNext())
    {
    String key = iteratorSet.next();


    while (iteratorTest.hasNext())
    {
    if (map.get(key) == null)
    {
    map.put(key, 0);
    }
    else
    {

    int value = map.get(key).intValue();
    value++;
    map.put(key,value);
    }
    }
    }

    Set<Map.Entry<String, Integer>> entrySet = map.entrySet();

    for (Map.Entry<String, Integer> entry: entrySet)
    System.out.println(entry.getValue() + "\t" + entry.getKey());
    }


    }


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: My code runs forever without output

    First, please wrap your code in the code tags ([highlight=java]Code goes here[/highlight]). Second, I might be the only one but I don't really know what your question is...does this compile? Are there exceptions? Does it behave properly? Give us something defined to help make your question easy to answer - it will help both you and us.

  3. The Following User Says Thank You to copeg For This Useful Post:

    jbinsomnia (October 8th, 2011)

  4. #3
    Junior Member
    Join Date
    Oct 2011
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: My code runs forever without output

     
    import java.util.*;
    import java.io.*;
    import java.lang.*;
     
    public class Project4 {
     
     
     
    public static void main(String[] args) throws Exception
    {
     
     
    java.io.File file = new java.io.File("keywords.txt");
    java.io.File testFile = new java.io.File("TestFile.txt");
     
    Set<String> set = new HashSet<String>();
     
    List<String> testList = new ArrayList<String>();
     
     
     
     
    Scanner input = new Scanner(file);
    Scanner testInput = new Scanner(testFile);
     
    while (input.hasNext())
    {
    set.add(input.next());
    // System.out.println(set);
    }
     
    TreeSet<String> treeSet = new TreeSet<String>(set);
     
     
    //System.out.println(treeSet);
     
    while (testInput.hasNext())
    {
    testList.add(testInput.next());
     
    }
     
    input.close();
     
    TreeMap<String, Integer> map = new TreeMap<String, Integer>();
     
    System.out.println(testList);
     
     
    Iterator<String> iteratorSet = set.iterator();
    Iterator<String> iteratorTest = testList.iterator();
     
     
    while (iteratorSet.hasNext())
    {
    String key = iteratorSet.next();
     
     
    while (iteratorTest.hasNext())
    {
    if (map.get(key) == null)
    {
    map.put(key, 0);
    }
    else
    {
     
    int value = map.get(key).intValue();
    value++;
    map.put(key,value);
    }
    }
    }
     
    Set<Map.Entry<String, Integer>> entrySet = map.entrySet();
     
    for (Map.Entry<String, Integer> entry: entrySet)
    System.out.println(entry.getValue() + "\t" + entry.getKey());
    }
     
     
    }


    The code compiles and runs but the output should list the keywords and the number of occurances they appear in the test file.

  5. #4
    Junior Member
    Join Date
    Oct 2011
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: My code runs forever without output

    The program sits running without ever producing an output.

  6. #5
    Member
    Join Date
    Mar 2011
    Posts
    84
    My Mood
    Daring
    Thanks
    17
    Thanked 1 Time in 1 Post

    Default Re: My code runs forever without output

    Quote Originally Posted by jbinsomnia View Post
    The program sits running without ever producing an output.
    i tried your code with empty keywords and TestFile, guess what programs doesn't run forever. i got an out put like
    C:\Users\Arvind\java>java Project4
    []

    C:\Users\Arvind\java>

  7. #6
    Junior Member
    Join Date
    Oct 2011
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: My code runs forever without output

    Thank you, I found that earlier today. I was trying to figure out how to delete the post so not to embarrass myself online. I revised my code and thought I was making progress until this error showed up and can't find any reference to help understand it:

    Ljava.lang.String;@13d4c61c

  8. #7
    Member
    Join Date
    Mar 2011
    Posts
    84
    My Mood
    Daring
    Thanks
    17
    Thanked 1 Time in 1 Post

    Default Re: My code runs forever without output

    Quote Originally Posted by jbinsomnia View Post
    Thank you, I found that earlier today. I was trying to figure out how to delete the post so not to embarrass myself online. I revised my code and thought I was making progress until this error showed up and can't find any reference to help understand it:

    Ljava.lang.String;@13d4c61c
    just click at thread tools at the start of your thread and mark it as solved!!

Similar Threads

  1. Java DB forever
    By potatostar in forum JDBC & Databases
    Replies: 1
    Last Post: July 20th, 2011, 02:16 PM
  2. Replies: 9
    Last Post: June 29th, 2011, 12:27 PM
  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
  5. [SOLVED] Code is correct, but incorrect output?
    By moodycrab3 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 6th, 2011, 02:32 PM

Tags for this Thread