sam
sam

Reputation: 19184

convert string to char with switch case

I am taking input from user in string and I want to iterate and test using case statement but it is not working. its not printing the statements.

import java.io.*;
import java.util.*;

public class fh3

{
    public static void main(String args[])throws IOException

{

    String sentence = "";

    System.out.println("Enter the word : ");
    Scanner scan = new Scanner(System.in);
    String word = scan.next();


    char[] chars = word.toCharArray(); 

    for(int i = 0; i < word.length(); i++)
    {

        System.out.println("---" + chars[i]);
        switch(chars[i])
        {
            case 0: sentence = " ";
                System.out.println("B");
                break;
            case 1: sentence = "A";
                break;
            case 2: sentence = "B";
                System.out.println("B");
                break;
            case 3: sentence = "C";
                break;


        }
        sentence+=sentence;
    System.out.println(sentence);
    }


}

}

if i enter 20 den it should print"B " but its printing as

Enter the word :
20
---2

---0

where i am getting wrong?

Upvotes: 1

Views: 2221

Answers (5)

arjun
arjun

Reputation: 73

import java.io.IOException;
import java.util.Scanner;

public class Fh3 {
    public static void main(String args[]) throws IOException {

        String sentence = "";
        System.out.println("Enter the word : ");
        Scanner scan = new Scanner(System.in);
        String word = scan.next();

        //Switch case needs you to compare the expression with constants hence the final keyword. 
        final char CHARONE = '1';
        final char CHARTWO = '2';
        final char CHARTHREE = '3';
        final char CHARFOUR = '4';

        char[] chars = word.toCharArray();

        for (int i = 0; i < word.length(); i++) {
            System.out.println("---" + chars[i]);
            switch (chars[i]) {
                case 0:
                    sentence = " ";
                    System.out.println("B");
                    break;
                case CHARONE:
                    sentence = "A";
                    break;
                case CHARTWO:
                    sentence = "B";
                    System.out.println("B");
                    break;
                case CHARTHREE:
                    sentence = "C";
                    break;

            }
            sentence += sentence;
            System.out.println(sentence);
        }
    }
}

You were trying to compare int with char .. Clear ?

Upvotes: 2

Jean Logeart
Jean Logeart

Reputation: 53839

In Java, the char type maps to the int type via the Ascii table.

Therefore, if you want to check the char '0' and not the NUL char, you should do:

switch(chars[i]) {
    case '0': // do the work
    case '1': // do the work
    // ...
}

Upvotes: 1

SpringLearner
SpringLearner

Reputation: 13854

your switch is accepting char but no suitable case is there.So its printing only this statement System.out.println("---" + chars[i]); two times(because word.length() returns 2 in your case)

import java.io.*;
import java.util.*;

public class fh3

{
    public static void main(String args[])throws IOException

{

    String sentence = "";

    System.out.println("Enter the word : ");
    Scanner scan = new Scanner(System.in);
    String word = scan.next();


    char[] chars = word.toCharArray(); 

    for(int i = 0; i < word.length(); i++)
    {

        System.out.println("---" + chars[i]);
        switch(chars[i])
        {
            case '0': sentence = " ";
                System.out.println("B");
                break;
            case '1': sentence = "A";
                break;
            case '2': sentence = "B";
                System.out.println("B");
                break;
            case '3': sentence = "C";
                break;


        }
        sentence+=sentence;
    System.out.println(sentence);
    }


}

}

Upvotes: 1

Rahul
Rahul

Reputation: 45070

Since you're doing the switch on char type, your case should have the same. In your case, since you give the case as integer values, its just not matching. '0' is not equal to 0

switch(chars[i]) {
    case '0': // switch on char '0' and not integer 0.
    case '1': // switch on char '1' and not integer 1.
    case '2': // switch on char '2' and not integer 2.
    ...
}

Upvotes: 3

Alexis C.
Alexis C.

Reputation: 93872

Because you're switching on characters, not integers :

switch(chars[i]){
    case '0': sentence = " ";
           System.out.println("B");
           break;
    case '1': sentence = "A";
           break;
    case '2': sentence = "B";
           System.out.println("B");
           break;
     case '3': sentence = "C";
           break;
}

Upvotes: 1

Related Questions