The Artima Developer Community
Sponsored Link

Java Answers Forum
count the number of tokens in the first line

2 replies on 1 page. Most recent reply: Jan 6, 2003 4:39 PM by Matt Gerrans

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
ben

Posts: 57
Nickname: loftty
Registered: Oct, 2002

count the number of tokens in the first line Posted: Jan 6, 2003 7:09 AM
Reply to this message Reply
Advertisement
Hello everyone i have this code which is below but i am stuck with how i can printout how many tokens are in the first line
could anyone give me some ideas please
thanks

code:
----------------------------------------------------------------------- ---------

import java.io.*;import java.util.*;import java.lang.*;import java.util.StringTokenizer.*;import java.sql.*; public class RT3 { public static void main(String[] args) { String token = null; String currentToken = ""; Hashtable hh = new Hashtable(); try { Vector v2 = new Vector(100); Vector v = new Vector(100); FileReader file = new FileReader("Work.csv"); BufferedReader buff = new BufferedReader(file); String line = buff.readLine(); StringTokenizer s = null; boolean eof = false; while (!eof){ s = new StringTokenizer(line.trim(),",",true); boolean lastToken = true; while (s.hasMoreTokens()) { token = s.nextToken(); if (lastToken){ if (token.equals(",")) { v.add(""); } lastToken = false; } if (! token.equals(",")) { v.add(token); } else { if (currentToken.equals(",")) { v.add(""); } } currentToken = token; } if (token.equals(",")) { v.add(""); } line = buff.readLine(); if (line == null){ eof = true;} } buff.close(); }catch (FileNotFoundException fe) { System.out.println("Error - - " + fe.toString()); }catch (NumberFormatException ne) { System.out.println("Error - - " + ne.toString()); } catch (IOException e) { System.out.println("Error - - " + e.toString()); } }}

----------------------------------------------------------------------------- ---


ben

Posts: 57
Nickname: loftty
Registered: Oct, 2002

Re: count the number of tokens in the first line Posted: Jan 6, 2003 7:11 AM
Reply to this message Reply
sorry do not know why it came out like that
import java.io.*;
import java.util.*;
import java.lang.*;
import java.util.StringTokenizer.*;
import java.sql.*;

public class RT3 {

public static void main(String[] args) {

String token = null;
String currentToken = "";
Hashtable hh = new Hashtable();

try {
Vector v2 = new Vector(100);
Vector v = new Vector(100);
FileReader file = new FileReader("Work.csv");
BufferedReader buff = new BufferedReader(file);
String line = buff.readLine();
StringTokenizer s = null;
boolean eof = false;


while (!eof){
s = new StringTokenizer(line.trim(),",",true);
boolean lastToken = true;


while (s.hasMoreTokens()) {


token = s.nextToken();
if (lastToken){
if (token.equals(",")) {
v.add("");
}
lastToken = false;
}
if (! token.equals(",")) {
v.add(token);
}
else { if (currentToken.equals(",")) {
v.add("");

}
}
currentToken = token;


}
if (token.equals(",")) {
v.add("");
}


line = buff.readLine();
if (line == null){
eof = true;}
}


buff.close();
}catch (FileNotFoundException fe) {
System.out.println("Error - - " + fe.toString());
}catch (NumberFormatException ne) {
System.out.println("Error - - " + ne.toString());
} catch (IOException e) {
System.out.println("Error - - " + e.toString());



}
}
}

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: count the number of tokens in the first line Posted: Jan 6, 2003 4:39 PM
Reply to this message Reply
Try using the [java] tags around your code in this forum, then it will come out with the indentation intact, as well as some color syntaxing.

Flat View: This topic has 2 replies on 1 page
Topic: writing into ms access table using java Previous Topic   Next Topic Topic: Google Api: A lame question

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use