Java parsing question

Status
Not open for further replies.
Before parsing the line, include something to the tune of:

code:<HR>
Code:
switch (line[0])
{
  case ' ':
  case '\0':
  case '\n':
    (do nothing)
    break;<P>  case etc // include case's for lines that you do want to parse here
  ...
  ...
}

Sorry the code's in C++ - my Java is rusty.

General rule - when parsing, never assume that something will be right. Make sure that no matter how screwed-up the data file/input stream is, that the parser will politely say "sorry, can't parse that" and return, instead of crashing.

-Gabe
 

Aatos

Ars Tribunus Militum
2,639
gubmo, you're right, it is integer only. But it has the benefit of being a very fast way to compare values. switch {} block should always be used instead of if/else if/else block where it makes sense.

The complete code for the above should go something like this (which essentially means !EOF):

code:<HR>
Code:
 String from = "the filename";
 BufferedReader br = new BufferedReader(new FileReader(from));
 String line; // temp variable<P> while((line = br.readLine()) != null){<P>   myMethodWichDoesSomethingOrOther(line);
 }<P> br.close();<P>
 

gumbo

Ars Tribunus Angusticlavius
6,107
You can (theoretically) convert any type to any other type. Therefore, you can make a String into an int, or a char into an int. See This applet for code. However, the conversion methods may make it harder to read and introduce the possibility for error (since bits may be truncated).

