В моем вспомогательном методе изображений мне нужно найти и пропустить / игнорировать # комментарии. Они могут находиться на любой строке между тем, когда я сканирую fileType и maxValue, а комментарии сохраняются до конца строки. fileType, столбцы, строки и maxValue могут находиться в разных строках или в одной строке. В начале метода я пытаюсь игнорировать комментарии, но при запуске он выдает мне IllegalStateException, и сканер закрывается. Почему это? Может ли кто-нибудь показать мне, как игнорировать # комментарии.
открытый класс ImageChallenge {
public static final int TOP = 20; // top edge of the image
public static final int LEFT = 20; // left edge of the image
public static final int PIXEL_SIZE = 2;
int cols;
int rows;
int offset;
int maxValue;
int r;
int g;
int b;
int G;
Color color;
public void renderImageChallenge(){
/*# YOUR CODE HERE */
int nRepeats = 3; //select number of repeats for animation
File myFile = new File(UIFileChooser.open());
try{
for(int repeat = 0; repeat <= nRepeats; repeat++){
Scanner scan = new Scanner(myFile);
if(scan.hasNext()){
this.renderImageHelper(scan);
if(!scan.hasNext()){
return;
}
}
while(scan.hasNext()){
this.renderImageHelper(scan);
UI.sleep(50);
}
}
} catch (IOException e) {UI.printf("File error: %s\n", e);}
}
//mystring = mystring.replaceAll("#.*$", "");
public void renderImageHelper(Scanner scan){
/*# YOUR CODE HERE */
UI.clearGraphics();
int specs[] = new int[3];
specs[0] = cols;
specs[1] = rows;
specs[2] = maxValue;
String fileType = scan.next();
if(fileType.equals("P3")){
for(int k = 0; scan.hasNext() && k<=3; k++){
String part = scan.next();
if(!part.contains("#")){
int i = 0;
specs[i] = Integer.parseInt(part);
i++;
}
}
r = 0;
g = 0;
b = 0;
for(int row = 0; row < rows; row++){
double y = TOP+(row*PIXEL_SIZE);
for(int col = 0; col < cols; col++){
double x = LEFT+(col*PIXEL_SIZE);
if(scan.hasNextInt()){
r = scan.nextInt()*(255/maxValue);
g = scan.nextInt()*(255/maxValue);
b = scan.nextInt()*(255/maxValue);
UI.setColor(new Color(r, g, b));
}
UI.fillRect(x, y, PIXEL_SIZE, PIXEL_SIZE);
}
}
}
else if(fileType.equals("P2")){
cols = scan.nextInt();
rows = scan.nextInt();
maxValue = scan.nextInt();
G = 0;
for(int row = 0; row < rows; row++){
double y = TOP+(row*PIXEL_SIZE);
for(int col = 0; col < cols; col++){
double x = LEFT+(col*PIXEL_SIZE);
if(scan.hasNextInt()){
G = scan.nextInt()*(255/maxValue);
UI.setColor(new Color(G, G, G));
}
UI.fillRect(x, y, PIXEL_SIZE, PIXEL_SIZE);
}
}
}
else{
UI.println("File is not applicable");
scan.close();
}
}
}