I m a bit confused with this program.I am getting error with StringTokenizer method nextToken(); ,but unable to solve this error.![]()
In a text file Records i have saved the data in the format shown below
/First/Sania/Last/Arora....etc
My java program is as below
public class ReadingText {
public static final String Records="Records";
public static void main(String[] args) throws NoSuchElementException {
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn=DriverManager.getConnection("jdbcdbc:sap11","sa","");
Statement stmt=conn.createStatement();
BufferedReader in=new BufferedReader(new FileReader("E:\\Records.txt"));
String line=in.readLine();
while(line!=null){
StringTokenizer tk=new StringTokenizer(line);
String First=tk.nextToken("/"),Last=tk.nextToken("/"),Email=tk.nextToken("/"),Phone=tk.nextToken("/");
String query="insert into "+ Records;
query+="values(" + quote(First)+ "," ;
query+=quote(Last)+ ",";
query+=quote(Email)+ ",";
query+=quote(Phone)+ "); ";
stmt.executeQuery(query);
line=in.readLine();
}
in.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
private static String quote(String include) {
return("\"" + include + "\"");
}
}
the error does not explain anything it just gets pointed when i try to run.
Please if any one can explain what is the error in this program why its not giving desired output.it will be realy kinda of you .Thanks in advance.
Um, I personally use a different method, using a custom java library thingy which if u want im pretty sure i could give to you. However, I think this should work...
Let me know how it goesCode:\\Create BufferedReader called reader and give it the filename BufferedReader reader = new BufferedReader("FileNameHere.txt"); \\Read data temporarily assigned to variable "data" String data = reader.readLine(); while (data != null) { \\If data is split up by a symbol ie tab, then insert symbol here. ("\t"=tab) \\Each "bits" is assigned to the appropriate String STARTING AT 0. String[] bits = data.split("\t"); String registration = bits[0]; String type = bits[1]; String costs = bits[2]; \\Then procceeds to read next line in file data = reader.readLine(); } \\Once all done, closes reader reader.close(); \\Prints to terminal "Finished", just to let u know. It is optional. System.out.println("Reader has finished"); }
Domestic
PS: I havent tested it - my proper PC is on the fritz and under repairs.....(its in pieces on my floor)
.
Programming Languages: Java, VB6, VB2005 (.NET2)
Web Languages: HTML, CSS, JS
Website: http://abdn.ac.uk/~u41am6
Opportunity is missed by most people because it is dressed in overalls and looks like work.
Thank u for replying .
I modified my program by using the split method but still i get a error.
The error does not explain anything it just gets pointed.Please refer to the below program.
public class ReadingText {
public static final String Records="Records";
public static void main(String[] args) throws NoSuchElementException {
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn=DriverManager.getConnection("jdbcdbc:sania","sa","");
BufferedReader in=new BufferedReader(new FileReader("E:\\Records.txt"));
String line=in.readLine();
while(line!=null){
String[] bits = line.split("\t");
String First = bits[0];
*(Error at this line)* String Last = bits[1];
String Email = bits[2];
String Phone = bits[3];
System.out.println("ok");
String query="insert into Records(First,Last,Email,Phone)values("+quote(Firs t)+","+quote(Last)+","+quote(Email)+" , "+quote(Phone)+" )";
PreparedStatement pstmt=conn.prepareStatement(query);
pstmt.executeQuery(query);
line = in.readLine();
}
in.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
private static String quote(String include) {
return("\"" + include + "\"");
}
}
one more doubt to be asked is in whta exact format should i save the data in my txt file?
The format which i use is as below.
/First/sonu/Last/Marthur/Email/sonumarthar/Phone/264567
I am unable to track the error please can anyone explain.
Thanks in advance.
===========good====programing===================== =======good===
=programing============================good====pro graming============================good====program ing============================go
od====programing============================good== ==programing============================good====pr ograming============================good====progra ming=======
=====================good====programing=========== =================good====programing=============== =============good====programing=================== =========good====programing======================= =====good====programing=========================== =good====programing============================goo d====programing=======
=====================good====programing=========== =================good====programing=============== =============good====programing=================== =========good====programing======================= =====good====programing=========================== =good====programing============================goo d====programin
g============================good====programing=== =========================good====programing======= =====================good====programing=========== =================good====programing=============== =============good====programing==============
==============good====programing================== ==========good====programing====================== ======good====programing========================== ==good====programing============================go od====programing============================good== ==programing============================good====pr ograming=============
===============good====programing================= ===========good====programing===================== =======good====programing========================= ===good====programing============================g ood====programing============================good= ===programing============================good====p rograming============================good====progr aming============================good====programin g============================good====programin
g=================
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks