No Line Found Error(java.util.NoSuchElementException)

时间:2015-04-09 12:40:21

标签: java java.util.scanner

我尝试在我的代码中处理多个异常,同时使用扫描程序让用户输入新路径(如果当前的路径不正确),但我一直收到相同的错误," No Line实测值&#34 ;.任何帮助,将不胜感激。问题发生在" path = sc.nextLine()"的catch块中。

public class Deck {

    private static ArrayList<Card> monsters = new ArrayList<Card>();
    private static ArrayList<Card> spells = new ArrayList<Card>();
    private ArrayList<Card> deck = new ArrayList<Card>();
    private static String monstersPath = "Database-Monster.csv";
    private static String spellsPath = "Database-Spells.csv";

    // private static Board board;

    public Deck() throws IOException, UnknownCardTypeException,
            UnknownSpellCardException, MissingFieldException,
            EmptyFieldException {
        if (monsters== null) {
            monsters = loadCardsFromFile(monstersPath);
        }
        if (spells == null) {
            spells = loadCardsFromFile(spellsPath);
        }
        // shuffleDeck();
        buildDeck(monsters, spells);
        shuffleDeck();
    }

    /*
     * public static Board getBoard() { return board; }
     * 
     * public static void setBoard(Board board) { Deck.board = board; }
     */

