1

I’ve a problem finding the proper regex to read a ppm file dimensions. I don’t know if it’s because my regex is wrong or if I misuse the Pattern.compile method, but I get a java.util.NoSuchElementException. Below my regex and the code along to it. It reads the whole file since my regex always return null. Thanks.

package projet;
import java.awt.Color;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Pattern;

public class Ppm extends File{
    private Dimensions d;
    private Color[][] p;
    public Ppm(String pathname) {
        super(pathname);
}
public void read(){
    try {
        Scanner s=new Scanner(new FileReader(this.getPath()));
        while(s.findInLine(Pattern.compile("/^\\d* \\d*\\s$/")) == null && s.hasNext()){
            System.out.println(s.nextLine());
        }
        s.close();
    } catch (FileNotFoundException ex) {
        Logger.getLogger(Ppm.class.getName()).log(Level.SEVERE, null, ex);
        System.out.println("Check filepath.");
    }
}
}

Here's my sample file : https://www.dropbox.com/s/z35dvi90zdfkws0/carte1.ppm?dl=0

EDIT :

I tried with a matcher, but it still doesn't work, following the corrected regex. Here's my code, it never matches the pattern...

public class Ppm extends File{
    private Dimensions d;
    private Color[][] p;
    public Ppm(String pathname) {
        super(pathname);
        d=new Dimensions(0, 0);
    }
    public void read(){
        try {
            Scanner s=new Scanner(new FileReader(this.getPath()));
            Pattern p=Pattern.compile("^\\d+ \\d+\\s$");
            Matcher m;
            while(s.hasNext()){
                m=p.matcher(s.nextLine());
                if(m.matches()){
                    System.out.println("Found");
                }
            }
            //System.out.println("W : "+d.getWidth()+" H: "+d.getHeight());
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Ppm.class.getName()).log(Level.SEVERE, null, ex);
            System.out.println("Check filepath.");
        }
    }
}
Lapsusone
  • 79
  • 6
  • You are checking for `hasNext()` and printing `nextLine?` – TheLostMind Sep 15 '14 at 06:23
  • It's for sure problem with Scanner. Moreover, there is no need to create Pattern object for every iteration, just create it once and use scanner.useDelimiter(yourPattern) . Can you paste a sample fragment because i opened the file which you posted and it contains lot's of number. – sol4me Sep 15 '14 at 06:38
  • That's the sample I use. Cut it if you want a smaller fragment but it's a basic one. I really don't get why my code doesn't work... – Lapsusone Sep 21 '14 at 20:58

1 Answers1

0

Your regex can never match.

Pattern.compile("/^\\d* \\d*\\s$/")

asks for a text has a slash before the beginning of the string and after the end of the string. Unlike PHP, Java doesn't use regex delimiters. Try

Pattern.compile("^\\d* \\d*\\s$")

(and be aware that this also matches strings like " 1 " or "1 " or even " ").

Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561
  • Thank you. I need to change the '*' by a '+' to scan for two numbers following each other right? Also I replaced the regex but it stills doesn't find the dimensions line and prints everything. – Lapsusone Sep 15 '14 at 08:42