0

x & y 座標と型の値を含むファイルに基づいたマップを表示するプログラムのコードを書いています。

私のコードは ASCII マップを正しく表示し、マップ上で機能を実行できるようにしますが、コードが最終的に次のオプションを選択するための入力を受け取る switch ステートメントに戻ると、「option = input.nextInt()」という行がスキップされます。 ;" そして、「input.nextLine();」である finally ブロックに進みます。Java.util.NoSuchElementException が発生します。

これがなぜなのか誰か知っていますか?この例外は通常、そのような要素がない場合に列挙で発生することは知っていますが、コードが input.nextInt() 行をスキップしてから input.nextLine(); で失敗する理由はわかりません。

このエラーは、最後に viewMap Switch ケースで Gorgon ケースがアクティブになっている場合にのみ発生します。「input.nextInt()」呼び出しの前に「input.nextLine()」呼び出しを追加しようとしましたが、違いはありません。

MapViewer プログラムの Switch ステートメントのコードと、マップを作成して表示するためのコードが含まれています。最後に Gorgon クラスも追加しました。

public class MapViewerMenu {

    public int option = 0;
    public boolean complete = false;

    Scanner input = new Scanner(System.in);

    char [][] mapper = null;

    Map currentMap = null;
    User currentUser = null;
    Vector <Grue> currentGrues = null;

    public void run(){

        while(!complete){
            System.out.println("*******************");
            System.out.println("* Map Viewer Menu *");
            System.out.println("*******************");
            System.out.println("1. Load Files");
            System.out.println("2. Set Symbols");
            System.out.println("3. View Map");
            System.out.println("4. Scramble Map");
            System.out.println("5. Reset Map");
            System.out.println("6. Exit");
            System.out.println("");

            try{
                option = input.nextInt();
            }
            catch(Exception e){
                System.out.println("Error in input.");
                System.out.println("Try again.");
                System.out.println("");
            }
            finally{

// The Error occurs once the program exits viewMap and returns here. 
//It skips the above "option = input.nextInt();" and comes down here and fails.

                input.nextLine();
            }

            switch(option){

            //removed irrelevant cases

            case 3:
                viewMap();
                break;

            case 6:
                complete = true;
                input.close();
                break;

            }
        }
    }

