|
||||||
| Java Help Java Help forum discussing all Java platforms - J2ME, J2SE and J2EE - as well as relevant standards, APIs and frameworks such as Swing, Servlets, JSPs, Applets, Struts, Spring, Hibernate, ANT, EJB, and other Java-related topics. |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
|
|||
|
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("jdbc dbc: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. |
| Sponsored Links |
|
|
|
|||||
|
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...
Code:
\\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("jdbc dbc: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====programing================= ===========good====programing===================== =======go od====programing======================= =====good====programing=========================== =good====programing============================goo d====programing======= =====================good= ===programing============================good====p rograming============================good====progr aming============================good====programin g============================good====programing=== =========================good====programing======= =====================good====programing=======:con fused:=====================good====programing===== =======================good====programing========= ===================good====programing============= ===============good====programing================= ===========good====programing===================== =======good====programing========================= ===good====programin g=========================== =good====programing============================goo d====programing============================good=== =programing============================good====pro graming============================good====program ing============== ==============good====programin g============================good====programing=== =========================good====programing======= =====================good====programing=========== =================good====programing=============== =============good====programing=================== =========good====programing============= ==== ===========good====programing===================== =======good====programing========================= ===good====programing============================g ood====programing============================good= ===programing============================good====p rograming============================good====progr aming============================good====programin g============================good====programing=== =========================good====programin g== =============== |
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Windows XP Tricks & Tips!!!!..new ones. | pranky | Tutorials | 9 | 08-23-2008 04:22 PM |
| text file manipulations in vb6.0 | Ronin_paes | Visual Basic Programming | 3 | 06-11-2007 05:54 AM |
| Linking Text data to deskop | njmase | JavaScript and CSS | 1 | 01-31-2007 07:33 PM |
| Working with TADO Table | MrDiaz | Pascal/Delphi | 0 | 07-03-2006 07:28 PM |
| WingedPanther | ........ | 2753.6 |
| Xav | ........ | 2704 |
| Brandon W | ........ | 1702.32 |
| John | ........ | 1207.73 |
| marwex89 | ........ | 1175.24 |
| morefood2001 | ........ | 966.05 |
| dcs | ........ | 655.75 |
| Steve.L | ........ | 475.59 |
| orjan | ........ | 418.58 |
| Aereshaa | ........ | 383.54 |