2021年1月22日星期五

Filled/Unfilled Rectangle From File

The project is supposed to read a file where the first line of the file tells how many rectangle are to be made, and the rest are the dimensions and whether it is filled or not. Assumes a Rectangle class with a toString to format the rectangles. I am getting an input mismatch error on the first line of my while loop, when setting the variable numLines, and need some help.

public static void main(String[] args) {          int columns = 0;          int rows = 0;          int numLines = 1;          String fill;          boolean filled = false;                    //first line of file creates array size          Rectangle[] rectangle = new Rectangle[numLines];          Scanner inFile = new Scanner("rectangle.txt");                    //reads file           while (inFile.hasNext()) {              numLines = inFile.nextInt();              for (int i=0; i<numLines; i++) {                  columns = inFile.nextInt();                  rows = inFile.nextInt();                  fill = inFile.nextLine();                  if (fill.equals("filled")) {                      filled = true;                  }                  Rectangle rec = new Rectangle(rows, columns, filled);                  rectangle = new Rectangle[numLines];                  rectangle[i] = rec;              }    //example of file being read        6      6 3 filled      3 6 unfilled      4 4 filled      6 6 unfilled      9 4 filled      4 8 unfilled  
https://stackoverflow.com/questions/65854464/filled-unfilled-rectangle-from-file January 23, 2021 at 08:17AM

没有评论:

发表评论