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.");
}
}
}