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

    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

    /Mitty

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,363
    Blog Entries
    7
    Rep Power
    20

    Default

    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

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

    Default

    Hi Jos, Thanks for your reply.

    I have the following code.

    Java 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 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].
    "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
    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.

    Mitty

Similar Threads

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