LimesKey / TrashSort

Computer Science Culminating class assignment in Java
2 stars 0 forks source link

NoSuchElementException, Java Scanner #1

Open LimesKey opened 5 months ago

LimesKey commented 5 months ago
PS C:\Users\Ryan\Desktop\Programming\Java\TrashSort\TrashSort\build\distributions\TrashSort\bin> .\TrashSort
WELCOME TRASH-SORTING-SIMULATOR!

Would you like to:
        1. Play the game
        2. Help
        3. Exit
Choice: 1
Please enter the amount of players:
1
Please enter the name for player 1:
1

Please choose your difficulty level, normal, hard or adaptive (either type in word or number)
Game Difficulty: 1
Where does Fruit go?
compost
Correct! You get a point!

Where does Eggshells go?
compost
Correct! You get a point!

Where does Plastic Packaging go?
recycle
Correct! You get a point!

Player 1 has a score of 3
Game Over! Elapsed Time: 5 seconds
Thank you for playing! Program ending...

Would you like to:
        1. Play the game
        2. Help
        3. Exit
Choice: Please enter the amount of players:
Exception in thread "main" java.util.NoSuchElementException: No line found
        at java.base/java.util.Scanner.nextLine(Scanner.java:1660)
        at trashsort.TrashSort.main(TrashSort.java:125)
PS C:\Users\Ryan\Desktop\Programming\Java\TrashSort\TrashSort\build\distributions\TrashSort\bin>
LimesKey commented 5 months ago

Also throws,

Exception in thread "main" java.lang.NumberFormatException: For input string: ""
        at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:67)
        at java.base/java.lang.Integer.parseInt(Integer.java:671)
        at java.base/java.lang.Integer.parseInt(Integer.java:777)
        at trashsort.TrashSort.main(TrashSort.java:72)
LimesKey commented 5 months ago

I fixed the NumberFormatException error with this code but the NoSuchElementException I haven't figured out yet,

            do {
                System.out.println("Please enter the amount of players: ");
                String player_amount_temp = scanner.nextLine();
                if (!player_amount_temp.isEmpty()) {
                    player_amount = Integer.parseInt(scanner.nextLine());
                }
                else {
                    player_amount = -5;
                }
            } while (player_amount < 0 || player_amount >= 10); // only allows 1 - 10 players
LimesKey commented 5 months ago

Looking more into this, it seems like a better alternative to the Java Scanner is a BufferedReader Console scanner, I will have to do more research though to compare the two.

maxwellmunro commented 4 months ago

Can you send the code?

LimesKey commented 4 months ago

Likely somewhere in this file https://github.com/LimesKey/TrashSort/blob/main/TrashSort/src/main/java/trashsort/TrashSort.java @OrinjCaike, search for scanS.nextline()