Results 1 to 3 of 3
  1. #1
    mitty is offline Member
    Join Date
    Jan 2010
    Rep Power

    Default 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


  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Voorschoten, the Netherlands
    Blog Entries
    Rep Power


    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,


  3. #3
    mitty is offline Member
    Join Date
    Jan 2010
    Rep Power


    Hi Jos, Thanks for your reply.

    I have the following code.

    Java Code:
    import javax.swing.JOptionPane;
    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));
    		  TreebankLanguagePack tlp = new PennTreebankLanguagePack();
    		  GrammaticalStructureFactory gsf = tlp.grammaticalStructureFactory();
    		  GrammaticalStructure gs = gsf.newGrammaticalStructure(parse);
    		  Collection tdl = gs.typedDependenciesCollapsed();
    		    TreePrint tp = new TreePrint("penn,typedDependenciesCollapsed");
    		    System.err.println("Penn format:");
    		    TreePrint treePrint = new TreePrint("penn");
    		    output.close ();
    	if(file.exists()) {
    		  String absolutePathOfFirstFile = file.getAbsolutePath();
    		  System.out.println("Saved to" + absolutePathOfFirstFile);
    		} else {
    		  System.out.println("file does not exist");
    This returns the following

    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].
      (NP (NNP "three",)))
    Penn format:
      (NP (NNP "three",)))
    Saved toC:\Documents and Settings\Mitesh\Desktop\eclipse\stanford-parser-2010-02-26\UserInput.txt
    This is when the user inputs the words one two three.

    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.


Similar Threads

  1. Convert Char Array to String Array
    By Mayur in forum New To Java
    Replies: 8
    Last Post: 10-12-2009, 12:41 PM
  2. string to array
    By Drukqs in forum New To Java
    Replies: 5
    Last Post: 04-22-2009, 06:03 AM
  3. Need Help with String Array
    By crazydeo in forum New To Java
    Replies: 1
    Last Post: 05-13-2008, 10:18 AM
  4. String []Array
    By Warren in forum New To Java
    Replies: 4
    Last Post: 12-01-2007, 09:03 AM
  5. Replies: 1
    Last Post: 08-03-2007, 11:52 AM

Posting Permissions

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