10 Replies - 3694 Views - Last Post: 01 February 2007 - 08:11 PM Rate Topic: -----

#1 JDMvirgin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 31-January 07

Word Count Problem

Posted 31 January 2007 - 07:04 PM

Hey, I am in my first year of CSCI and im starting to learn to program in Java.

Im having some trouble trying to make a program that counts how many times the word chases is inputed by the user into the Console. After they type the word mutiple times on one line the last word will be FINISHED.

Here is what i have so far, but i cant figure out what coding needs to be changed around.

public class WordCount {
static ConsoleInput cin = new ConsoleInput();
public static void main(String[] args) {
int a;
String s1,s2,s3;
s2 = "chases";
s3 = "FINISHED";
a = 0;

s1 = cin.getLine();
while(true){
if(s1.equals(s2))
a+=1;
else if(s1.equals(s3))
break;
}
System.out.println("Occurances= " + a);
}

}

Any help appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: Word Count Problem

#2 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 290
  • View blog
  • Posts: 1,900
  • Joined: 25-October 06

Re: Word Count Problem

Posted 01 February 2007 - 07:56 AM

try this ammended version
import java.util.*;

public class WordCount {
public static void main(String[] args) {
  Scanner ConsoleInput = new Scanner(System.in);
  int a;
  String s1,s2,s3;
  s2 = "chases";
  s3 = "FINISHED";
  a = 0;

  while(true){
	s1 = ConsoleInput.nextLine();
	System.out.println( s1);
	if(s1.equals(s2))
		a+=1;
	 else if(s1.equals(s3))
		  break;
	 }
  System.out.println("Occurances= " + a);
  }
}

Was This Post Helpful? 0
  • +
  • -

#3 JDMvirgin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 31-January 07

Re: Word Count Problem

Posted 01 February 2007 - 09:54 AM

View Posthorace, on 1 Feb, 2007 - 07:56 AM, said:

try this ammended version
import java.util.*;

public class WordCount {
public static void main(String[] args) {
  Scanner ConsoleInput = new Scanner(System.in);
  int a;
  String s1,s2,s3;
  s2 = "chases";
  s3 = "FINISHED";
  a = 0;

  while(true){
	s1 = ConsoleInput.nextLine();
	System.out.println( s1);
	if(s1.equals(s2))
		a+=1;
	 else if(s1.equals(s3))
		  break;
	 }
  System.out.println("Occurances= " + a);
  }
}



This is giving me an Error with nextLine telling me that it is undefined and so it wont let me run it.
Was This Post Helpful? 0
  • +
  • -

#4 born2c0de  Icon User is offline

  • printf("I'm a %XR",195936478);
  • member icon

Reputation: 180
  • View blog
  • Posts: 4,667
  • Joined: 26-November 04

Re: Word Count Problem

Posted 01 February 2007 - 10:11 AM

Try readLine() instead of nextLine()
Was This Post Helpful? 0
  • +
  • -

#5 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 290
  • View blog
  • Posts: 1,900
  • Joined: 25-October 06

Re: Word Count Problem

Posted 01 February 2007 - 10:57 AM

whicj JDK do you have ? - Scanner needs JDK1.5 or later
http://java.sun.com/...il/Scanner.html
Was This Post Helpful? 0
  • +
  • -

#6 JDMvirgin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 31-January 07

Re: Word Count Problem

Posted 01 February 2007 - 03:13 PM

View Posthorace, on 1 Feb, 2007 - 10:57 AM, said:

whicj JDK do you have ? - Scanner needs JDK1.5 or later
http://java.sun.com/...il/Scanner.html


Ok i have the first suggestion working and when I input as the user into the Console

chases chases chases FINISHED

The output just returns me the same data

chases chases chases FINISHED

as shown.

Edit: Ok i removed System.out.println( s1) which fixed the problem with it just showing me what i Typed but if i type
e.g. chases chases FINISHED
It will not output the # of occurances. Only if i type just
e.g. FINISHED
Then it will output the # of occurances which is Occurances= 0

This post has been edited by JDMvirgin: 01 February 2007 - 03:17 PM

Was This Post Helpful? 0
  • +
  • -

#7 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: Word Count Problem

Posted 01 February 2007 - 06:04 PM

The program is currently structured to read one line at a time....you are only typing one line and expecting it to separate the one string into it's component parts. the program will work as expected if you hit enter after every word, or you can take the entire line, and use the string.split() method to split it using the space as a delimiter into an array you can loop through.
Was This Post Helpful? 0
  • +
  • -

#8 JDMvirgin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 31-January 07

Re: Word Count Problem

Posted 01 February 2007 - 07:18 PM

View PostAmadeus, on 1 Feb, 2007 - 06:04 PM, said:

The program is currently structured to read one line at a time....you are only typing one line and expecting it to separate the one string into it's component parts. the program will work as expected if you hit enter after every word, or you can take the entire line, and use the string.split() method to split it using the space as a delimiter into an array you can loop through.



Yes I must have it where the user may type several words per line and it can identify from those.
Was This Post Helpful? 0
  • +
  • -

#9 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: Word Count Problem

Posted 01 February 2007 - 07:23 PM

In that case, I would advise taking the user input as one String object, and then splitting that string into an array. You can then loop through the array and check each element. Something like the following is a very rudimentary version.
import java.util.*;

public class dic1 {
public static void main(String[] args) {
  Scanner ConsoleInput = new Scanner(System.in);
  int a=0;
  String s2,s3;
  s2 = "chases";
  s3 = "FINISHED";
  String s4= new String(ConsoleInput.nextLine());
  String[] s5 = s4.split(" ");
  for(int i=0;i<s5.length;i++)
  {
	 if(s5[i].equals(s2))
		a+=1;
	 if(s5[i].equals(s3))
		break;
  }
  System.out.println("Occurances= " + a);
}
}


Was This Post Helpful? 0
  • +
  • -

#10 JDMvirgin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 31-January 07

Re: Word Count Problem

Posted 01 February 2007 - 08:08 PM

View PostAmadeus, on 1 Feb, 2007 - 07:23 PM, said:

In that case, I would advise taking the user input as one String object, and then splitting that string into an array. You can then loop through the array and check each element. Something like the following is a very rudimentary version.
import java.util.*;

public class dic1 {
public static void main(String[] args) {
  Scanner ConsoleInput = new Scanner(System.in);
  int a=0;
  String s2,s3;
  s2 = "chases";
  s3 = "FINISHED";
  String s4= new String(ConsoleInput.nextLine());
  String[] s5 = s4.split(" ");
  for(int i=0;i<s5.length;i++)
  {
	 if(s5[i].equals(s2))
		a+=1;
	 if(s5[i].equals(s3))
		break;
  }
  System.out.println("Occurances= " + a);
}
}




Oh cool, Thanks alot for your help. I understand what it is doing now. It uses the variable and goes through the whole string and pulls out the indicated words. Thanks Again
Was This Post Helpful? 0
  • +
  • -

#11 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: Word Count Problem

Posted 01 February 2007 - 08:11 PM

No problem at all, although what the code is really doing is the following:

It takes one variable that is made up of several words, each separated by a space. It then splits that single variable into a collection (array) made up of single words. It is then looping through that collection of individual words to identify the ones you want.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1