Добавление метода Object [] в main () - PullRequest
0 голосов
/ 25 мая 2018

Я пытаюсь передать этот метод через мою функцию main (), но в LoadMap уже есть средство чтения буфера, поэтому я пытаюсь использовать его вместо создания собственного нового средства чтения буфера.Как я могу это сделать?

public static void main(String args[]) {
    //throw exceptions here if args is empty
    filename = args[0];
    System.out.println(MapIO.loadMap(filename)[0]);
    System.out.println(MapIO.loadMap(filename)[1]);

    if (args.length < 1) {
        System.err.println("Usage:\n" +"java CrawlGui mapname");
        System.exit(1);
    }
    List<String> names=new LinkedList<String>();

    try (BufferedReader reader = new BufferedReader(new FileReader(new
            File(filename)))) {

        String line;
        while ((line = reader.readLine()) != null)
            names.add(line);
            System.out.println(names);

    } catch (IOException e) {
        e.printStackTrace();
    }

    MapIO.loadMap(filename);


    launch(args);
} 


/** Read information from a file created with saveMap
 * @param filename Filename to read from
 * @return null if unsucessful. If successful, an array of two Objects.
        [0] being the Player object (if found) and 
        [1] being the start room. 
 * @detail. Do not add the player to the room they appear in, the caller 
        will be responsible for placing the player in the start room.
 */

public static Object[] loadMap(String filename) {
    Player player = null;

    try {
        BufferedReader bf = new BufferedReader(
                new FileReader(filename));
        String line = bf.readLine();
        int idcap = Integer.parseInt(line);
        Room[] rooms = new Room[idcap];
        for (int i = 0; i < idcap; ++i) {
            line = bf.readLine();
            if (line == null) {
                return null;
            }
            rooms[i] = new Room(line);
        }
        for (int i = 0; i < idcap; ++i) {  // for each room set up exits
            line = bf.readLine();
            int exitcount=Integer.parseInt(line);
            for (int j=0; j < exitcount; ++j) {
                line = bf.readLine();
                if (line == null) {
                    return null;
                }
                int pos = line.indexOf(' ');
                if (pos < 0) {
                    return null;
                }
                int target = Integer.parseInt(line.substring(0,pos));
                String exname = line.substring(pos+1);
                try {
                    rooms[i].addExit(exname, rooms[target]);
                } catch (ExitExistsException e) {
                    return null;
                } catch (NullRoomException e) {
                    return null;
                }
            }
        }
        for (int i = 0;i<idcap;++i) {
            line = bf.readLine();
            int itemcount = Integer.parseInt(line);
            for (int j = 0; j < itemcount; ++j) {
                line = bf.readLine();
                if (line == null) {
                    return null;
                }                    
                Thing t = decodeThing(line, rooms[0]);
                if (t == null) {
                    return null;
                }
                if (t instanceof Player) { // we don't add 
                    player = (Player)t;      // players to rooms
                } else {
                    rooms[i].enter(t);
                }
            }
        }
        Object[] res = new Object[2];                        
        res[0] = player;
        res[1] = rooms[0];
        return res;
    } catch (IOException ex) {
        return null;
    } catch (IndexOutOfBoundsException ex) {
        return null;
    } catch (NumberFormatException nfe) {
        return null;
    }
}

1 Ответ

0 голосов
/ 25 мая 2018

Вы не должны делать ничего в main() кроме звонка launch().Переместите весь другой код запуска в ваш метод start().Вы можете получить содержимое массива args, используя getParameters().getRaw():

@Override
public void start(Stage primaryStage) {

    //throw exceptions here if args is empty
    filename = getParameters().getRaw().get(0);

    System.out.println(MapIO.loadMap(filename)[0]);
    System.out.println(MapIO.loadMap(filename)[1]);

    if (args.length < 1) {
        System.err.println("Usage:\n" +"java CrawlGui mapname");
        System.exit(1);
    }
    List<String> names=new LinkedList<String>();

    try (BufferedReader reader = new BufferedReader(new FileReader(new
            File(filename)))) {

        String line;
        while ((line = reader.readLine()) != null)
            names.add(line);
            System.out.println(names);

    } catch (IOException e) {
        e.printStackTrace();
    }

    Object[] whateverThisThingIs = MapIO.loadMap(filename);

    // Now you have access to everything you need, at the point where you need it.

    // existing start() code goes here...

}

public static void main(String args[]) {
    launch(args);
} 
...