Advertisement
Guest User

MapLoader.java

a guest
Feb 13th, 2021
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.93 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class MapLoader {
  5.    
  6.     public static String [] load (String fileName, int mapIndex) {
  7.         boolean flag;
  8.         Scanner inputFile;
  9.         String fileLine;
  10.         List <String> linesList = new ArrayList <> ();
  11.        
  12.         flag = false;
  13.         try {
  14.             inputFile = new Scanner (new File (fileName));
  15.             while (true) {
  16.                 if (!inputFile .hasNextLine ()) {
  17.                     break;
  18.                 }
  19.                 fileLine = inputFile .nextLine ();
  20.                 if (isMapString (fileLine)) {
  21.                     flag = true;
  22.                     if (0 == mapIndex) {
  23.                         linesList .add (fileLine);
  24.                     }
  25.                     continue;
  26.                 }
  27.                 if (flag) {
  28.                     if (0 == mapIndex) {
  29.                         break;
  30.                     }
  31.                     --mapIndex;
  32.                     flag = false;
  33.                 }
  34.             }
  35.             inputFile .close ();
  36.         } catch (IOException ex) {
  37.             ex .printStackTrace ();
  38.             return null;
  39.         }
  40.        
  41.         if (!flag || 0 != mapIndex) {
  42.             return null;
  43.         }
  44.         return linesList .toArray (new String [linesList .size ()]);
  45.     }
  46.    
  47.     private static boolean isMapString (String arg) {
  48.         int k, length;
  49.         boolean flag = false;
  50.        
  51.         length = arg .length ();
  52.         if (3 > length) {
  53.             return false;
  54.         }
  55.         for (k = 0; length > k; ++k) {
  56.             switch (arg .charAt (k)) {
  57.             case '#':
  58.                 flag = true;
  59.             case ' ':
  60.             case '$':
  61.             case '*':
  62.             case '+':
  63.             case '.':
  64.             case '@':
  65.                 break;
  66.             default:
  67.                 return false;
  68.             }
  69.         }
  70.         return flag;
  71.     }
  72. }
  73.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement