###質問内容
標準入力を受け取ろうとScanner を用いてnextLine()をしようとしたらjava.util.NoSuchElementException
と例外がでてしまう.
どのようにScannerの部分を直せば正しく入力を受け取ってくれますか.
次に示すようなサイトを見たりしてみたのですが,よく対処の仕方が分からなかったので質問させていただきました.
リンク内容
###例外本文
Exception in thread "main" java.util.NoSuchElementException: No line found at java.base/java.util.Scanner.nextLine(Scanner.java:1651) at ClientMain.main(ClientMain.java:53)
###ClientMain
Java
1import java.io.*; 2import java.util.*; 3 4public class ClientMain { 5 public static void main(String[] args) throws InterruptedException{ 6 Player player = new Player("", 0, 0); //(name, score, mdoe) 7 GameData gameData = new GameData(player); 8 ClientSystem clientsystem = new ClientSystem(); 9 Requester requester = new Requester(); 10 11 clientsystem.opening(); //op 12 13 clientsystem.say("Choose difficulty from easy:1 or hard:2."); 14 clientsystem.say("Input the difficulty number."); 15 Scanner scan = new Scanner(System.in); 16 int difficulty = scan.nextInt(); 17 scan.close(); 18 player.setMode(difficulty); 19 requester.sendDifficulty(player); 20 21 clientsystem.say("send difficulty"); 22 clientsystem.say("gamedata download..."); 23 Thread.sleep(1000); 24 25 requester.requestGameData(gameData); //download gamedata from server 26 clientsystem.say("Load Successes!"); 27 Thread.sleep(2000); 28 29 clientsystem.startGame(gameData); //gamestart 30 System.out.println(); 31 clientsystem.say("Finish"); 32 33 clientsystem.showScore(player); //show score 34 35 36 clientsystem.say("Please input your name."); 37 Scanner sc = new Scanner(System.in); 38 String playerName = sc.nextLine(); 39 sc.close(); 40 player.setName(playerName); 41 42 requester.sendScore(player); 43 44 clientsystem.say("See you!"); 45 } 46}
###実行結果
PS C:\Users\Client> java ClientMain [Welcome to this TYPING GAME.] [When this system says something, uses [] like this message] [--rule--] [Type the message enclosed in {}.] [The next is a example.] Q.{ もんだいぶん } [if you misstype and enter without modifying, subtract your score.] [There are two difficulty levels] [Choose difficulty from easy:1 or hard:2.] [Input the difficulty number.] 1 [send difficulty] [gamedata download...] [Load Successes!] [Are you ready?] [START!] [Finish] [Score: 0] [Please input your name.] Exception in thread "main" java.util.NoSuchElementException: No line found at java.base/java.util.Scanner.nextLine(Scanner.java:1651) at ClientMain.main(ClientMain.java:38)
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/06/28 13:19