The Artima Developer Community
Sponsored Link

Java Answers Forum
help with reading in a line from a txt file

0 replies on 1 page.

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 0 replies on 1 page
jason

Posts: 7
Nickname: bibs
Registered: Nov, 2002

help with reading in a line from a txt file Posted: Nov 25, 2002 3:31 AM
Reply to this message Reply
Advertisement
hello i hope someone can help me please
i have this code which i want to be able read in my txt file and read in the first line of the file and then check if it has a comma to seperate any words and to print the empty spaces aswell as the text but also i want to be able to print out any word of the file after i have iterated though it how can i do this
thanks for your time
jason



import java.io.*;
import java.util.*;
import java.lang.*;
import java.util.StringTokenizer.*;

public class RT2 {


public static void main(String[] args) {
String cT = null;
String lT = null;
try {


Vector v = new Vector(100);
FileReader file = new FileReader("Work.txt");
BufferedReader buff = new BufferedReader(file);
String line = buff.readLine();
StringTokenizer s = null;
boolean eof = false;
while (!eof){
s = new StringTokenizer(line.trim(),",",true);
while (s.hasMoreTokens()) {
cT = s.nextToken();
if (cT.equals(",") && lT.equals(",")){
v.add("");
}
else{
if (!cT.equals(",")) {
v.add(cT);
}
}
lT = cT;


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

}
for (Enumeration e = v.elements();e.hasMoreElements();) {
String se = (String)e.nextElement();
System.out.println(se);



}
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());

}
}
}

Topic: Confused and need help desperately Previous Topic   Next Topic Topic: Chat Program Problem -using sockets

Sponsored Links



Google
  Web Artima.com   

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