Hey guys! So I'm writing a program that takes in a user input (1-3) and then executes the corresponding method. I keep getting the wrong output. For example, for prob1 when I input "john doe" it prints "Unauthorized!". And I'm like, foolish computer! I am your MASTER! How can I solve this? Thanks guys!

 public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        System.out.print("Enter problem number: ");
        int probNum = s.nextInt();
        String probName; 
        switch (probNum){
            case 1: probName = "Case Sensitive Problem";
                System.out.print("Input: ");
                String strName = s.next(); 


                  public static String prob1(String strName){
        String name = "john doe";
        if (strName.equalsIgnoreCase(name))
            System.out.print("Welcome John Doe!");

            return strName;
2 Years
Discussion Span
Last Post by JamesCherrill

The quickest thing to do would have been to print strName just before the if test, which would have revealed the problem without waiting for a DaniWeb response. But anyway...

That code fragment seems a bit scrambled, but it looks like you have a scanner problem. I hate Scanner. A default scanner will read one word when you call next() - ie "john". For multi-word input you need nextLine().
But then there's a gotcha:

You have some input with an int followed by some text, eg
John Doe
... and you try to read it with
int num = scanner.nextInt();
String name = scanner.nextLine();
... and name is an empty String ("")!

Here's why:
Your input looks like this with the new line characters shown explicitly
101\nJohn Doe\n
nextInt takes the int value from the scanner, and stops when it finds a chaacter that's not part of an int, so it takes the "101", leaving this in the scanner
"\nJohn Doe\n"
then nextLine takes everything up to the first \n character - a zero-length String ("").

Possible fixes:
1. Add add extra nextLine() between the nextInt and the real nextLine to clear the unwanted \n. This may be difficult if the nextInt and the nextLine are in different areas of code which are not always executed together.
2. Give up on nextInt (etc) and just read whole lines and parse then into ints (etc) with Integer.parseInt (etc) - in which case you can junk the whole scanner and use a BufferedReader instead. new BufferedReader(new InputStreamReader(System.in))


Thanks you! I finally got the first one working! Here's what I did:

  switch (probNum){
            case 1: probName = "Case Sensitive ";      
                System.out.print("Input: ");
                String strName = s.nextLine(); 


I added s.nextLine();to clear the content inside Scanner. Altough, when I applied the same line of code to my other method, the expected output is an undesirable. Here's what I have:

            case 2: probName = "Replace all vowels ";
                System.out.print("Input: ");
                String strInput = s.nextLine();

                  public static String prob2(String strInput){

        strInput = strInput.replaceAll("[aeiou]", "e");
        strInput = strInput.replaceAll("[AEIOU]", "E");

        return strInput;

Nothing happens when I input a word or phrase. I added the s.nextLine(); but doesn't work. Could this be a Scanner problem again?


If that's all the relevant code then prob2 does the replacements and returns the result, which you then do nothing with. Somewghere you need to print that result!

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.