user3185727
user3185727

Reputation: 159

JAVA: Why do I keep getting these runtime errors?

Wrote a roshambo program, ran it once, worked fine, ran it again and the following errors popped up:

RoShamBo!

  1. Play Game
  2. Show Score
  3. Quit

1

Which do you choose?

  1. Rock
  2. Paper
  3. Scissors

2

Which do you choose?

  1. Rock
  2. Paper
  3. Scissors

Invalid Entry, Please Try Again.

Exception in thread "main" java.util.NoSuchElementException

at java.util.Scanner.throwFor(Unknown Source)

at java.util.Scanner.next(Unknown Source)

at RoShamBo.getUserChoice(RoShamBo.java:82)

at RoShamBo.winner(RoShamBo.java:112)


at RoShamBo.main(RoShamBo.java:27)

not sure on how to deal with these type of errors, this my first time using methods so i'm thinking it has to do with how I called each method? please help.

thanks in advance.

import java.util.Scanner;

public class RoShamBo 
{
public static void main(String[] args)
{
    System.out.println("RoShamBo!");
    System.out.println("1. Play Game");
    System.out.println("2. Show Score");
    System.out.println("3. Quit");
    Scanner userInput = new Scanner(System.in);

    if(userInput.hasNextInt())
    {
        int userIn = userInput.nextInt();
        if(userIn == 1)
        {
            getUserChoice();
            getCompChoice();
            winner();
        }
        else if(userIn == 2)
        {
            scores();
        }
        else if(userIn == 3)
        {
            System.out.println("Qutiing: Final Score was: ");
            scores();
            userInput.close();
        }
        else
        {
            System.out.println("Invalid Entry, Please Try Again.");
            userInput.next();
        }
    }
    else
    {
        System.out.println("Invalid Entry, Please Try Again.");
        userInput.next();
    }
}      
public static String getUserChoice()
{
    // ask player for a move : playerMove
    System.out.println("Which do you choose?");
    System.out.println("1. Rock");
    System.out.println("2. Paper");
    System.out.println("3. Scissors");

    Scanner input = new Scanner(System.in);
    String userChoice = " ";

    if (input.hasNextInt())
    {
        int userInput = input.nextInt();
        switch(userInput)
        {
            case 1:
                    userChoice = "Rock";
                    break;
            case 2:
                    userChoice = "Paper";
                    break;
            case 3:
                    userChoice = "Scissors";
                    break;
        }

    }
    else
    {
        System.out.println("Invalid Entry, Please Try Again.");
        input.next();
    }
    input.close();
    return userChoice;
}
private static String getCompChoice()
{
    //Method for getting Computers move
    int compChoice = (int) ( 1 + (Math.random() * 3));
    String compMove = " ";
    switch(compChoice)
    {
        case 1:
                compMove = "Rock";
                break;
        case 2:
                compMove = "Paper";
                break;
        case 3:
                compMove = "Scissors";
                break;
    }

    return compMove;
}

public static String winner()
{
    String winnerIs = " ";
    String comp = getCompChoice();
    String user = getUserChoice();
    if(user.equals("Rock") && comp.equals("Scissors") ||
       user.equals("Paper") && comp.equals("Rock") ||
       user.equals("Scissors") && comp.equals("Paper"))
    {          
        System.out.println("You Win!");
    }
    else
    {
        System.out.println("You Lose");
    }
    return winnerIs;
}
public static void scores()
{
    int compCount = 0;
    int userCount = 0;
    if (winner().equals("You Win!"))
    {
        userCount++;
    }
    else
    {
        compCount++;
    }
    System.out.println("Player = " + userCount );
    System.out.println("Computer = " + compCount );
}
}

Upvotes: 0

Views: 91

Answers (2)

zubergu
zubergu

Reputation: 3706

Your stack gives one hint.

at java.util.Scanner.next(Unknown Source).

and

Exception in thread "main" java.util.NoSuchElementException

Turning to the documentation here:

http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#next%28%29

part that is of interest to you:

Throws: NoSuchElementException - if no more tokens are available

You call .next() but what do you want to read? Is there anything waiting in input buffer? Also, see my comment, meaning that your logic is busted.Rethink your game logic instead of fixing it.

Giving a complete answer that gets your code to run like it's supposed to would mean writing most of your code, I won't go that way. Instead, you should apply one of the most powerful debugging methods known to humans and do it yourself:

https://en.wikipedia.org/wiki/Rubber_duck_debugging

Good luck and enjoy ;)

Upvotes: 2

PeterMmm
PeterMmm

Reputation: 24630

Probably here

 {
        System.out.println("Invalid Entry, Please Try Again.");
        input.next();
    }

you are doing a next() without checking for has*().

Upvotes: 2

Related Questions