java.util.scanner - Java Scanner ignores answer -


hi it's first post here , have problem:

i have program in which, @ point, asks user if wants share stars, , if supposedly does, program goes collecting them , after time comes question if wants share them again.

the problem that, when user comes point, program ignores whatever answer u give , goes "else answer block".

it looks this:

"do want share stars?  yes  please answer yes or no" 

code:

import java.util.random; import java.util.scanner;  public class app { static scanner skan = new scanner(system.in); static int starcount = 0; static random random = new random();  public static void main(string[] args) {     starreaching();  }  static void starreaching() {     boolean starcollected = false;     int = 0;     int j = random.nextint(101);      while (i < j) {         i++;         system.out.println("stars out of reach");     }     if (i > j || == j) {         starcollected = true;     }     if (starcollected == true) {         starcollector();     } }  static void starcollector() {     system.out.println("you caught star !");     starcount++;     if (starcount == 10) {         system.out.println("you have 10 stars ! :)");         system.out.println("do want share stars?");         string line = skan.nextline();         if (line.equals("yes")) {             skan.reset();             stargiver();         } else if (line.equals("no")) {             wishmaker();         } else {             system.out.println("please answer yes or no");             system.exit(0);          }     } else {         starreaching();     } }  static void stargiver() {     system.out.println("how many want share?");     int starstoshare = skan.nextint();     if (starstoshare < 10 || starstoshare == 10 && starstoshare > 0) {         starcount = starcount - starstoshare;         system.out.println("stars shared !");         system.out.println("you have " + starcount + " stars");         system.out.println("go collect them again!");         starreaching();     } else if (starstoshare > 10) {         system.out.println("you don't have enough stars share much!");         stargiver();     } else {         system.out.println("that's not valid option");         stargiver();     } }  static void wishmaker() {  }  } 

every time call skan.nextline() read new line , advance scanner's pointer, , reason code failing: you're calling often.

instead of

if (skan.nextline().equals("yes")) {     skan.reset();     stargiver(); } else if (skan.nextline().equals("no")) {     wishmaker(); } else { 

do:

string line = scan.nextline(); // read scanner **once** if (line.equals("yes")) {     skan.reset();     stargiver(); } else if (line.equals("no")) {     wishmaker(); } else { 

Comments

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -