Need help with a program

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • erekose666
    New Member
    • Oct 2006
    • 23

    #1

    Need help with a program

    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! :)
  • erekose666
    New Member
    • Oct 2006
    • 23

    #2
    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.prin tln( "Enter a sentence and press Enter" );
    String sentence = scanner.nextLin e();

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

    while ( tokens.hasMoreT okens() )
    System.out.prin tln( tokens.nextToke n( ) );
    } // end main
    } // end class TokenTest

    Comment

    • erekose666
      New Member
      • Oct 2006
      • 23

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

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        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(" ");

        Comment

        • erekose666
          New Member
          • Oct 2006
          • 23

          #5
          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?

          Comment

          • erekose666
            New Member
            • Oct 2006
            • 23

            #6
            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.

            Comment

            • r035198x
              MVP
              • Sep 2006
              • 13225

              #7
              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.
              Code:
              //You misunderstood my post
              import java.io.*;
              import java.util.*;
              public class TokenTest {
              
              	public static void main( String args[] ) {
              
              
              		Scanner scanner = new Scanner( System.in );
              		System.out.println( "Enter a sentence and press Enter" );
              		String sentence = scanner.nextLine();
              		int index = 0;
              
              		String [] tokens = sentence.split(" ");
              		String [] bstart = new String[tokens.length];
              
              		System.out.println( "The tokens starting with b are: ");
              		for(int i = 0; i < tokens.length; i++) {
              			if(tokens[i].charAt(0) == 'b')
              				bstart[index++] = tokens[i];
              		}
              
              		/*
              		OR
              
              		StringTokenizer st = new StringTokenizer(sentence);
              		while(st.hasMoreTokens()) {
              			String s = st.nextToken();
              			if(s.charAt(0) == 'b') {
              				bstart[index++] = s;
              			}
              
              		*/
              
              		for(int i = 0; i < bstart.length; i++) {
              			System.out.println(bstart[i]);
              		}
              		//The problem is the null values appended to fill in the array
              	}
              }

              Comment

              • erekose666
                New Member
                • Oct 2006
                • 23

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

                Comment

                • r035198x
                  MVP
                  • Sep 2006
                  • 13225

                  #9
                  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.

                  Comment

                  Working...