Click here to Skip to main content
15,896,730 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have the below code:

Where do i wrong?

I am confused because i is char.

What I have tried:

Java
public class SwitchChar {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        char i;
        LOOP:
        for (i = 0; i < 5; i++) {
            System.out.println((int)i);
            switch (i++) {
                case '0':
                    System.out.println((int)i);
                    System.out.println("A");
                case 1:
                    System.out.println((int)i);
                    System.out.println("B");
                    break LOOP;
                case 2:
                    System.out.println((int)i);
                    System.out.println("C");
                    break;
                case 3:
                    System.out.println((int)i);
                    System.out.println("D");
                    break;
                case 4:
                    System.out.println((int)i);
                    System.out.println("E");
                case 'E':
                    System.out.println((int)i);
                    System.out.println("F");
            }
        }
    }

}


I don't understand the steps who conduct to the result.

when i = 0 - no case to select, i is incremented to 1
when i = 1 - break loop so for is never executed



Thank a lot!!!
Posted
Updated 20-Feb-17 11:38am
v2

You do understand the common fact that zero is not same in integer form, and character form. Their binary, that gets generated and checked up against is not same. A character is a switch-able value, but it will not map any true value for the cases that you are using it for.

To solve this problem, change one of the types to other one,
for (i = 0; i < 5; i++) {
    System.out.println((int)i);
        switch (i++) {
            case 0:
            // Code here.
            // ...

Otherwise, you should consider using character values, and use Character.valueOf(i); and then move onwards. That would look something like this,
for (i = 0; i < 5; i++) {
     // Map it.
     char ch = Character.valueOf(i);
     System.out.println(i); // No need of (int)int; it is already an int.
         switch (ch++) {
             case '0':
             // code here...

Doing the ch++ is legal in Java, because a character is a numeric value that represents the Unicode character. Incrementing it, provides you with the next character and so on. If that doesn't sound easy, read about pointer arithmetic.
 
Share this answer
 
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900