-1

I am very much a beginner in Java. I am making a simple switch, in which the user is entering a number in words. For training I added a "for" loop.

package JavaExc;
import java.util.Scanner;


public class JavaStart {

    public static void main(String[] args) {
        Scanner sck = new Scanner(System.in);
        System.out.println("Type loop counter ");

        int counter = sck.nextInt();

        System.out.println("Counter is: " + counter );


        for (int i = 0; i <= counter; i++) {


            System.out.println("Type number in words");
            String ch = sck.nextLine();

            switch (ch.toLowerCase()) {


                case "one":
                    System.out.println("CHOICE 1");
                    break;
                case "Two":
                    System.out.println("CHOICE 2");
                    break;
                case "THREE":
                    System.out.println("CHOICE 3");
                    break;
                case "FoUr":
                    System.out.println("CHOICE 4");
                    break;
                case "fiVE":
                    System.out.println("CHOICE 5");
                    break;

                default:
                    System.out.println("Wrong Data");
                    break;

            }

            System.out.println("Loops left:\n " + counter--);
        }
        System.out.println("End of Switch");


    }
}

Here is the result:

Type loop counter 
5
Counter is: 5
Type number in words
Wrong Data                   // ??
Loops left:
5
Type number in words
one
CHOICE 1
Loops left:
4
Type number in words
three                        // Should be ok, so what is wrong here?
Wrong Data
Loops left:
3
End of Switch                //To early break loop I think

Process finished with exit code 0

My questions are: Why first loop make default code? Should I make 2 Scanners? One for take value for counter and second for switch? Why counter and numbers in words do not working properly?

I know I can do it with tables etc. but generally, the purpose of this program is to test the "toLowerCase()" method.

Wojciech Wirzbicki
  • 2,990
  • 4
  • 27
  • 47
Buckethead
  • 105
  • 2
  • 13

6 Answers6

1

You're checking your string in lower case ("three") against upper case ("THREE"). Make sure they're both identical and it shouldn't fail.

Coloco
  • 353
  • 4
  • 14
1

Switch statement

In your switch clause, you are testing a String that has been turned into a lowercase form of itself. Therefore, the only possible case clause that will be matched is "one", as the others all contain uppercase characters.

Change all of the case clauses to lowercase and it will work, as long as you input the data correctly.

case "one":
    System.out.println("CHOICE 1");
    break;
case "two":
    System.out.println("CHOICE 2");
    break;
case "three":
    System.out.println("CHOICE 3");
    break;
 case "four":
    System.out.println("CHOICE 4");
    break;
 case "five":
    System.out.println("CHOICE 5");
    break;

Not accepting input

Regarding your problem with the program not accepting input, you have to call sck.nextLine() after calling sck.nextInt(), see here for details on why this is.

Only one scanner is necessary.

Community
  • 1
  • 1
SamTebbs33
  • 4,921
  • 3
  • 19
  • 39
1

The convention of a switch statement is similar to an if-else if-else chain. The value placed between the switch statement's parenthesizes is evaluated against every case present in the switch statement.

A switch statement of:

String condition = "foo";

switch(condition){
    case "example":{
    }

    case "foo":{
    }

    case "bar":{
    }

    default:{
    }
}

is similar to the expression:

if(condition.equals("example")){

}else if(condition.equals("foo")){

}else if(condition.equals("bar")){

}else{

}

Note that by making your input into the switch statement lowercase, it will never match any case that is uppercase.

e.g:

if("tHREE".toLowerCase().equals("tHREE")){

will never evaluate true, since "tHREE" is first being dropped to all lowercase "three" before performing a case-sensitive comparison to "tHREE".

initramfs
  • 7,625
  • 2
  • 32
  • 56
  • Ok. I understand. I wanted to convert string no matter it is Upper or Lower case and try to match to cases. – Buckethead Apr 29 '15 at 17:29
  • @m4rtin77 continue with your toLowerCase() approach, but make sure every case in your code is lowercase as well. In that situation, a string that is originally mixed-case coming in will be converted to lowercase before being matched against one of the cases you provided. – initramfs Apr 29 '15 at 17:42
1

Why first loop make default code?

As explained in Skipping nextLine() after use next(), nextInt() or other nextFoo() methods, nextInt doesn't consume the newline character. Add a call to sck.nextLine(); after setting counter.

Should I make 2 Scanners? One for take value for counter and second for switch?

No, only one Scanner is necessary.

Why counter and numbers in words do not working properly?

First, your for loop condition is i <= counter, which means that it's set up to run counter + 1 times. Change it to i < counter.

Second, your switch cases except for "one" contain capital letters, so they will never match ch.toLowerCase(). Change your case labels to strings with all lowercase letters.

Third, you decrement counter at the end of the for loop, cutting down on the number of iterations run. Instead, use the expression (counter - i - 1) to output the number of loops left.

Community
  • 1
  • 1
rgettman
  • 167,281
  • 27
  • 248
  • 326
0

retype the cases to:

            case "one":
                System.out.println("CHOICE 1");
                break;
            case "two":
                System.out.println("CHOICE 2");
                break;
            case "three":
                System.out.println("CHOICE 3");
                break;
            case "four":
                System.out.println("CHOICE 4");
                break;
            case "five":
                System.out.println("CHOICE 5");
                break;
fatCop
  • 2,218
  • 9
  • 31
  • 50
0

Ok i added changes and it works fine. Now I will try to make possibilty to input words whatever they are upper or lower cases. Thanks for help! StackOverflow rocks so much!

package JavaExc;
import java.util.Scanner;

public class JavaStart {

public static void main(String[] args) {
    Scanner sck = new Scanner(System.in);
    System.out.println("Type loop counter ");

    int counter = sck.nextInt();
    sck.nextLine(); 
    System.out.println("Counter is: " + counter );

    int i;
    for ( i = 0; i < counter; i++) {


        System.out.println("Type number in words");
        String ch = sck.nextLine();

        switch (ch.toLowerCase()) {


            case "one":
                System.out.println("CHOICE 1");
                break;
            case "two":
                System.out.println("CHOICE 2");
                break;
            case "three":
                System.out.println("CHOICE 3");
                break;
            case "four":
                System.out.println("CHOICE 4");
                break;
            case "five":
                System.out.println("CHOICE 5");
                break;

            default:
                System.out.println("Wrong Data");
                break;

        }

        System.out.println("Loops left:\n " + (counter - i - 1)  );

    }
    System.out.println("End of Switch");


}}
Buckethead
  • 105
  • 2
  • 13