poker - Exception in thread "main" java.util.NoSuchElementException Scanner error -
i getting error , cannot figure out how fix it. class tutor couldn't figure out. poker program deals out hand, allows user replace 4 cards, , analyzes it. designed allow user play many hands want, scanner input decides if play or not isn't working , can't figure out why.
here main code
import java.util.*; public class poker { public static void main(string[] args) { string game = "continue"; run runpoker = new run(); system.out.println("enter 2 play hand or 1 quit game: "); while (game == "continue") { game = ((run)runpoker).playhand();//this line 18 } } }
here method playhand calling, real main code
import java.util.scanner; public class run { public string playhand() { int r = 1; int limit = 0; // create new, shuffled deck deck d = new deck(); hand h = d.deal(); h.sort(); system.out.println(h); system.out.println("which cards replace, 4"); system.out.println("enter 0 when finished"); scanner in = new scanner(system.in); r = in.nextint(); while (r > 0 && r < 6 && limit < 4) { if (r > 0) h.replace(d, r - 1); limit++; r = in.nextint(); } h.sort(); system.out.println(h); string result = h.analyze(); system.out.println(result); string answer; system.out.println("would play again? enter 'continue' play again or 'quit' stop playing: "); in.close(); scanner input = new scanner(system.in); answer = input.nextline();//this line 34 input.close(); return answer; } }
there other classes shouldn't affect scanner. here error
exception in thread "main" java.util.nosuchelementexception: no line found @ java.util.scanner.nextline(unknown source) @ run.playhand(run.java:34) @ poker.main(poker.java:18)
the intial carriage return pressed after
system.out.println("enter 2 play hand or 1 quit game: "); while ((game = scan.nextint()) == 2)
is never consumed , still on input when playhand
called.
so before calling playhand
call scan.nextline();
Comments
Post a Comment