How to use switch statement on second letter of a string in Java

2 Answers

0 votes
package javaapplication1;

public class JavaApplication1 {

    public static void main(String[] args) {

        try {

            String value = "jx";

            switch (value.charAt(0)) {
                case 'j':
                    switch (value.charAt(1)) {
                        case 'x':
                            System.out.println("second letter is 'x'");
                            break;
                        default: {
                            System.out.println("second letter is not 'x'");
                        }
                    }
                    break;
                case 'a':
                    System.out.println("x");
                    break;
                case 'b':
                    System.out.println("y");
                    break;
                default: {
                    System.out.println("first letter is not 'j', 'a' or 'b'");
                }
            }

        } catch (Exception e) {
            System.out.print(e.toString());
        }
    }
}

/*
             
run:

second letter is 'x'
    
 */

 



answered Nov 28, 2016 by avibootz
0 votes
package javaapplication1;

public class JavaApplication1 {

    public static void main(String[] args) {

        try {

            String value = "jx";

            switch (value.charAt(0)) {
                case 'j':
                    switch (value.charAt(1)) {
                        case 'x':
                            System.out.println("second letter is 'x'");
                            break;
                        default: {
                            System.out.println("second letter is not 'x'");
                        }
                    }
                    break;
                default: {
                    System.out.println("first letter is not 'j'");
                }
            }

        } catch (Exception e) {
            System.out.print(e.toString());
        }
    }
}

/*
             
run:

second letter is 'x'
    
 */

 



answered Nov 28, 2016 by avibootz

Related questions

1 answer 206 views
1 answer 87 views
1 answer 156 views
1 answer 153 views
2 answers 235 views
8 answers 926 views
926 views asked Jan 14, 2016 by avibootz
...