-
May 14th, 2012, 02:34 AM
#1
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?
-
May 14th, 2012, 06:59 AM
#2
Re: StringTokenizer
Have you tried googling for something like "java read from file"
-
May 14th, 2012, 09:46 PM
#3
Re: StringTokenizer
yes I did but I can't find any way to tokenize from text file.
-
May 15th, 2012, 02:04 AM
#4
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.
-
May 17th, 2012, 09:59 PM
#5
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());
}
}
}
-
May 18th, 2012, 03:02 AM
#6
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.
-
May 18th, 2012, 03:23 AM
#7
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());
}
}
}
-
May 18th, 2012, 03:28 AM
#8
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|