Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9149165
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:26:24+00:00 2026-06-17T11:26:24+00:00

Hey guys I don’t get why I get an exception in this simple code

  • 0

Hey guys I don’t get why I get an exception in this simple code
I’m trying to make a card game, and try to read the users wanted index of the card in his hand (it’s in German)

public Karte abschlagen(Karte gegnK) {
    System.out
            .println("Bitte die Zahl für die jeweilige Karte auswählen: ");
    gibHandAus(); // prints the users cards with their indexes 

    try {
        Scanner sc = new Scanner(System.in);
        int index = sc.nextInt();
        if (index < 0 || index >= getHand().size()) {
            System.out
                    .println("Bitte nur eine der gegebenen Zahlen auswählen!");
                     sc.close();
            return abschlagen(gegnK);
        }
        Karte k = getHand().get(index);
        getHand().remove(index);
        sc.close();

So what’s the problem here?
Before I can choose a number, it throws the noSuchElement exception.
I read in another question that the close() method can be causing this, but it still did it without it.

The error message is:

Exception in thread "main" java.util.NoSuchElementException
   at java.util.Scanner.throwFor(Unknown Source) 
   at java.util.Scanner.next(Unknown Source) 
   at java.util.Scanner.nextInt(Unknown Source) 
   at java.util.Scanner.nextInt(Unknown Source) 
   at durak.Spiel.spielerWehrtAb(Spiel.java:229) 
   at durak.Spiel.main(Spiel.java:314) 

EDIT: Code of spielerWehrAb():

public static boolean spielerWehrtAb(Karte k) {
.
.
.
try {
Scanner sc = new Scanner(System.in);
int x = sc.nextInt();
sc.close();

if (x == 0) {
... }
else if(x == 1) {
System.out.println("1 ausgewählt");
Karte k2 = spieler.abschlagen(k);
....
} 
else return spielerWehrAb(k);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T11:26:25+00:00Added an answer on June 17, 2026 at 11:26 am

    The problem is Scanner.close(); closes the underlying stream. The second time, you’re trying to read from a closed stream, so it obviously fails.

    Please see the tests I designed to demonstrate this behavior here: java.util.Scanner strange behaviour when used with System.in

    The solution would be not to read after closing the Scanner. You could, for example, define the Scanner as a class member and always use that member.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey guys I feel like this is a simple question, but I don't know
Hey guys this is my html code: <div class=nakupy> <li class=icn_kategorie><a href=#>Nákupy</a> <div class=sub_menu>
hey guys having this really simple problem but cant seem to figure out have
Hey guys I've been trying all day to get an ant file to automatically
Hey guys I'm trying to make it so when you click on the existing
hey guys, how can I just achieve this simple layout? I'm hoping the answer
Hey guys, i'm not much of a hardcore coder and so I don't get
Hey guys, I'm trying to make a puzzle bubble clone but I'm stumped on
hey guys, don't get it... I want to show a bar at the top
hey guys, this works fine: $dir = new DirectoryIterator(get_template_directory().'/images/headers/'); however I don't have just

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.