aspose file tools*
The moose likes Beginning Java and the fly likes please help with tokenizing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "please help with tokenizing" Watch "please help with tokenizing" New topic
Author

please help with tokenizing

kevin comario
Ranch Hand

Joined: Jun 29, 2002
Posts: 65
My professor has given us a two part project of input output, but i can't get past the first part. I need to read in a file that contains blank lines, some sentences preceeded by the # symbol and also keys=values statements. I'm supposed to avoid everything except the key=value statements tokenize them and feed them in to a Hashtable. my question is how do i do that, i understand how tokenize on "=" but how do i get the tokens individualy and seperate them in to seperate key and value variables.
Any help would be appreciated.
Thanks in advance.
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
kevin,
Welcome to JavaRanch!
It would be easier (from my perspective) to help you with this project if you were to post an example of the file contents that you have to deal with, and, most importantly, whatever it is that you have tried so far. Note that when posting code, you can preserve most formatting by surrounding the code with the [code] and [/code] UBB Tags.
In case you aren't already familiar with it, be sure to read about any classes that you'd propose to use in The Standard API Documentation.
Good Luck.


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
kevin comario
Ranch Hand

Joined: Jun 29, 2002
Posts: 65
sorry forgot to put the text file heres what it looks like:
#start of file

#port
port=80

#domain name
domain=www.calstatela.edu

#invalid line
this is an invalid line

#index file
index=index.html

#end of file
************************************************
sorry i have no real useful code.
kevin comario
Ranch Hand

Joined: Jun 29, 2002
Posts: 65
if your curious as to what the complete project is you can visit the website:
http://www.calstatela.edu/faculty/jmiller6/cs212/assignments/Assignment1.html
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
You forgot the most important part. What have you tried so far? What are your thoughts on how to solve this problem?
kevin comario
Ranch Hand

Joined: Jun 29, 2002
Posts: 65
here is what i have tried so far:

when i try this code i cant read any of the key= value statements that i want instead it continually loops doing nothig.
sorry sbout my terrible code but its what i have so far.
[ edited to preserve formatting using the [code] and [/code] UBB tags -ds ]
[ June 29, 2002: Message edited by: Dirk Schreckmann ]
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
What do you expect your example program to do? What does it actually do? Does it compile and run without errors?
Rene Larsen
Ranch Hand

Joined: Oct 12, 2001
Posts: 1179

You are on the right track, BUT you need to break this 'project' up into at least two phases.
First you need to be able to read a file from the beginning to the end the correct way.
Second you need to check if there is a '=' in the current line - if so then use the StringTokenizer and put key, value into a HashMap.
/Rene


Regards, Rene Larsen
Dropbox Invite
Rene Larsen
Ranch Hand

Joined: Oct 12, 2001
Posts: 1179

You also need to give the file used as a parameter when you start you program.
e.g. java ConfigReader C:\kel.txt
You should only print the HashMap ones.
/Rene
kevin comario
Ranch Hand

Joined: Jun 29, 2002
Posts: 65
here are some of the changes that i have made, but again it continually loops doing nothing:
please help:

can someone please help me with seperating what i dont need from the key = value that i need. I tried by seperating the file with if else statements and sending what i need to a method i called hashTab but it dosent seem to work.
can anyone help i need to avoid blank lines and other lines that are not in a key=value statement.
[ edited to preserve formatting using the [code] and [/code] UBB tags -ds ]
[ June 30, 2002: Message edited by: Dirk Schreckmann ]
Rene Larsen
Ranch Hand

Joined: Oct 12, 2001
Posts: 1179

Try this:


/Rene
kevin comario
Ranch Hand

Joined: Jun 29, 2002
Posts: 65
thank you rene.
kevin comario
Ranch Hand

Joined: Jun 29, 2002
Posts: 65
I just have one more question.
i have also created another class in a diffent file and package, the package name is log and the class is called LogWriter, within the class i have a method called write that has void as a return type and takes a String as a paramenter.
My question is from the previous file that contained key=value pairs, lines and words procedeeded by # symbols after i send that info to my Logwriter.write() method how do i get it to print to a file in a form sort of like this:
Monday 6-24-2002 14:39:03 blank line
Monday 6-24-2002 14:39:04 port=80
Monday 6-24-2002 14:39:06 blank line
Monday 6-24-2002 14:39:08 domain=www.calstatela.edu
Monday 6-24-2002 14:39:10 blank line
Monday 6-24-2002 14:39:12 ERROR: this is an invalid line
Monday 6-24-2002 14:39:13 blank line
Monday 6-24-2002 14:39:15 index=index.html
Monday 6-24-2002 14:39:17 blank line
*************************************************
havent tried the date format yet but when i try to write to the file i think one writes over the other and dosent skip a line like its supposed to.
any help would be appreciated.
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1873
hi
also if u want to ignore the blank lines having nothing in it except new line character then, u should do,
line = buff.readLine().trim();
if ( line.equals("") ) {
//its a blank line
//write how u want to handle this
}
also i would suggest to trim every line u read as it might be possible that u have some spaces before u have # sign and in that case ur method of line.startsWith("#") won't work...
regards
maulin
kevin comario
Ranch Hand

Joined: Jun 29, 2002
Posts: 65
one mare question i have this code and i'm not getting done what i want:
package log;
import java.io.*;
import java.util.*;
import java.text.*;

public class LogWriter {
public static void write(String s){
//String a = value;
try {
PrintWriter pw = new PrintWriter(new FileOutputStream("C:\\logfile.txt"),true);
StringReader sr = new StringReader(s);
String a = ": Error: ";
String b = ": Blank Line";
String space = " ";
SimpleDateFormat sdf = new SimpleDateFormat("EEEE MM-dd-yyyy HH:mm:ss");
Date currentTime = new Date();
String d = sdf.format(currentTime);

if (s.indexOf("") > 0) {
pw.println(d + b);
}
if(s.indexOf("=") > 0) {
pw.println(d + space + s);
}
pw.flush();

}catch (Exception e) {
System.err.println("error");
}
}
}
**************************************************
i need to print out a file, but when i do it, it prints out only one line i need to print all relavent lines. Can anyone help.
Thanks in advance.
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1873
hi
where u put ur LogWriter.write() call in the code? post the new code after u made changes suggested by Rene.
'coz u should have LogWriter.write() for each line you are scanning...so put that in the while loop at the last after all "if" conditions....
regards
maulin
kevin comario
Ranch Hand

Joined: Jun 29, 2002
Posts: 65
thanks for your help everyone, I did the best i could and turned in my project. I also have one more question do you recomend any books especially related toward data structures.
once again thank you.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: please help with tokenizing