Connecting Tech Pros Worldwide Forums | Help | Site Map

Need help with a program

Newbie
 
Join Date: Oct 2006
Posts: 23
#1: Oct 23 '06
I am totally lost here. I need a program to do the following:

write an application that reads a line of text, tokenizes the line using space characters as delimiters and outputs only those words beginning with the letter "b"

I know how to count tokens and whatnot but how the heck do i print out only toeksn beginning with a certain char?

HELP! :)

Newbie
 
Join Date: Oct 2006
Posts: 23
#2: Oct 23 '06

re: Need help with a program


This is the code I have thus far btw:

import java.io.*;
import java.util.*;


public class TokenTest
{
// execute application
public static void main( String args[] )
{
// get sentence
Scanner scanner = new Scanner( System.in );
System.out.println( "Enter a sentence and press Enter" );
String sentence = scanner.nextLine();

// process user sentence
StringTokenizer tokens = new StringTokenizer( sentence);
System.out.printf( "Number of elements: %d\nThe tokens are:\n",
tokens.countTokens() );

while ( tokens.hasMoreTokens() )
System.out.println( tokens.nextToken( ) );
} // end main
} // end class TokenTest
Newbie
 
Join Date: Oct 2006
Posts: 23
#3: Oct 23 '06

re: Need help with a program


I am assuming you have to do something with the charAt function, but I have no idea how to implement it within the program.
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#4: Oct 23 '06

re: Need help with a program


Quote:

Originally Posted by erekose666

I am assuming you have to do something with the charAt function, but I have no idea how to implement it within the program.

for any string s, s.charAt(0) will return the first character of that String

eg String s = "bless";
s.charAt(0) == 'b' will return true

you could also tokenize the string without using the StringTokenizer

String [] tokens = s.split(" ");
Newbie
 
Join Date: Oct 2006
Posts: 23
#5: Oct 23 '06

re: Need help with a program


OK so let's assume I'm going to use StringTokenizer. How would I fit this into the program as I have coded it thus far?
Newbie
 
Join Date: Oct 2006
Posts: 23
#6: Oct 23 '06

re: Need help with a program


Let me rephrase....I need to return any token that starts with the letter "b", not just the letter "b" itself. :) So from what I'm seeing that you're saying, charAt isn't going to help me at all. Now I'm even more confused than ever lol.
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#7: Oct 23 '06

re: Need help with a program


Quote:

Originally Posted by erekose666

Let me rephrase....I need to return any token that starts with the letter "b", not just the letter "b" itself. :) So from what I'm seeing that you're saying, charAt isn't going to help me at all. Now I'm even more confused than ever lol.

Expand|Select|Wrap|Line Numbers
  1. //You misunderstood my post
  2. import java.io.*;
  3. import java.util.*;
  4. public class TokenTest {
  5.  
  6.     public static void main( String args[] ) {
  7.  
  8.  
  9.         Scanner scanner = new Scanner( System.in );
  10.         System.out.println( "Enter a sentence and press Enter" );
  11.         String sentence = scanner.nextLine();
  12.         int index = 0;
  13.  
  14.         String [] tokens = sentence.split(" ");
  15.         String [] bstart = new String[tokens.length];
  16.  
  17.         System.out.println( "The tokens starting with b are: ");
  18.         for(int i = 0; i < tokens.length; i++) {
  19.             if(tokens[i].charAt(0) == 'b')
  20.                 bstart[index++] = tokens[i];
  21.         }
  22.  
  23.         /*
  24.         OR
  25.  
  26.         StringTokenizer st = new StringTokenizer(sentence);
  27.         while(st.hasMoreTokens()) {
  28.             String s = st.nextToken();
  29.             if(s.charAt(0) == 'b') {
  30.                 bstart[index++] = s;
  31.             }
  32.  
  33.         */
  34.  
  35.         for(int i = 0; i < bstart.length; i++) {
  36.             System.out.println(bstart[i]);
  37.         }
  38.         //The problem is the null values appended to fill in the array
  39.     }
  40. }
Newbie
 
Join Date: Oct 2006
Posts: 23
#8: Oct 23 '06

re: Need help with a program


OK sorry I did indeed misunderstand :)
That definitely helps me out a lot :)
Thanks so much!
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#9: Oct 23 '06

re: Need help with a program


Quote:

Originally Posted by erekose666

OK sorry I did indeed misunderstand :)
That definitely helps me out a lot :)
Thanks so much!

If you learn a bit of your regular expressions, you can split the string so that you get only tokens that start with a b.
Reply