guys me trying to use switch statement in this program but cant get to end...
i tryed to convert string into char
but dont knw why it still not getting right...
please help me

import javax.swing.*;
import java.io.*;




 class HappyBirthday {

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

            {

                     String  Month;
                 

                    char[] Months = Month.toCharArray(); 

                        BufferedReader dataIn= new BufferedReader(new InputStreamReader(System.in));

                        

                        System.out.println("Please enter your month.");





                        Month = JOptionPane.showInputDialog("enter month");
                         String month1={"January","feb"};
                         char[] month2 = month1.toCharArray(); 
                         
         

         //  String s=month1.equals(Month);
         

//System.out.print(month2Array[0]);
       switch (month2) {
        
        	case 0:
        		System.out.println("kool");
        		break;
        		
        		case 1:
        			System.out.println("not kool");
        			break;
        			default:
        }}}

guys me trying to use switch statement in this program but cant get to end...
i tryed to convert string into char
but dont knw why it still not getting right...
please help me.....

class HappyBirthday {

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

            {

                     String  Month;
                 

                    char[] Months = Month.toCharArray(); 

                        BufferedReader dataIn= new BufferedReader(new InputStreamReader(System.in));

                        

                        System.out.println("Please enter your month.");





                        Month = JOptionPane.showInputDialog("enter month");
                         String month1={"January","feb"};
                         char[] month2 = month1.toCharArray(); 
                         
         

         //  String s=month1.equals(Month);
         

//System.out.print(month2Array[0]);
       switch (month2) {
        
        	case 0:
        		System.out.println("kool");
        		break;
        		
        		case 1:
        			System.out.println("not kool");
        			break;
        			default:
        }}}

You can't do a switch on a char array. The Language Refeence says:
The type of the Expression must be char, byte, short, int, Character, Byte, Short, Integer, or an enum type.
THere was a recent thread here about doing a switch on String values - look it up.

ps: Doing a switch on a String is apparently planned for inclusion in Java 7 (maybe later this year?). We can only hope.

pps: IMHO a switch implies that the set of valid Strings is fixed and known at compile time - so they should be an enum anyway.

hi, I hope you are doing well, first of all: in my opinion you must take/make coding easier and clear, for example you don't have to mix the GUI and console mode ( in line 29 you wrote:

Month = JOptionPane.showInputDialog("enter month");

and again in same program in line 42 you wrote:

System.out.println("kool");

.

second: there is no need to declare a variable of type String/char while you will need to use it as an integer (

char[] month2 = month1.toCharArray();

here you declared a variable of char named month2... and then you are switching to month2:

switch (month2) {

...
...
remember that you Cannot switch on a value of type String/char. Only "int" values or "enum" constants are permitted...
and finally you are trying to use "month2" as an integer after declaring it as a char:

case 0:

...

case 1:

. I hope you are getting the point, so I wrote the code again and I ran it using eclipse Galileo. here is the new code for program:

import java.util.Scanner; 

public class HappyB {

	
	public static void main(String[] args) {
		
		//to read the input from the keyboard
		Scanner scn = new Scanner(System.in);
		
		int month;//the variable to switch 
		
		
		System.out.println("Enter the number of the month: ");
		
		//month is equal to user input
		month = scn.nextInt();
		
		/*Cannot switch on a value of type String. Only 
		 * convertible 
		*int values or enum constants are permitted
		*/
		switch(month)//switch to month variable
		{
		//the selection
		case 0:
			System.out.println("kool"); break;
		case 1:
			System.out.println("not kool"); break;
			default:
				System.out.println("out of context...");
		
		}

	}

}

thnx man for help...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.