Results 1 to 5 of 5
  1. #1
    toad is offline Member
    Join Date
    Oct 2007
    Posts
    11
    Rep Power
    0

    Default Extracting words from a string using delimiters

    I need to write a program that extracts words from a string using spaces and punctuation marks as delimiters. Enter the string from an input dialog box.

    Here is my code so far.
    Java Code:
    import java.util.*;
    import javax.swing.JOptionPane;
    
    public class Exercise8_18 {
        /**Main method*/
            public static void main(String[] args) {
            // Receive text entered from the dialog box
            String s = JOptionPane.showInputDialog(
            "Enter a sentence using punctuation:");
            String delims = "[\\s\\p{Punct}]+";
            String[] tokens = str.split(delims);  
                    
            System.out.println("The extracted words are:");
            
                    System.exit(0);
        }
    }
    Here is the error message I am getting:
    Java Code:
    Exercise8_18.java:11: cannot find symbol
    symbol  : variable str
    location: class Exercise8_18
            String[] tokens = str.split(delims);  
                              ^
    1 error
    sprouts-computer:~/Desktop sprout$
    Am I close or way off base?
    Last edited by toad; 11-12-2007 at 03:06 AM. Reason: typo

  2. #2
    toad is offline Member
    Join Date
    Oct 2007
    Posts
    11
    Rep Power
    0

    Default

    I think I need to use the variable string, but I haven't defined it yet. I'm pretty sure I should substitute it with the String that was entered into the dialog but I do not know how to do this.

    Any help is greatly appreciated.

  3. #3
    unhurt is offline Member
    Join Date
    Oct 2007
    Posts
    21
    Rep Power
    0

    Default

    hmm i don't know about this but i would do it the hard way which is charAt() and then replace lol... but anyway don't bother about my way its kinda long way but if not mistaken there is another thread on this i think u should check it out

  4. #4
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Java Code:
    Exercise8_18.java:11: cannot find symbol
    symbol  : variable str
    Java cannot find a declaration for the variable "str" - it has not been declared.
    You did however declare a string "s" and you saved the user input in it.
    Java Code:
    public class Exercise8_18 {
        /**Main method*/
        public static void main(String[] args) {
            // Receive text entered from the dialog box
            // Save it in local variable "s".
            String s = JOptionPane.showInputDialog("Enter a sentence " +
                                                   "using punctuation:");
            String delims = "[\\s\\p{Punct}]+";
            // Split the String "s":
            String[] tokens = s.split(delims);  
                    
            System.out.println("The extracted words are: " +
                                java.util.Arrays.toString(tokens));

  5. #5
    baskar.nitt's Avatar
    baskar.nitt is offline Member
    Join Date
    Apr 2008
    Location
    Chennai, India
    Posts
    19
    Rep Power
    0

    Default

    Extracting words using StringTokenizer: (try this out)

    Java Code:
    Stirng s = JOptionPane.showInputDialog("Enter a sentence using punctuation:");
    
    	String delims = ",. ";
    
    StringTokenizer st = new StringTokenizer(s, delims);
    		System.out.println("No of Token = " + st.countTokens());
    
    		 while (st.hasMoreTokens())
    		 {
    			 System.out.println(st.nextToken());
    		 }

Similar Threads

  1. Extracting JAR file
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-08-2008, 09:17 AM
  2. Extracting embedded files
    By kasturi in forum New To Java
    Replies: 0
    Last Post: 02-07-2008, 12:25 PM
  3. program help: Extracting words from a string
    By toad in forum New To Java
    Replies: 1
    Last Post: 11-04-2007, 06:39 PM
  4. Analyze a string of words
    By zoe in forum Advanced Java
    Replies: 2
    Last Post: 07-26-2007, 10:01 AM
  5. Extracting data from an XML file...
    By techno_brains in forum New To Java
    Replies: 1
    Last Post: 07-15-2007, 05:46 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •