Results 1 to 5 of 5
- 11-12-2007, 03:06 AM #1
Member
- Join Date
- Oct 2007
- Posts
- 11
- Rep Power
- 0
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.
Here is the error message I am getting: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); } }
Am I close or way off base?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$Last edited by toad; 11-12-2007 at 03:06 AM. Reason: typo
- 11-12-2007, 03:08 AM #2
Member
- Join Date
- Oct 2007
- Posts
- 11
- Rep Power
- 0
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.
- 11-12-2007, 06:37 AM #3
Member
- Join Date
- Oct 2007
- Posts
- 21
- Rep Power
- 0
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
- 11-12-2007, 08:19 PM #4
Java cannot find a declaration for the variable "str" - it has not been declared.Java Code:Exercise8_18.java:11: cannot find symbol symbol : variable str
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));
- 07-07-2008, 01:32 PM #5
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
-
Extracting JAR file
By Java Tip in forum Java TipReplies: 0Last Post: 02-08-2008, 09:17 AM -
Extracting embedded files
By kasturi in forum New To JavaReplies: 0Last Post: 02-07-2008, 12:25 PM -
program help: Extracting words from a string
By toad in forum New To JavaReplies: 1Last Post: 11-04-2007, 06:39 PM -
Analyze a string of words
By zoe in forum Advanced JavaReplies: 2Last Post: 07-26-2007, 10:01 AM -
Extracting data from an XML file...
By techno_brains in forum New To JavaReplies: 1Last Post: 07-15-2007, 05:46 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks