Results 1 to 7 of 7
Thread: what wrong wiith this code?
- 02-25-2009, 08:57 AM #1
Member
- Join Date
- Feb 2009
- Posts
- 4
- Rep Power
- 0
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..
-
for one, a String array can never be assigned an int array.
- 02-25-2009, 09:11 AM #3
Member
- Join Date
- Feb 2009
- Posts
- 4
- Rep Power
- 0
so i should change to what?just delete the int?
- 02-25-2009, 11:04 AM #4
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
- 02-25-2009, 01:12 PM #5
Member
- Join Date
- Feb 2009
- Posts
- 4
- Rep Power
- 0
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
- 02-25-2009, 01:58 PM #6
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.
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="Red"]s.toLowerCase()[/COLOR][/B])
Also...Java Code:public static int[] countWords([B][COLOR="Blue"]String myParameter[/COLOR][/B])
... your countWords() method indicates that it returns an array, but doesn't have a return statementJava Code:public static [B][COLOR="Blue"]int[][/COLOR][/B] countWords(s.toLowerCase())
Luck,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 02-25-2009, 03:58 PM #7
Member
- Join Date
- Feb 2009
- Posts
- 4
- Rep Power
- 0
Similar Threads
-
What's wrong with this code?
By Doctor Cactus in forum New To JavaReplies: 4Last Post: 11-29-2008, 05:44 PM -
What is wrong with this code
By rosh72851 in forum New To JavaReplies: 13Last Post: 10-31-2008, 01:50 AM -
what's wrong with this code?
By agenteleven in forum Advanced JavaReplies: 5Last Post: 10-07-2008, 11:26 AM -
what is wrong with this code
By masaka in forum New To JavaReplies: 5Last Post: 04-16-2008, 08:27 AM -
What's wrong with this code?
By Wizard wusa in forum New To JavaReplies: 14Last Post: 01-22-2008, 11:55 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks