Results 1 to 20 of 20
- 02-26-2009, 02:46 PM #1
Member
- Join Date
- Feb 2009
- Posts
- 19
- Rep Power
- 0
- 02-26-2009, 03:00 PM #2
Not sure if I understnd your question, but one way is that you could use the string split() method to separte the words in an array and then cycle through the array looking (by comparing) for the string ocurrences.
String API link:
String (Java Platform SE 6)
Luck,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 02-26-2009, 04:14 PM #3
Member
- Join Date
- Feb 2009
- Posts
- 19
- Rep Power
- 0
can i do like this...it seems no errors but no counting occur at all...why?i dont know how to compare the words inside a line between each other...
import java.util.*;
public class Question3{
public static void main(String[]args){
Scanner scan=new Scanner(System.in);
System.out.println("Enter a string:");
String s=scan.nextLine();
String[] words = s.split("[ ':.!?]");
String a=words[0];
int count=0;
for (int i=1;i<words.length;i++){
if (words[i].equals(a)){
count++;
a= words[i];
}
}
for (int i=1;i<words.length;i++){
System.out.print(words[i]+" "+count);
System.out.println();
}
}
}
- 02-26-2009, 06:05 PM #4
A for within a for ...
I think what you need is two loops... the first loop (outer) would cycle through the words in the array and the second (inner) loop would compare the words from the first loop to all the words in the array. I think it would be something like...
I would also to rename some of your variables... for example change variable "a" to "word", "words" to "wordArray", etc. give some meaning to the variables name.Java Code:String word = ""; for (int i=0;i<words.length;i++){ word = words[i]; for (int j=0;i<words.length;j++){ if (words[j+1].equals(word)){ . . . } }
Luck,
CJSLLast edited by CJSLMAN; 02-26-2009 at 06:05 PM. Reason: correct a typo
Chris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 02-26-2009, 10:23 PM #5
Member
- Join Date
- Feb 2009
- Location
- Romania
- Posts
- 11
- Rep Power
- 0
- 02-26-2009, 11:21 PM #6
huh?
Sorry JVPoster, but countMatches() is not a Java API method. It maybe a third party API, but it is not part of the Java API specification (at least I couldn't find it).
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 02-27-2009, 12:01 AM #7
OK... so I'm curious about JVp's comment about using "StringUtils.countMatches()" and I decided to see where is it used. I opened Google and typed "java countmatch" and the first match that showed up was this:
String count matches : String Utils*«*Apache Common*«*Java
and guess what? it's extacly the same example that JVp posted. Now, to be able to use that method, you have to have had the following import statement:
So... JVp's suggestion is worthless and he just posted the first thing that he found on the web that looked similiar... and from his other posts (equally worthless), he's either a troll or trying to bump his post count up to 20.Java Code:import org.apache.commons.lang.StringUtils;
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 02-27-2009, 01:34 AM #8
Member
- Join Date
- Feb 2009
- Posts
- 19
- Rep Power
- 0
Enter a string:
how are you
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
at Question3.main(Question3.java:24)
Press any key to continue . . .
how to initialise the count array?
import java.util.*;
public class Question3{
public static void main(String[]args){
Scanner scan=new Scanner(System.in);
System.out.println("Enter a string:");
String s=scan.nextLine();
String[] words = s.split("[ ':.!?]");
String word = "";
int [] count=new int[100];
for (int i=0;i<words.length;i++){
word = words[i];
for (int j=0;j<words.length;j++){
if (words[j+1].equals(word)){
count[j+1]++;
System.out.println(words[i]+" "+count[j+1]);
}
}
}
}
}
- 02-27-2009, 04:20 AM #9
ooppss... sorry
OK, I had an error... the array was running out of bounds becasue of the [j+1]... simple to fix...
and change all [j+1] to [J]Java Code:for (int j=1;j<words.length;j++){
Luck,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 02-27-2009, 04:37 AM #10
Member
- Join Date
- Feb 2009
- Posts
- 19
- Rep Power
- 0
Enter a string:
how do you do
how 0
do 2
you 1
do 2
Press any key to continue . . .
why the word "do" print 2 times?and the word "how" not counting...
import java.util.*;
public class Question3{
public static void main(String[]args){
Scanner scan=new Scanner(System.in);
System.out.println("Enter a string:");
String s=scan.nextLine();
String[] words = s.split("[ ':.!?]");
String word = "";
int [] count=new int[100];
for (int i=0;i<words.length;i++){
word = words[i];
for (int j=1;j<words.length;j++){
if (words[j].equals(word))
count[j]++;
}
}
for (int x=0;x<words.length;x++){
System.out.println(words[x]+"\t"+count[x]);
}
}
}
- 02-27-2009, 06:05 AM #11
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
- 02-27-2009, 06:08 AM #12
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Thinking about this, if you really don't want "do" to be counted and reported twice then a map of some sort really is the easiest way to go.
- 02-27-2009, 06:22 AM #13
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Or something like this in the loop that does the counting:
That way only the last of the counts for a given word will be nonzero.Java Code:FOR j=0 j<words.length j++ IF words[j].equals(word) increment count[i] (not j) IF i!=j make count[j]=0
- 02-27-2009, 07:10 AM #14
Member
- Join Date
- Feb 2009
- Posts
- 19
- Rep Power
- 0
is it like this?i dont understand what u mean...sorry...still got errors although can run
import java.util.*;
public class Question3{
public static void main(String[]args){
Scanner scan=new Scanner(System.in);
System.out.println("Enter a string:");
String s=scan.nextLine();
String[] words = s.split("[ ':.!?]");
String word = "";
int [] count=new int[20];
for (int i=0;i<words.length;i++){
word = words[i].toLowerCase();
for (int j=0;j<words.length;j++){
if (words[j].equals(word)){
count[i]++;
}
if (i != j){
count[j]=0;
}
}
}
for (int y=0;y<words.length;y++){
System.out.println(words[y]+"\t"+count[y]);
}
}
}
- 02-27-2009, 07:41 AM #15
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
I have no idea what the errors might be unless you say.
You have misplaced the if statement within the second for loop. To see how to correct it, think for a bit about what the algorithm is trying to do.
Your original version updated the count every time it found two words the same. What I am suggesting is that it continues to do this - but that it should also "reset" the counts for all the other equal words.
For instance when you count the occurrences in "How do you do" the counting would go like (each row represents one time round the outer for loop)
The idea is that when you are counting all the words that are equal to the second "do" you reset the count for the first one back to zero.Java Code:How do you do 1 0 0 0 How do you do 1 2 0 0 How do you do 1 2 1 0 How do you do 1 0 1 2
Then when you output the counts you can just ignore words with a zero count. (You are not doing this at the moment. You will have to change the second for loop so that the first occurrence of "do" - the one with count 0 - is not printed.)
One small point - you should decide whether your string comparisons should be case sensitive or not and then stick to that. At the moment you are initialising word as the lower case version of the word in your original string, but then comparing it to the original within the inner for loop.
- 02-27-2009, 10:32 AM #16
Member
- Join Date
- Feb 2009
- Posts
- 19
- Rep Power
- 0
That's all i can do...but the print out got problems...i dont know how to avoid to print words that already printed...the 'do' still print 2 times...:(
Enter a string:
how do you do
how 1
do 2
you 1
do 1
Press any key to continue . . .
import java.util.*;
public class Question3{
public static void main(String[]args){
Scanner scan=new Scanner(System.in);
System.out.println("Enter a string:");
String s=scan.nextLine();
String[] words = s.split("[ ':.!?]");
String word = "";
int [] count=new int[20];
for (int i=0;i<words.length;i++){
word = words[i].toLowerCase();
for (int j=1;j<words.length;j++){
if (i != j){
if (words[j].equals(word)){
count[i]++;
}
}
if (i == j){
if (words[j].equals(word)){
count[i]=1;
}
}
if (!words[j].equals(words[0])){
count[0]=1;
}
}
}
for (int y=0;y<words.length;y++){
System.out.println(words[y]+"\t"+count[y]);
}
}
}
- 02-27-2009, 05:37 PM #17
Member
- Join Date
- Feb 2009
- Posts
- 19
- Rep Power
- 0
Help!
can somebody help me?reply me...:(
- 02-28-2009, 12:07 AM #18
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
When you post code, highlight it and use the code button (lebelled #).
I've given up and put the if statement where it should be, corrected the string comparison and changed the error reintroduced into the inner loop (j=0;...). But I've left you the output loop!Java Code:import java.util.*; public class Question3 { public static void main(String[]args){ Scanner scan=new Scanner(System.in); System.out.println("Enter a string:"); String s=scan.nextLine(); String[] words = s.split("[ ':.!?]"); String word = ""; int [] count=new int[20]; for (int i=0;i<words.length;i++){ word = words[i].toLowerCase(); for (int j=0;j<words.length;j++){ // first increment i's count for every // equal word if (words[j].toLowerCase().equals(word)){ count[i]++; // then make sure the other instances of this // word get a zero count if (i != j){ count[j] = 0; } } } } // correct this loop so that it doesn't // print the zero counts for (int y=0;y<words.length;y++){ System.out.println(words[y]+"\t"+count[y]); } } }
Once you understand what is happening you might try to make it more efficient. (Would it help to change the words array so that it contained only lower case strings? Do we really need to check every word with every other - or is it enough to check every word with all those with a lower index?)
- 02-28-2009, 07:05 AM #19
Member
- Join Date
- Feb 2009
- Posts
- 19
- Rep Power
- 0
i correct it already...it can be run now...thank you...but can u tell me how to sort the words alphabetically when printed out?
- 02-28-2009, 07:37 AM #20
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
(Just an aside: using a Map - which your OP said you didn't want to do - makes this trivial. A TreeMap for instance will store the counts for each word so that they can be accessed alphabetically without any extra code at all.)can u tell me how to sort the words alphabetically when printed out?
The alternative is to write a sorting algorithm that sorts both arrays alphabetically on the words array.
Similar Threads
-
count character in text file as input file
By aNNuur in forum New To JavaReplies: 7Last Post: 03-25-2010, 04:01 PM -
Make a text in Jlabel down to next Line
By hungleon88 in forum AWT / SwingReplies: 2Last Post: 12-01-2008, 11:10 PM -
Search a word(taken from one file) in another file and give the line as the output
By SwapnaNaidu in forum New To JavaReplies: 7Last Post: 11-19-2008, 02:09 PM -
saving to a new line a text
By jadaleus in forum Advanced JavaReplies: 1Last Post: 10-20-2008, 06:10 PM -
[SOLVED] what is text limit line?
By Nicholas Jordan in forum NetBeansReplies: 12Last Post: 07-16-2008, 05:05 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks