Results 1 to 7 of 7
  1. #1
    share is offline Member
    Join Date
    Feb 2009
    Posts
    4
    Rep Power
    0

    Unhappy what wrong wiith this code?

    the application is require count the occurrences of words with alphabetically.






    import java.util.*;
    import javax.swing.JOptionPane;

    public class CountOccurrenceOfWords {
    public static void main(String[] args) {
    //Prompt the user to enter a string
    String s = JOptionPane.showInputDialog("Enter a string:");


    // Create a hash map to hold words as key and count as value
    Map<String, Integer> hashMap = new HashMap<String, Integer>();

    String[] words = text.split("[ ]");
    for (int i = 0; i < words.length; i++) {
    if (words[i].length() > 1) {
    if (hashMap.get(words[i]) != null) {
    int value = hashMap.get(words[i]).intValue();
    value++;
    hashMap.put(words[i], value);
    }
    else
    hashMap.put(words[i], 1);
    }
    }

    //Count each Word in the string
    public static String[] countWords(String s){
    String [] words =new int[];

    for(int i=0;i<s.length();i++){
    if(Character.isWord(s.chartAt(i)))
    counts[s.chartAt(i)-"a"]++;
    }

    // Create a tree map from the hash map
    Map<String, Integer> treeMap =
    new TreeMap<String, Integer>(hashMap);

    // Display mappings
    System.out.println("Display words and their count in " +
    "ascending order of the words");
    System.out.print(treeMap);
    }


    }
    after compile it...that show
    public static String[] countWords(String s){
    String [] words =new int[];

    is illegal start of expression..

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

    Default

    for one, a String array can never be assigned an int array.

  3. #3
    share is offline Member
    Join Date
    Feb 2009
    Posts
    4
    Rep Power
    0

    Default

    so i should change to what?just delete the int?

  4. #4
    Niveditha's Avatar
    Niveditha is offline Senior Member
    Join Date
    May 2008
    Posts
    307
    Rep Power
    7

    Default

    No it shud be,

    String [] words =new String[];
    Create a string array first and then if you want to store integers in it convert the integer values to string( using ".toString()" or some other function like this) and then store it in above string array.
    To finish sooner, take your own time....
    Nivedithaaaa

  5. #5
    share is offline Member
    Join Date
    Feb 2009
    Posts
    4
    Rep Power
    0

    Default

    import java.util.*;
    import javax.swing.JOptionPane;

    public class CountOccurrenceOfWords {
    public static void main(String[] args) {
    //Prompt the user to enter a string
    String s = JOptionPane.showInputDialog("Enter a string:");

    int[] counts=countWords(s.toLowerCase());

    // Create a hash map to hold words as key and count as value
    Map<String, Integer> hashMap = new HashMap<String, Integer>();

    public static int[] countWords(s.toLowerCase()){
    String[] words = s.split("[:.!? ]");
    for (int i = 0; i < words.length; i++) {
    if (words[i].length() > 1) {
    if (hashMap.get(words[i]) != null) {
    int value = hashMap.get(words[i]).intValue();
    value++;
    hashMap.put(words[i], value);
    }
    else
    hashMap.put(words[i], 1);
    }
    }
    }
    // Create a tree map from the hash map
    Map<String, Integer> treeMap =
    new TreeMap<String, Integer>(hashMap);

    // Display mappings
    System.out.println("Display words and their count in " +
    "ascending order of the words");
    System.out.print(treeMap);
    }


    }

    how i put that method ?
    because i want to covert all the uppercase letters in the string to lowercase

  6. #6
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default

    Share... your prog has a lot of errors... I'll try to point out some of the, but you should do more studying about java basics and methods.
    Java Code:
    public static int[] countWords([B][COLOR="Red"]s.toLowerCase()[/COLOR][/B])
    You can't declare a method's parameter that way. It needs to be an variable with it's type... for example...
    Java Code:
    public static int[] countWords([B][COLOR="Blue"]String myParameter[/COLOR][/B])
    Also...
    Java Code:
    public static [B][COLOR="Blue"]int[][/COLOR][/B] countWords(s.toLowerCase())
    ... your countWords() method indicates that it returns an array, but doesn't have a return statement

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  7. #7
    share is offline Member
    Join Date
    Feb 2009
    Posts
    4
    Rep Power
    0

Similar Threads

  1. What's wrong with this code?
    By Doctor Cactus in forum New To Java
    Replies: 4
    Last Post: 11-29-2008, 05:44 PM
  2. What is wrong with this code
    By rosh72851 in forum New To Java
    Replies: 13
    Last Post: 10-31-2008, 01:50 AM
  3. what's wrong with this code?
    By agenteleven in forum Advanced Java
    Replies: 5
    Last Post: 10-07-2008, 11:26 AM
  4. what is wrong with this code
    By masaka in forum New To Java
    Replies: 5
    Last Post: 04-16-2008, 08:27 AM
  5. What's wrong with this code?
    By Wizard wusa in forum New To Java
    Replies: 14
    Last Post: 01-22-2008, 11:55 PM

Posting Permissions

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