    public void viewMap(){
        //removed earlier code that built part of the map for readability

        number = currentGrues.elements(); //this is an enumeration

        while(number.hasMoreElements()){
            temp3 = number.nextElement();
            int sure =2;

            switch(temp3.getName()){

            case "Gorgon":

                while(sure != 0){
                    System.out.printf("Would you like the Gorgon to change a square type?\n");
                    System.out.println("(0 for yes, 1 for no.)");

                    try{
                        sure = input.nextInt();
                    }
                    catch(Exception e){
                        System.out.println("Error in input.");
                        System.out.println("Try again.");
                        System.out.println("");
                        sure = 2;
                    }
                    finally{
                        input.nextLine();
                    }

                    if(sure == 0){
                        Gorgon gor = (Gorgon) temp3;
                        gor.boulder(currentMap, mapper);
                        after = true;
                        break;
                    }
                    else{
                        if(sure == 1){
                            break;
                        }
                    }
                }
                break;

// A boolean value after tells the program to reprint the map.

        if(after){
            System.out.println("Since some squares were changed, the updated map is printed.");
            System.out.println("");
            System.out.printf("Map Name: %s", currentMap.getName());
            System.out.println("");

            for(xpos = 0, ypos = 0; xpos < 16 && ypos < 16;){
                System.out.print(mapper[xpos][ypos]);
                xpos++;

                if(xpos < 16){
                    continue;
                }
                else{
                    System.out.println("");
                    ypos++;
                    if(ypos < 16){
                        xpos = 0;
                        continue;
                    }
                }
            }
            System.out.println("");
        }
}

//This should then go back to the "option = input.nextInt" line, where it should ask the user for input, but doesnt.

import java.util.Scanner;

public class Gorgon extends Giant {

     //Included is the function called by the viewMap switch statement.
     //I added this because the error might exist here.
     //other classes have similar functions, but perform just fine.

    public void boulder(Map map, char[][] mapper){
        int x = 0;
        int y = 0;
        Scanner input = new Scanner(System.in);

        System.out.println("The Gorgon wants to turn an adjacent Square to stone!");
        System.out.printf("Its position is (%d,%d).\n", this.currPos.col, this.currPos.row);

        do{
            do{ // Get an adjacent x coordinate
                try{
                    System.out.println("Enter an x coordinate:");
                    x = input.nextInt();
                }
                catch(Exception e){
                    System.out.println("Error in input.");
                    System.out.println("Try again:");
                    System.out.println("");
                }
                finally{
                    input.nextLine();
                }
            }while(!(x >= currPos.col-1 && x <= currPos.col+1));

            do{  // get an adjacent y coordinate
                try{
                    System.out.println("Enter a y coordinate:");
                    y = input.nextInt();
                }
                catch(Exception e){
                    System.out.println("Error in input.");
                    System.out.println("Try again:");
                    System.out.println("");
                }
                finally{
                    input.nextLine();
                }
            }while(!(y >= currPos.row-1 && y <= currPos.row+1)); // keep asking for input while the input isn't within range.

        }while(!((x >= currPos.col-1 && x <= currPos.col+1) && (y >= currPos.row-1 && y <= currPos.row+1)));

        mapper[x][y] = 'B'; //Change the value in map data.
        input.close();  //Close local input reader.

}

ユーザーは (3,3) にいます

私はグルーのコレクションに (6,6) のゴルゴンを持っています。これは、ユーザーが立つことができない障害物である「岩」を入力するために隣接する正方形を回転させることができるはずです。

Gorgon の関数ボールダーを使用することを選択するたびに、プログラムの最初にある元の switch ステートメントに到達すると、プログラムが失敗します。

プログラムが実行されると、通常は次のようになります。

適切なファイルをロードします (これらはエラーとは関係ありません)。


  • マップ ビューアー メニュー *


    1. ファイルのロード
    2. 記号を設定する
    3. 地図を見ます
    4. スクランブルマップ
    5. マップをリセット
    6. 出口

3

建物マップ...

マップは ASCII 文字で表示されます。

ユーザーのロープが盗まれました!

ジャイアントに正方形のタイプを変更してもらいたいですか?

(はいの場合は 0、いいえの場合は 1)

1

肉!肉!肉!

ゴルゴンを四角いタイプに変えてみませんか?

(はいの場合は 0、いいえの場合は 1)

0

ゴルゴンは、隣接するスクエアを石に変えようとしています!

その位置は (6,6) です。

X 座標を入力してください:

7

Y 座標を入力してください:

6

一部の正方形が変更されたため、更新されたマップが印刷されます。

更新されたものが再び正しく印刷されます。


  • マップ ビューアー メニュー *


    1. ファイルのロード
    2. 記号を設定する
    3. 地図を見ます
    4. スクランブルマップ
    5. マップをリセット
    6. 出口

入力エラー。再試行。

Exception in thread "main" java.util.NoSuchElementException: No line found
    at java.util.Scanner.nextLine(Unknown Source)
    at viewer.MapViewerMenu.run(MapViewerMenu.java:92) //points to the finally block at the beginning, where "input.nextLine()" is written.
    at viewer.MapViewer.main(MapViewer.java:40) //This is just the original main that runs the program.

クラスの元の switch ステートメントに戻ると失敗しますが、最初に戻って再度 switch の入力を求める必要があります。

4

1 に答える 1

0

このエラーは、Gorgon のボルダー関数にアクセスすることで修正され、System.in で新しいスキャナーを開く代わりに、既存のスキャナーをパラメーターに渡すだけで済みました。

system.in で別のスキャナーを作成して閉じたという事実は、他のスキャナーの障害と関係がある可能性があります。

あなたの助けと時間をありがとう。

于 2013-11-03T20:31:01.820 に答える