Java - Switch Statement not functioning properly, System.out.println's mysteriously repeating -
seems basic problem, google nor have helped me resolve issue. made program out of sheer boredom allows user input string of text, , edit colour of said text using colour codes game minecraft. added advanced function allows user colourize each individual letter in given string - problems arise. @ end of advanced part of program, prompt user menu asks them want - return main part of program, restart advanced section, or exit program altogether. both options 1 , 3 work expected, when try restart advanced portion of program, strangely repeats menu on , on until select option. have rewritten how user's choice evaluated numerous times, rewrote plenty other sections of program, hoping logical error, no avail. here menu they're shown:
system.out.println("\n======= devtest terminated=======\n"); system.out.println("what do?"); system.out.println("1: return main program"); system.out.println("2: run devtest again"); system.out.println("3: exit program"); int useroption = input.nextint(); switch(useroption){ case 1: system.out.println(); start.main(null); break; case 2: devtests.rainbowtest(); break; case 3: system.exit(0); default: system.exit(0); break; }
when user enters "2" , hits enter, program runs first line of advanced program (prompting user enter string), instantly runs menu again. i've been looking on few hours now, , have yet come solutions. i've added entire advanced program @ end of post, guys can see that's going on, , before ask, no, primary part of program isn't interfering way.
any suggestions/comments appreciated.
note: realize of code isn't complete - please realize still in-progress, , until issue resolved, quite time. :p
import java.util.scanner; public class devtests { static scanner input = new scanner(system.in); public static void rainbowtest(){ system.out.println(); system.out.println("enter string:"); string userstring = input.nextline(); int stringlength = userstring.length(); system.out.println(); string rainbowstring = ""; for(int = 1; <= stringlength; i++){ system.out.println("enter colour #" + + ":"); system.out.println("black"); system.out.println("white"); system.out.println("blue"); system.out.println("dark blue"); system.out.println("green"); system.out.println("dark green"); system.out.println("aqua"); system.out.println("dark aqua"); system.out.println("red"); system.out.println("dark red"); system.out.println("purple"); system.out.println("dark purple"); system.out.println("gray"); system.out.println("dark gray"); system.out.println("yellow"); system.out.println("gold"); string usercolour2 = input.nextline(); system.out.println(); switch (usercolour2){ case "black": usercolour2 = "§0"; break; case "white": usercolour2 = "§f"; break; case "blue": usercolour2 = "§9"; break; case "dark blue": usercolour2 = "§1"; break; case "green": usercolour2 = "§a"; break; case "dark green": usercolour2 = "§2"; break; case "aqua": usercolour2 = "§b"; break; case "dark aqua": usercolour2 = "§3"; break; case "red": usercolour2 = "§c"; break; case "dark red": usercolour2 = "§4"; break; case "purple": usercolour2 = "§d"; break; case "dark purple": usercolour2 = "§5"; break; case "gray": usercolour2 = "§7"; break; case "dark gray": usercolour2 = "§8"; break; case "yellow": usercolour2 = "§e"; break; case "gold": usercolour2 = "§6"; break; default: system.out.println("please select colour. \n"); break; } rainbowstring += usercolour2 + userstring.charat(i - 1); } system.out.println(rainbowstring); system.out.println("\n======= devtest terminated=======\n"); system.out.println("what do?"); system.out.println("1: return main program"); system.out.println("2: run devtest again"); system.out.println("3: exit program"); int useroption = input.nextint(); switch(useroption){ case 1: system.out.println(); start.main(null); break; case 2: devtests.rainbowtest(); break; case 3: system.exit(0); default: system.exit(0); break; } } }
when using int useroption = input.nextint()
not read end-of-line symbol, it's being read next string userstring = input.nextline()
call. result empty userstring
, skip for
loop , go again last menu. fix use:
int useroption = input.nextint(); input.nextline();
Comments
Post a Comment