简体   繁体   中英

Scanner Element Exception

I'm doing a simple Rock Paper Scissors program that allows the user to continue playing. I keep getting an Element Exception when it comes to the declaration of the repeatstring scanner —

import java.util.Scanner;
import java.util.Random;

public class RockPaperScissors {
    String ai = null;
    String player = null;
    static int playerscore=0, aiscore=0, ties=0, rounds=0;
    static String repeatstring = null;

    public static void main(String[] args) {
        boolean repeat = false;
        RockPaperScissors rps = new RockPaperScissors();
        System.out.println("Welcome to Rock Paper Scissors!\n");
        do {
            System.out.println("Choose your output! R - Rock, P - Paper, S - Scissors");
            rps.user();
            rps.comp();
            rps.game();
            System.out.println("YOU - " + playerscore + " / COMPUTER - " + aiscore);
            System.out.println("Rounds played - " + rounds + "\n");
            System.out.println("Do you wish to continue? Y/N");

            // Error reading string input... Solution pending.
            Scanner repeatinput = new Scanner(System.in);
            repeatstring = repeatinput.nextLine();
            if(repeatstring.equals("Y")) {
                repeat = true;
            }
            else if(repeatstring.equals("N")) {
                repeat = false;
            }
            repeatinput.close();

        } while (repeat == true); 
        System.exit(0);
    }

    void user() {
        Scanner scan = new Scanner(System.in);
        String input = scan.next();
        if(input.equals("R")) {
            player = "Rock";
        }
        else if (input.equals("P")) {
            player = "Paper";
        }
        else if (input.equals("S")) {
            player = "Scissors";
        }
        scan.close();
    }

    void comp() {
        Random rand = new Random();
        int randno = rand.nextInt((3 - 1) + 1) + 1;
        switch (randno) {
            case 1: ai = "Rock";
            case 2: ai = "Paper";
            case 3: ai = "Scissors";
        }
    }

    void game() {
        // If both player and AI has the same output -
        if(ai.equals(player)) {
            ties = ties + 1;
            System.out.println("You chose - " + player);
            System.out.println("Computer chose - " + ai);
            System.out.println("We have a TIE!");
        }

        // ROCK VS SCISSORS
        if(((ai.equals("Rock")) && (player.equals("Scissors"))) || (player.equals("Rock") && (ai.equals("Scissors")))) {
            if(ai.equals("Rock")) {
                aiscore = aiscore + 1;
                System.out.println("You chose - " + player);
                System.out.println("Computer chose - " + ai);
                System.out.println("The COMPUTER WINS!");
            }
            else {
                playerscore = playerscore + 1;
                System.out.println("You chose - " + player);
                System.out.println("Computer chose - " + ai);
                System.out.println("YOU WIN!");
            }
        }

        // ROCK VS PAPER
        if(((ai.equals("Rock")) && (player.equals("Paper"))) || (player.equals("Rock") && (ai.equals("Paper")))) {
            if(ai.equals("Paper")) {
                aiscore = aiscore + 1;
                System.out.println("You chose - " + player);
                System.out.println("Computer chose - " + ai);
                System.out.println("The COMPUTER WINS!");
            }
            else {
                playerscore = playerscore + 1;
                System.out.println("You chose - " + player);
                System.out.println("Computer chose - " + ai);
                System.out.println("YOU WIN!");
            }
        }

        // PAPER VS SCISSORS
        if(((ai.equals("Scissors")) && (player.equals("Paper"))) || (player.equals("Scissors") && (ai.equals("Paper")))) {
            if(ai.equals("Scissors")) {
                aiscore = aiscore + 1;
                System.out.println("You chose - " + player);
                System.out.println("Computer chose - " + ai);
                System.out.println("The COMPUTER WINS!");
            }
            else {
                playerscore = playerscore + 1;
                System.out.println("You chose - " + player);
                System.out.println("Computer chose - " + ai);
                System.out.println("YOU WIN!");
            }
        }
        rounds = rounds + 1;
    }
}

The program terminates itself after asking for a repeat input (Y/N) with the following error message:

Exception in thread "main" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Unknown Source)
at RockPaperScissors.main(RockPaperScissors.java:28)

Usually when you're getting an exception it's best to look up that exception online (unless you know what is throwing it). In this case if we lookup the exception you received we're directed to the Javadocs which state:

Thrown by the nextElement method of an Enumeration to indicate that there are no more elements in the enumeration.

This leads us to look at the Javadoc of Scanner to see if this is being thrown by nextLine() . We see that under the Throws section it says:

NoSuchElementException - if no line was found

What this means is that your program is trying to grab from a scanner that doesn't yet have a result for it. If you would like it to wait until there is a result then there are other methods available that can help your program to know when a result is available, specifically hasNextLine() . What this means is that you should check if a user has responded before trying to get that response.

As always, it's best to check the Javadocs whenever possible.

As an aside it might be best to not generate the scanner each time that the loop runs but rather declare it outside of the loop and then close it when you are done with it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM