java - Throw Exception when the String shape is illegal -


im doing wrong excetpion in code because when string layout such as:

..b. bbb.     //<---illegal  ... .x. ...      //<---illegal  ..r .rr      //<---illegal  ..... .y... ..y.. ...y.    //<---illegal 

is passed method (only 1 layout can passed @ 1 time), the method should throw exception because string layouts shapes must have @ least 1 filled block in each of 0th row, 0th column, last row, , last column. following string layouts legal:

...e ..e. e...  a...a ..... ..... a.... 

my code handles exception when sees first , last charcters of first line. can smb please me out method regarding throwing exception? in advance!

public static shape makeshape(string layout,char displaychar)   {       shape result;       int height = 0;       int width = 0;       scanner data = new scanner(layout);       char[][] temp;       while(data.hasnextline())       {           string line = data.nextline();           height = line.length();           width++;       }       temp = new char[height][width];        scanner data2 = new scanner(layout);       while(data2.hasnextline())       {           string line2 = data2.nextline();           if(line2.charat(0) == '.' && line2.charat(width) == '.')               throw new fititexception("empty borders!");          else {              (int r = 0; r < height; r++)                 (int c = 0; c < width; c++) {                     // system.out.println(line2.charat(c));                      if (temp[r][c] == '.') {                         temp[r][c] = displaychar;                     }                     system.out.println(line2.charat(temp[r][c]));                      }             }       }       result = new createshape(height, width, displaychar, layout);       return result;   } 

there couple of things not clear me still, concentrated on parsing layout 2 dimensional char array , checking constraints specified. let adapt exact needs:

public static char[][] parseshape(string layout, char displaychar) throws exception {     int height = 0;     scanner data = new scanner(layout);     arraylist<string> lines = new arraylist<string>();      // parse layout array of lines determine dimensions     while (data.hasnextline()) {         string line = data.nextline();         lines.add(line);         height = line.length();     }     int width = lines.size();     char[][] temp = new char[height][width];     boolean row0 = false;     boolean col0 = false;     boolean rowlast = false;     boolean collast = false;      // parse array of lines in char array , check constraints     (int w = 0; w < width; w++) {         string line = lines.get(w);         (int h = 0; h < height; h++) {             char c = line.charat(h);             if (c == displaychar) {                  // looking @ display characters,                 // check if we're in of rows of columns matter                 if (h == 0)                     row0 = true;                 if (w == 0)                     col0 = true;                 if (h == height - 1)                     rowlast = true;                 if (w == width - 1)                     collast = true;             }             temp[h][w] = c;         }     }      // if of constraints not true, layout invalid     if(!row0) {         throw new exception("no block in oth row");     }     if(!col0) {         throw new exception("no block in oth column");     }     if(!rowlast) {         throw new exception("no block in last row");     }     if(!collast) {         throw new exception("no block in last column");     }     return temp; } 

basically have parse entire layout , accumulate constraints satisfied rather checking non-satisfaction. @ end know if not satisfied.


Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -