The Artima Developer Community
Sponsored Link

Java Answers Forum
Stuck & project due soon! HELP!!!

2 replies on 1 page. Most recent reply: May 6, 2003 11:32 AM by Senthoorkumaran Punniamoorthy

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
Victor VanAlter

Posts: 4
Nickname: cadetvva
Registered: May, 2003

Stuck & project due soon! HELP!!! Posted: May 5, 2003 5:08 PM
Reply to this message Reply
Advertisement
Anyone who can look at my code and help me understand how to count the word frequencies in the sorted array I have created will be admired forever!!! I have coded for hours and hours, but nothing is working---I am absolutely clueless where to go next, and using the Comparator Interface is puzzling me to no end!!!

Thank you in advance for your kind assistance!!!


As posted 5/4/03:
Sir/Madame:

I am working on a program that is to take user text, put it in a sorted array, then display words and their frequencies. I am getting stuck on the frequencies-- I assume I need to make an int[], but am confused how to return both the word(String) and frequency(int) to the tester program. I visualize a two dimensional array, but cannot pull it together for coding. I have read the postings for word counts and the recently answered Set question Duane posed, but I am still unsure how my code should look.

CODE:
import java.util.*;

public class StringInfo extends Object
{

private StringTokenizer st;
private String string;

public StringInfo(String string)
{

this.string = string;
}

public String listWords()
{
st = new StringTokenizer(string, " \t\n\r\f.?,;:!");
String returnString = "";

while(st.hasMoreTokens())
{
returnString = returnString + st.nextToken() + "\n";
}

return returnString;
}

public int wordCount()
{
st = new StringTokenizer(string, " \t\n\r\f.?,;:!");
return st.countTokens();
}

public String listWordsInOrder()
{
String returnString = "";
st = new StringTokenizer(string, " \t\n\r\f.?,;:!");
String[] sa = new String[wordCount()];
int index = 0;




while(st.hasMoreTokens())
{
sa[index++] = st.nextToken();
}


Arrays.sort(sa);

for(int i = 0; i < sa.length; i++)
{
returnString = returnString + sa + "\n";
}

return returnString;
}

}




CODE FOR TESTER PROGRAM:
public class StringInfoTester extends Object
{

public static void main(String[] args)
{
StringInfo si = new StringInfo("Hello, my name is Victor. \n What is your name?");
System.out.println(si.listWords() + "\n");
System.out.println("There are " + si.wordCount() + " words in the above list.");
System.out.println(" ");
System.out.println("The sorted list is: ");
System.out.println(si.listWordsInOrder());
}
}


I am also wondering how I might use the Comparator Interface with this program.

Any help would be very much appreciated!

CVA


Senthoorkumaran Punniamoorthy

Posts: 335
Nickname: senthoor
Registered: Mar, 2002

Re: Stuck & project due soon! HELP!!! Posted: May 6, 2003 11:23 AM
Reply to this message Reply
/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2003</p>
 * <p>Company: </p>
 * @author unascribed
 * @version 1.0
 */
 
import java.util.*;
 
public class StringInfo
{
 
  private StringTokenizer st;
  private String string;
 
  public StringInfo(String string)
  {
 
    this.string = string;
  }
 
  public String listWords()
  {
    st = new StringTokenizer(string, " \t\n\r\f.?,;:!");
    String returnString = "";
 
    while(st.hasMoreTokens())
    {
      returnString = returnString + st.nextToken() + "\n";
    }
 
    return returnString;
  }
 
  public int wordCount()
  {
    st = new StringTokenizer(string, " \t\n\r\f.?,;:!");
    return st.countTokens();
  }
 
  public String listWordsInOrder()
  {
    String returnString = "";
    st = new StringTokenizer(string, " \t\n\r\f.?,;:!");
    String[] sa = new String[wordCount()];
    int index = 0;
 
    while(st.hasMoreTokens())
    {
      sa[index++] = st.nextToken();
    }
 
    Arrays.sort(sa);
 
    for(int i = 0; i < sa.length; i++)
    {
      returnString = returnString + sa[i] + "\n";
    }
 
    return returnString;
  }
 
  public static void main(String[] args)
  {
    StringInfo si = new StringInfo("Hello, my name is Victor. \n What is your name?");
    System.out.println(si.listWords() + "\n");
    System.out.println("There are " + si.wordCount() + " words in the above list.");
    System.out.println(" ");
    System.out.println("The sorted list is: ");
    System.out.println(si.listWordsInOrder());
  }
}

Senthoorkumaran Punniamoorthy

Posts: 335
Nickname: senthoor
Registered: Mar, 2002

Re: Stuck & project due soon! HELP!!! Posted: May 6, 2003 11:32 AM
Reply to this message Reply
import java.util.*;
 
public class StringInfo
{
 
  private StringTokenizer st;
  private String string;
 
  public StringInfo(String string)
  {
 
    this.string = string;
  }
 
  public String listWords()
  {
    st = new StringTokenizer(string, " \t\n\r\f.?,;:!");
    String returnString = "";
 
    while(st.hasMoreTokens())
    {
      returnString = returnString + st.nextToken() + "\n";
    }
 
    return returnString;
  }
 
  public int wordCount()
  {
    st = new StringTokenizer(string, " \t\n\r\f.?,;:!");
    return st.countTokens();
  }
 
  public String listWordsInOrder()
  {
    String returnString = "";
    st = new StringTokenizer(string, " \t\n\r\f.?,;:!");
    String[] sa = new String[wordCount()];
    int index = 0;
 
    while(st.hasMoreTokens())
    {
      sa[index++] = st.nextToken();
    }
 
    Arrays.sort(sa);
 
    for(int i = 0; i < sa.length; i++)
    {
      returnString = returnString + sa[i] + "\n";
    }
 
    return returnString;
  }
 
  public static void main(String[] args)
  {
    StringInfo si = new StringInfo("Hello, my name is Victor. \n What is your name?");
    System.out.println(si.listWords() + "\n");
    System.out.println("There are " + si.wordCount() + " words in the above list.");
    System.out.println(" ");
    System.out.println("The sorted list is: ");
    System.out.println(si.listWordsInOrder());
  }
}


What I have posted above is the code which is compiling. And also I did some changes so that you can see the sorted list. The line I changed was

 returnString = returnString + sa[i] + "\n";


As you can see it has not got sorted properly since in String comparison "A" is less than "a". This way of comparing leads to something like so basically a word "It" will be less than "at" but it should be other way around. To over come this when you read the string convert all the chars to CAPS and then sort them.

Flat View: This topic has 2 replies on 1 page
Topic: projectDue = HELP; Previous Topic   Next Topic Topic: Swing applet - bank account

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use