    public ArrayList<Card> loadCardsFromFile(String path) throws IOException,
            UnknownCardTypeException, UnknownSpellCardException,
            MissingFieldException, EmptyFieldException {
        Scanner sc = new Scanner(System.in);
        int trials = 3;
        String currentLine = null;
        //String newPath = "";
        for (int i = 0; i <=trials ; i++) {
            try {


                FileReader fileReader = new FileReader(path);
                BufferedReader br = new BufferedReader(fileReader);
                ArrayList<Card> temp = new ArrayList<Card>();
                int sourceLineNumber = 1; // Source line
                while ((currentLine = br.readLine()) != null) {
                    String[] mOrS = new String[6]; // Monsters or Spells
                    mOrS = currentLine.split(",");
                    int sourceFieldNumber = 0;
                    while (sourceFieldNumber < mOrS.length) {
                        if (mOrS[sourceFieldNumber].equals("")
                                || mOrS[sourceFieldNumber].equals(" ")) {
                            throw new EmptyFieldException(path,
                                    sourceLineNumber, sourceFieldNumber + 1); // Depends
                                                                                // on
                                                                                // the
                                                                                // Splitted
                                                                                // String
                                                                                // array,
                                                                                // loop
                                                                                // on
                                                                                // every
                                                                                // field
                                                                                // and
                                                                                // check
                        }
                        sourceFieldNumber++;
                    }

                    if (mOrS[0].equals("Monster")) {
                        if (mOrS.length == 6) {
                            int attack = (int) (Integer.parseInt(mOrS[3]));
                            int defense = (int) (Integer.parseInt(mOrS[4]));
                            int level = (int) (Integer.parseInt(mOrS[5]));
                            MonsterCard monster = new MonsterCard(mOrS[1],
                                    mOrS[2], level, attack, defense);
                            temp.add(monster);
                        } else {
                            throw new MissingFieldException(path,
                                    sourceLineNumber); // Depends on the amount
                                                        // of fields in the
                                                        // String array, Monster
                                                        // should have 6, Type
                                                        // and 5 attributes.
                        }
                    } else if (mOrS[0].equals("Spell")) {
                        if (mOrS.length != 3) {
                            throw new MissingFieldException(path,
                                    sourceLineNumber); // Depends on the amount
                                                        // of fields in the
                                                        // String Array, Spells
                                                        // should have 3, Type
                                                        // and 2 attributes
                        }
                        if (mOrS[1].equals("Card Destruction")) {
                            CardDestruction cardDestruction = new CardDestruction(
                                    mOrS[1], mOrS[2]);
                            temp.add(cardDestruction);
                        } else if (mOrS[1].equals("Change Of Heart")) {
                            ChangeOfHeart changeOfHeart = new ChangeOfHeart(
                                    mOrS[1], mOrS[2]);
                            temp.add(changeOfHeart);
                        } else if (mOrS[1].equals("Dark Hole")) {
                            DarkHole darkHole = new DarkHole(mOrS[1], mOrS[2]);
                            temp.add(darkHole);
                        } else if (mOrS[1].equals("Graceful Dice")) {
                            GracefulDice gracefulDice = new GracefulDice(
                                    mOrS[1], mOrS[2]);
                            temp.add(gracefulDice);
                        } else if (mOrS[1].equals("Harpie's Feather Duster")) {
                            HarpieFeatherDuster harpieFeatherDuster = new HarpieFeatherDuster(
                                    mOrS[1], mOrS[2]);
                            temp.add(harpieFeatherDuster);
                        } else if (mOrS[1].equals("Heavy Storm")) {
                            HeavyStorm heavyStorm = new HeavyStorm(mOrS[1],
                                    mOrS[2]);
                            temp.add(heavyStorm);
                        } else if (mOrS[1].equals("Mage Power")) {
                            MagePower magePower = new MagePower(mOrS[1],
                                    mOrS[2]);
                            temp.add(magePower);
                        } else if (mOrS[1].equals("Monster Reborn")) {
                            MonsterReborn monsterReborn = new MonsterReborn(
                                    mOrS[1], mOrS[2]);
                            temp.add(monsterReborn);
                        } else if (mOrS[1].equals("Pot of Greed")) {
                            PotOfGreed potOfGreed = new PotOfGreed(mOrS[1],
                                    mOrS[2]);
                            temp.add(potOfGreed);
                        } else if (mOrS[1].equals("Raigeki")) {
                            Raigeki raigeki = new Raigeki(mOrS[1], mOrS[2]);
                            temp.add(raigeki);
                        } else {
                            throw new UnknownSpellCardException(path,
                                    sourceLineNumber, mOrS[1]); // We have 10
                                                                // spells, if
                                                                // there is an
                                                                // unknown one
                                                                // we throw the
                                                                // exception
                        }
                    } // else of Spell code
                    else {
                        throw new UnknownCardTypeException(path,
                                sourceLineNumber, mOrS[0]); // We have two
                                                            // types, Monster
                                                            // and Spell.
                    }
                    sourceLineNumber++;
                }// While loop close
                br.close();
                return temp;
            }// try Close
            catch (FileNotFoundException exception) {
                if (i == 3) {
                    throw exception;
                }
                System.out
                        .println("The file was not found, Please enter a correct path:");
                path = sc.nextLine();
                //path = newPath;
            } catch (MissingFieldException exception) {
                if (i == 3) {
                    throw exception;
                }
                System.out.print("The file path: " + exception.getSourceFile()
                        + "At Line" + exception.getSourceLine()
                        + "Contians a missing Field");
                System.out.print("Enter New Path");
                path = sc.nextLine();
                //path = newPath;
            } catch (EmptyFieldException exception) {
                if (i == 3) {
                    throw exception;
                }
                System.out.println("The file path" + exception.getSourceFile()
                        + "At Line" + exception.getSourceLine() + "At field"
                        + exception.getSourceField()
                        + "Contains an Empty Field");
                System.out.println("Enter New Path");
                path = sc.nextLine();
                //path = newPath;
            } catch (UnknownCardTypeException exception) {
                if (i == 3) {
                    throw exception;
                }
                System.out.println("The file path:" + exception.getSourceFile()
                        + "At Line" + exception.getSourceLine()
                        + "Contains an Unknown Type"
                        + exception.getUnknownType());
                System.out.println("Enter New Path");
                path = sc.nextLine();
                //path = newPath;
            } catch (UnknownSpellCardException exception) {
                if (i == 3) {
                    throw exception;
                }
                System.out.println("The file Path" + exception.getSourceFile()
                        + "At Line" + exception.getSourceLine()
                        + "Contains an Unknown Spell"
                        + exception.getUnknownSpell());
                System.out.println("Enter New Path");
                path = sc.nextLine();
                //path = newPath;
            }
        } // For loop close
        ArrayList<Card> noHope = null;
        return noHope;
    }// Method Close

1 个答案:

答案 0 :(得分:0)

在分配hasNext()

之前,您应该使用path = sc.nextLine();

类似的东西: -

if (sc.hasNext()){
    path = sc.nextLine();
}
else{
    //print something else.
}

next

  

public String next()查找并返回下一个完整标记   这个扫描仪。之前是一个完整的标记,然后是输入   匹配分隔符模式。此方法可能会在等待时阻止   输入到扫描,即使先前调用hasNext()返回   真正。指定者:接口Iterator中的next返回:   next token抛出:NoSuchElementException - 如果没有更多的令牌   available IllegalStateException - 如果此扫描程序已关闭参见:   迭代器