• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

please help with tokenizing

 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
kevin comario
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What do you expect your example program to do? What does it actually do? Does it compile and run without errors?
 
Ranch Hand
Posts: 1179
Mac OS X Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Rene Larsen
Ranch Hand
Posts: 1179
Mac OS X Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1179
Mac OS X Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try this:


/Rene
 
kevin comario
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you rene.
 
kevin comario
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1873
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic