Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.*;
- public class MapLoader {
- public static String [] load (String fileName, int mapIndex) {
- boolean flag;
- Scanner inputFile;
- String fileLine;
- List <String> linesList = new ArrayList <> ();
- flag = false;
- try {
- inputFile = new Scanner (new File (fileName));
- while (true) {
- if (!inputFile .hasNextLine ()) {
- break;
- }
- fileLine = inputFile .nextLine ();
- if (isMapString (fileLine)) {
- flag = true;
- if (0 == mapIndex) {
- linesList .add (fileLine);
- }
- continue;
- }
- if (flag) {
- if (0 == mapIndex) {
- break;
- }
- --mapIndex;
- flag = false;
- }
- }
- inputFile .close ();
- } catch (IOException ex) {
- ex .printStackTrace ();
- return null;
- }
- if (!flag || 0 != mapIndex) {
- return null;
- }
- return linesList .toArray (new String [linesList .size ()]);
- }
- private static boolean isMapString (String arg) {
- int k, length;
- boolean flag = false;
- length = arg .length ();
- if (3 > length) {
- return false;
- }
- for (k = 0; length > k; ++k) {
- switch (arg .charAt (k)) {
- case '#':
- flag = true;
- case ' ':
- case '$':
- case '*':
- case '+':
- case '.':
- case '@':
- break;
- default:
- return false;
- }
- }
- return flag;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement