CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: StringTokenizer

  1. #1
    Join Date
    Apr 2012
    Posts
    6

    StringTokenizer

    If we type in "this is a test" into the code below:

    StringTokenizer st = new StringTokenizer("this is a test");
    while (st.hasMoreTokens()) {
    System.out.println(st.nextToken());
    }

    ..we'll get the output as below:

    this
    is
    a
    test

    My question is how do I read my input from text file to split them instead of type it in as above?

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: StringTokenizer

    Have you tried googling for something like "java read from file"
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  3. #3
    Join Date
    Apr 2012
    Posts
    6

    Re: StringTokenizer

    yes I did but I can't find any way to tokenize from text file.

  4. #4
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: StringTokenizer

    All you need to find out is how to read in a file as lines of text, you've already got the code for splitting it.

    BTW StringTokenizer is no longer the recommended way of splitting text, you should use the String classes split method instead.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  5. #5
    Join Date
    Apr 2012
    Posts
    6

    Re: StringTokenizer

    I already managed to split my text file data. Now I only need to insert them into my database which contains 2 fields - rootWordA & posWordA.

    As example my text file is like below:

    abang kn
    aksen kn
    aksep kn

    output after tokenize:

    abang <-- gonna put into "rootWordA" field
    kn <-- gonna put into "posWordA" field
    aksen
    kn
    aksep
    kn

    How do I assigned the 2 variables to insert the data into those fields?
    I put my code here as your reference. Thanks if you could help.

    Code:
    import java.io.IOException;
    import java.sql.*;
    import java.util.StringTokenizer;
    public class Dictionary {
        public static void main(String[ ] args){
            try{
            Connection con=null;
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            String url = "jdbc:odbc:Driver={Microsoft Access Driver " + "(*.mdb, *.accdb)};DBQ=C:\\Users\\King\\My Documents\\ITKING.accdb";
            con=DriverManager.getConnection(url);
            Statement stmt=con.createStatement();
    
            String file_name = "C:/Users/King/Desktop/Table A2.txt";
    
            
                ReadFile file = new ReadFile( file_name );
                String[ ] aryLines = file.OpenFile( );
    
                int i;
                for ( i=0; i < aryLines.length; i++ ) {
                            
                StringTokenizer st = new StringTokenizer(aryLines[ i ]);
                while (st.hasMoreTokens()) {
                System.out.println(st.nextToken());
                }
              }
    
                con.close();
            }
    
            catch ( IOException e ) {
                System.out.println( e.getMessage() );
            }
    
            catch (SQLException e) {
                System.out.println("SQL Exception: "+ e.toString());
            }
    
            catch (ClassNotFoundException cE) {
                System.out.println("Class Not Found Exception: "+
                    cE.toString());
            }
        }
    }

  6. #6
    Join Date
    May 2012
    Location
    Bonn, Germany
    Posts
    43

    Re: StringTokenizer

    Do it with SQL.

    Code:
    stmt.executeUpdate("INSERT INTO MyTable (rootWordA, posWordA) VALUES ('" + firstWord + "', '" + secondWord + "')");
    Change "MyTable" to the name of your table.

  7. #7
    Join Date
    Apr 2012
    Posts
    6

    Re: StringTokenizer

    Oopps forgot to mention that I'm using MS Access. I did like following code but I got this output with error:

    abang
    kn
    Exception in thread "main" java.util.NoSuchElementException
    at java.util.StringTokenizer.nextToken(StringTokenizer.java:332)
    at textfiles.Dictionary.main(Dictionary.java:41)


    What's wrong actually?

    Code:
    package textfiles;
    import java.io.IOException;
    import java.sql.*;
    import java.util.StringTokenizer;
    public class Dictionary {
        public static void main(String[ ] args){
            try{
            Connection con=null;
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            String url = "jdbc:odbc:Driver={Microsoft Access Driver " +
                "(*.mdb, *.accdb)};DBQ=C:\\Users\\King\\My Documents\\ITKING.accdb";
            con=DriverManager.getConnection(url);
            Statement stmt=con.createStatement();
    
            String file_name = "C:/Users/King/Desktop/Table A2.txt";
    
            
                ReadFile file = new ReadFile( file_name );
                String[ ] aryLines = file.OpenFile( );
    
                int i;
                String word="";
                String pos="";
                for ( i=0; i < aryLines.length; i++ ) {
                            
                StringTokenizer st = new StringTokenizer(aryLines[ i ]);
                while (st.hasMoreTokens()) {
                System.out.println(st.nextToken());
                }
                
                word = st.nextToken();
                pos = st.nextToken();
                String SQLInsert = "INSERT INTO TableA (rootWordA,posWordA) VALUES (word, pos)";
                int rowsEffected = stmt.executeUpdate(SQLInsert);
                System.out.println(rowsEffected + " rows effected");
                }
    
                con.close();
            }
    
            catch ( IOException e ) {
                System.out.println( e.getMessage() );
            }
    
            catch (SQLException e) {
                System.out.println("SQL Exception: "+ e.toString());
            }
    
            catch (ClassNotFoundException cE) {
                System.out.println("Class Not Found Exception: "+
                    cE.toString());
            }
        }
    }

  8. #8
    Join Date
    May 2012
    Location
    Bonn, Germany
    Posts
    43

    Re: StringTokenizer

    You just tried to insert "word" and "pos" into your table instead of the variables' contents.
    And additionally, SQL requires single quotes around strings: 'word', 'pos'.
    So if you actually want to insert the contents of the variables "word" and "pos", you need to do it as I showed you before, with the + operator to concatenate strings.
    Last edited by TomasRiker; May 18th, 2012 at 03:39 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
  •  





Click Here to Expand Forum to Full Width

Featured