文字游戏协助

时间:2014-12-14 03:38:43

标签: java text

这可能是一个简单的修复和一个愚蠢的问题,但我仍然是学习java的新手。我目前正在研究基于文本游戏的类代码。下面的代码是我们从教科书中获得的用于游戏的内容。我主要更改了文本文件中的实际文本,并没有对代码做太多但是我收到以下错误:

Exception in thread "main" java.util.NoSuchElementException: No line found
    at java.util.Scanner.nextLine(Unknown Source)
    at BradySkuza58.getRoom(BradySkuza58.java:143)
    at BradySkuza58.loadRoomsFromFile(BradySkuza58.java:90)
    at BradySkuza58.main(BradySkuza58.java:39)

这是我用于文字游戏的代码。

 import java.util.Scanner;

    class Room
    {
        int roomNumber;
    String roomName;
    String description;
    int numExits;
    String[] exits = new String[20];
    int[] destinations = new int[20];
}

public class BradySkuza58
{
    public static void main( String[] args )
    {
        Scanner keyboard = new Scanner(System.in);

        // initialize rooms from file
        Room[] rooms = loadRoomsFromFile("textadventurerooms.txt");

        //showAllRooms(rooms); // for debugging

        // Okay, so let's play the game!
        int currentRoom = 0;
        String ans;
        while ( currentRoom >= 0 )
        {
            Room cur = rooms[currentRoom];
            System.out.print( cur.description );
            System.out.print("> ");
            ans = keyboard.nextLine();

            // See if what they typed matches any of our exit names
            boolean found = false;
            for ( int i=0; i<cur.numExits; i++ )
            {
                if ( cur.exits[i].equals(ans) )
                {
                    found = true;
                    // if so, change our next room to that exit's room number
                    currentRoom = cur.destinations[i];
                }
            }
            if ( ! found )
            System.out.println("Sorry, I don't understand.");
        }

    }

    public static Room[] loadRoomsFromFile( String filename )
    {
        Scanner file = null;
        try
        {
            file = new Scanner(new java.io.File(filename));
        }
        catch ( java.io.IOException e )
        {
            System.err.println("Sorry, I can't read from the file '" +filename + "'.");
            System.exit(1);
        }

        int numRooms = file.nextInt();
        Room[] rooms = new Room[numRooms];

        // initialize rooms from file
        int roomNum = 0;
        while ( file.hasNext() )
        {
            Room r = getRoom(file);
            if ( r.roomNumber != roomNum )
            {
                System.err.println("Reading room # " + r.roomNumber + ", but" + roomNum + " was expected.");
                System.exit(2);
            }
            rooms[roomNum] = r;
            roomNum++;
        }
        file.close();

        return rooms;
    }

    public static void showAllRooms( Room[] rooms )
    {
        for ( Room r : rooms )
        {
            String exitString = "";
            for ( int i=0; i<r.numExits; i++ )
            exitString += "\t" + r.exits[i] + " (" + r.destinations[i] +")";
            System.out.println( r.roomNumber + ") " + r.roomName + "\n" +exitString );
        }
    }

    public static Room getRoom( Scanner f )
    {
        // any rooms left in the file?
        if ( ! f.hasNextInt() )
        return null;

        Room r = new Room();
        String line;

        // read in the room # for errorchecking later
        r.roomNumber = f.nextInt();
        f.nextLine(); // skip "\n" after room #

        r.roomName = f.nextLine();

        // read in the room's description r.description = "";
        while ( true )
        {
            line = f.nextLine();
            if ( line.equals("%%") )
            break;
            r.description += line + "\n";
        }

        // finally, we'll read in the exits
        int i = 0;
        while ( true )
        {
            line = f.nextLine();
            if ( line.equals("%%") )
            break;
            String[] parts = line.split(":");
            r.exits[i] = parts[0];
            r.destinations[i] = Integer.parseInt(parts[1]);
            i++;
        }
        r.numExits = i;

        // should be done; return the Room
        return r;
    }

}

第39行

Room[] rooms = loadRoomsFromFile("textadventurerooms.txt");

第90行

    Room r = getRoom(file);

第143行

    line = f.nextLine();

我没有附加文本文件本身,但如果需要找到错误,我可以。

2 个答案:

答案 0 :(得分:0)

我猜你的文本文件没有你的程序所期望的那么多行(你试图在达到EOF后调用nextLine()。仔细检查你的文本文件是否与你想要的一致。没有在文本文件中很难提供更多细节。根据它确定你是否突破了while循环。你也可以通过调用hasNextLine()来检查是否还有另一行。

答案 1 :(得分:0)

在您的会议室定义文件中添加一行“%%”。您的解析器需要。