0

Seems like a basic problem, but Google nor SO have helped me resolve this issue. I made a program out of sheer boredom that allows the user to input a string of text, and edit the colour of said text using colour codes for the game Minecraft. I also added an advanced function that allows the user to colourize each individual letter in a given string - this is where the problems arise. At the end of the advanced part of the program, I prompt the user with a menu that asks them what they want to do - return to the main part of the program, restart the advanced section, or exit the program altogether. Both options 1 and 3 work as expected, but when you try to restart the advanced portion of the program, it strangely repeats the menu over and over until you select another option. I have rewritten how the user's choice is evaluated numerous times, as well as rewrote plenty other sections of the program, hoping it was just some logical error, but to no avail. Here is the menu they're shown:

    System.out.println("\n======= DevTest TERMINATED=======\n");
    System.out.println("What would you like to do?");
    System.out.println("1: Return to 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 the user enters "2" and hits Enter, the program runs the first line of the advanced program (prompting the user to enter a string), but then instantly runs this menu again. I've been looking over this for a few hours now, and have yet to come to any solutions. I've added the entire advanced program at the end of this post, so you guys can see everything that's going on, and before you ask, no, the primary part of the program isn't interfering with this is any way.

Any suggestions/comments would be appreciated.

NOTE: I realize some of the code isn't complete - please realize that this is still in-progress, and until this issue is resolved, will be for quite some 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 a string:");
    String userString = input.nextLine();

    int stringLength = userString.length();

    System.out.println();

    String rainbowString = "";

    for(int i = 1; i <= stringLength; i++){
        System.out.println("Enter colour #" + i + ":");
        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 a colour. \n");
            break;
        }

        rainbowString += userColour2 + userString.charAt(i - 1);
    }

    System.out.println(rainbowString);

    System.out.println("\n======= DevTest TERMINATED=======\n");
    System.out.println("What would you like to do?");
    System.out.println("1: Return to 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;
        }
    }
}
  • 3
    possible duplicate of [Skipping nextLine() after using next(), nextInt() or other nextFoo() methods](http://stackoverflow.com/questions/13102045/skipping-nextline-after-using-next-nextint-or-other-nextfoo-methods) – RealSkeptic Jul 14 '15 at 09:05
  • What would happen if you pass the `scanner` in your main class to the `rainbowTest` method? – npinti Jul 14 '15 at 09:06

2 Answers2

1

When you are using int userOption = input.nextInt() it actually does not read the end-of-line symbol, thus it's being read by next String userString = input.nextLine() call. As a result you get empty userString, so you skip the for loop and go again to the last menu. To fix this use:

int userOption = input.nextInt();
input.nextLine();
Tagir Valeev
  • 87,515
  • 18
  • 194
  • 305
  • Wooow. Can't believe it was that simple. Glad it took me three hours for that -_- All is well now, though. Thank you for explaining. – Darian Kuhn Jul 14 '15 at 09:16
0

you are calling the function rainbowTest() recursive in the case of user input 2.

And after calling the function rainbowTest() you are printing the meu again ;)

Coder55
  • 544
  • 4
  • 17