IMO, using a switch for something besides an int would need to be looked at. Perhaps a new switch with overloaded String, char, etc. types could be made, if Java supported method overloading. (They do it for the + operator and such, why not make it publically available? C++ may (I don't think so) allow other type switching, but it can at least be overloaded to provide the correct functionality.

gubmo
 

wb

Ars Legatus Legionis
25,208
Subscriptor
gubmo, you're right, it is integer only. But it has the benefit of being a very fast way to compare values. switch {} block should always be used instead of if/else if/else block where it makes sense.

You can also switch on the char type, which is basically treated as an integer internally but has special handling for unicode.
 

JeffN825

Smack-Fu Master, in training
88
Thanks for all the help. The problem, however, was never about telling whether or not the line has a null value. I already have a structure in my code that makes sure the line isn't null before making references to it. The real problem seems to be that if the last line of the data file is empty, I get a null pointer error when the program tries to read in the line. It's no problem if other lines of the data file are blank. Just the last line. It seems almost as if the problem is that if the last line of the data file is blank, java doesn't count it as a line. All extra help is appreciated. Thanks again.
 
Hummm, could you post the code (if possible), or part of the code, that you are using to parse such file?? Otherwise is just a guessing game. I can help you if I can see what you are doing, otherwise I can only guess what you may be doing.

There are several bugs in the JVM (depends on the version). I know of the JVM blocking infinetily when waiting for a null value using sockets (streaming characters thru sockets). But I have not heard of a bug when parsing from a file.

In any case, your code will allows us to point out if anything is wrong or not.

Alex
:mad:
 

JeffN825

Smack-Fu Master, in training
88
import java.io.*;

public class Emails {
public static void main(String[] args) throws IOException {
TokenReader input=new TokenReader(System.in);
System.out.println("Enter the path and name of the data file to parse "+
"using forward slashes to indicate directory structure (ie. c:/foodir/foo.dat) "+
"(NOTE CASE SENSITIVITY):");
String inFileName=input.readLine();
BufferedReader inFile=new BufferedReader(new FileReader(inFileName));
System.out.println("Enter the path and name of the desired output file to be "+
"created in the same manner as above:");
String outFileName=input.readLine();
File f=new File(outFileName);
BufferedWriter outFile=new BufferedWriter(new FileWriter(outFileName));
FileInputStream tempInFile=new FileInputStream(inFileName);
int totalLines=0;
while (true) {
int tempReadFile=tempInFile.read();
if (tempReadFile==-1) {
break;
}
if ((char)(tempReadFile)=='\n') {
totalLines++;
}
}
totalLines++;
for (int i=1;i<=totalLines;i++) {
String line=inFile.readLine().toLowerCase();
if (!line.equals(null)&&line.length()>=5) {
if (line.charAt(0)=='r'&&line.charAt(1)=='e'
&&line.charAt(2)=='t'&&line.charAt(3)=='u'
&&line.charAt(4)=='r'&&line.charAt(5)=='n'
&&line.charAt(6)=='-'&&line.charAt(7)=='p'
&&line.charAt(8)=='a'&&line.charAt(9)=='t'
&&line.charAt(10)=='h'&&line.charAt(11)==':') {
int charLoc=0;
for (int j=0;j<line.length();j++) {
if (line.charAt(j)=='@') {
charLoc=j;
}
}
if (charLoc!=0) {
while (line.charAt(charLoc-1)!=' '&&line.charAt(charLoc-1)!='<') {
charLoc--;
}
String email="";
while (charLoc<line.length()&&line.charAt(charLoc)!='>') {
email+=line.charAt(charLoc);
charLoc++;
}
outFile.write(email);
outFile.newLine();
}
}
}
}
outFile.flush();
outFile.close();
}
}

The above is the code for the entire program. Thanks again for the help.
 

newton_meter

Ars Scholae Palatinae
1,424
Yikes, what a mess.

code:<HR>
Code:
if (!line.equals(null)&&line.length()>=5) {

I'm sure this isn't what you want. You're trying to avoid invoking a method on "line" when line is null. So, to test if line is null, you invoke the equals method on it?

Something's wrong there.

Meditate on:

code:<HR>
Code:
if (line != null && line.length() >= 5) {

(kg*m*m)/(s*s)
 
Instead of:

code:<HR>
Code:
int totalLines=0;
while (true) {
  int tempReadFile=tempInFile.read();
  if (tempReadFile==-1) {
    break;
  }
  if ((char)(tempReadFile)=='\n') {
    totalLines++;
  }
}
totalLines++;
for (int i=1;i<=totalLines;i++) {
  String line=inFile.readLine().toLowerCase();
  if (!line.equals(null)&&line.length()>=5) {
    if (line.charAt(0)=='r'&&line.charAt(1)=='e'
    &&line.charAt(2)=='t'&&line.charAt(3)=='u'
    &&line.charAt(4)=='r'&&line.charAt(5)=='n'
    &&line.charAt(6)=='-'&&line.charAt(7)=='p'
    &&line.charAt(8)=='a'&&line.charAt(9)=='t'
    &&line.charAt(10)=='h'&&line.charAt(11)==':') {
      int charLoc=0;
      for (int j=0;j<line.length();j++) {
        if (line.charAt(j)=='@') {
        charLoc=j;
      }
    }
    
    if (charLoc!=0) {
      while (line.charAt(charLoc-1)!=' '&&line.charAt(charLoc-1)!='<') {
        charLoc--;
      }
      String email="";
      while (charLoc<line.length()&&line.charAt(charLoc)!='>') {
        email+=line.charAt(charLoc);
        charLoc++;
      }
      outFile.write(email);
      outFile.newLine();
    }
  }
}

Perhaps something like:

code:<HR>
Code:
String line;
int charLoc = 0;
int totalLines=0;<P>while(in.ready()){
    String line=in.readLine();
    if(line==null || line.equals("")) continue;
    
    totalLines++;
    
    if (line.charAt(0)=='r'&&line.charAt(1)=='e'
    &&line.charAt(2)=='t'&&line.charAt(3)=='u'
    &&line.charAt(4)=='r'&&line.charAt(5)=='n'
    &&line.charAt(6)=='-'&&line.charAt(7)=='p'
    &&line.charAt(8)=='a'&&line.charAt(9)=='t'
    &&line.charAt(10)=='h'&&line.charAt(11)==':') {
      charLoc=0;
      for (int j=0;j<line.length();j++) {
        if (line.charAt(j)=='@') {
        charLoc=j;
      }
    }
    
    if (charLoc!=0) {
      while (line.charAt(charLoc-1)!=' '&&line.charAt(charLoc-1)!='<') {
        charLoc--;
      }
      String email="";
      while (charLoc<line.length()&&line.charAt(charLoc)!='>') {
        email+=line.charAt(charLoc);
        charLoc++;
      }
      outFile.write(email);
      outFile.newLine();
    }    
}
 
Status
Not open for further replies.