Results 1 to 3 of 3
Thread: String to String Array
- 04-07-2010, 06:27 PM #1
Member
- Join Date
- Jan 2010
- Posts
- 16
- Rep Power
- 0
String to String Array
I'm having trouble looking for help on converting a string to a string array.
I have a program which asks for user input, once the user has input their sentence, the program tokenizes the sentences into the following format:
"word1", "word2", "word3", etc
I need to convert this into an array so that I can use a parser to parse the array.
I have looked at some examples but they all have arrays with a pre determined number of items. As the user will be inputting a sentence, the number of words they use can't really be pre-determined.
Does anyone have any links or suggestions I could look at to try resolving this?
Thank you in advance
/Mitty
- 04-07-2010, 06:57 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,601
- Blog Entries
- 7
- Rep Power
- 17
If none of those "words" contain commas you could have a look at the String.split( ... ) method. Read the API documentation for the String class.
kind regards,
Jos
- 04-08-2010, 12:01 AM #3
Member
- Join Date
- Jan 2010
- Posts
- 16
- Rep Power
- 0
Hi Jos, Thanks for your reply.
I have the following code.
This returns the followingJava Code:import javax.swing.JOptionPane; import java.io.*; import java.util.*; import edu.stanford.nlp.trees.*; import edu.stanford.nlp.parser.lexparser.LexicalizedParser; class FileInput{ public static void main(String[] args)throws IOException{ LexicalizedParser lp = new LexicalizedParser("C:/Documents and Settings/Mitesh/Desktop/eclipse/stanford-parser-2010-02-26/bin/englishPCFG.ser.gz"); Writer output = null; String text = JOptionPane.showInputDialog("Please enter your sentence here"); String text2 = null; File file = new File("UserInput.txt"); output = new BufferedWriter(new FileWriter(file)); StringTokenizer st2; st2 = new StringTokenizer (text, " "); while (st2.hasMoreElements()){ // System.out.println(" \" " + st2.nextToken() + " \", "); text2 = ("\"" + st2.nextToken() + "\","); //text2 = {"one", "two", "three" }; System.out.println (text2); output.write (text2); } Tree parse = (Tree) lp.apply(Arrays.asList(text2)); //parse.pennPrint(); System.out.println(); TreebankLanguagePack tlp = new PennTreebankLanguagePack(); GrammaticalStructureFactory gsf = tlp.grammaticalStructureFactory(); GrammaticalStructure gs = gsf.newGrammaticalStructure(parse); Collection tdl = gs.typedDependenciesCollapsed(); System.out.println(tdl); System.out.println(text2); TreePrint tp = new TreePrint("penn,typedDependenciesCollapsed"); tp.printTree(parse); System.err.println("Penn format:"); TreePrint treePrint = new TreePrint("penn"); treePrint.printTree(parse); output.close (); if(file.exists()) { String absolutePathOfFirstFile = file.getAbsolutePath(); System.out.println("Saved to" + absolutePathOfFirstFile); } else { System.out.println("file does not exist"); } } }
This is when the user inputs the words one two three.Java Code:Loading parser from serialized file C:/Documents and Settings/Mitesh/Desktop/eclipse/stanford-parser-2010-02-26/bin/englishPCFG.ser.gz ... done [4.1 sec]. "one", "two", "three", [] "three", (ROOT (NP (NNP "three",))) Penn format: (ROOT (NP (NNP "three",))) Saved toC:\Documents and Settings\Mitesh\Desktop\eclipse\stanford-parser-2010-02-26\UserInput.txt
I'm trying to put the one two three into an Array as another class of the program requires the words to be in an array to parse them. At the moment, the only word which is being parsed is the last word as this is the final word in the array. It is not saving them in an array but is replacing the word each time until the final word.
Mitty
Similar Threads
-
Convert Char Array to String Array
By Mayur in forum New To JavaReplies: 8Last Post: 10-12-2009, 11:41 AM -
string to array
By Drukqs in forum New To JavaReplies: 5Last Post: 04-22-2009, 05:03 AM -
Need Help with String Array
By crazydeo in forum New To JavaReplies: 1Last Post: 05-13-2008, 09:18 AM -
String []Array
By Warren in forum New To JavaReplies: 4Last Post: 12-01-2007, 08:03 AM -
I can't seem to pass the value of a string variable into a string array
By mathias in forum Java AppletsReplies: 1Last Post: 08-03-2007, 10:52 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks