首页 > 解决方案 > 将 Object[] 方法添加到 main()

问题描述

我试图通过我的 main() 函数传递这个方法,但是 loadMap 已经有了 bufferreader,所以我试图使用它而不是创建我自己的新缓冲区读取器。我怎样才能做到这一点?

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;
    }
}

标签: user-interfaceobjectjavafxmain

解决方案


main()除了 call ,你不应该做任何事情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);
} 

推荐阅读