Getting java.util.InputMismatchException? -
so haven't worked java in while , trying out codingame.com see still remembered. doing tutorial level, has code:
import java.util.*; import java.io.*; import java.math.*; /** * code below read game information you. * on each game turn, information available on standard input, sent: * -> total number of visible enemies * -> each enemy, name , distance * system wait write enemy name on standard output. * once have designated target: * -> cannon shoot * -> enemies move * -> new info available read on standard input. **/ class player { public static void main(string args[]) { scanner in = new scanner(system.in); // game loop while (true) { int count = in.nextint(); // number of current enemy ships within range string[] enemy = new string[count]; int[] dist = new int[count]; string closeenemy = ""; (int = 0; < count; i++) { enemy[i] = in.next(); // name of enemy dist[i] = in.nextint(); // distance cannon of enemy } for(int j = 0; j <= count - 1; j++){ if(dist[j] > dist[j+1] ){ closeenemy = enemy[j]; } else{ closeenemy = enemy[j+2]; } } // write action using system.out.println() // debug: system.err.println("debug messages..."); system.out.println(closeenemy); // name of threatening enemy (hotdroid 1 example) } } }
and need assuming count 2, need check enemy closer comparing dist
variables diferent enemies.
what did this:
import java.util.*; import java.io.*; import java.math.*; /** * code below read game information you. * on each game turn, information available on standard input, sent: * -> total number of visible enemies * -> each enemy, name , distance * system wait write enemy name on standard output. * once have designated target: * -> cannon shoot * -> enemies move * -> new info available read on standard input. **/ class player { public static void main(string args[]) { scanner in = new scanner(system.in); // game loop while (true) { int count = in.nextint(); // number of current enemy ships within range string[] enemy = new string[count]; int[] dist = new int[count]; string closeenemy = ""; (int = 0; < count; i++) { enemy[i] = in.next(); // name of enemy dist[i] = in.nextint(); // distance cannon of enemy } for(int j = 0; j <= count - 1; j++){ if(dist[j] > dist[j+1] ){ closeenemy = enemy[j]; } else{ closeenemy = enemy[j+2]; } } // write action using system.out.println() // debug: system.err.println("debug messages..."); system.out.println(closeenemy); // name of threatening enemy (hotdroid 1 example) } } }
i tried make work amount of enemies (for count user insert), when test gives me following error:
exception in thread "main" java.util.inputmismatchexception @ player.main on line 24
i have checked , can't find mistake , appreciate on this.
you inputting string
or double
when program expecting int
. following input works without throwing inputmismatchexception
:
2 enemy1 1 enemy2 2
after array out of bounds exceptions can fixed with:
for(int j = 0; j < count - 1; j++){ if(dist[j] > dist[j+1] ){ closeenemy = enemy[j]; } else { closeenemy = enemy[j+1]; } }
Comments
Post a Comment