Hi, I just started Java (But I do have 1.5 years in c++) and I found that string switchs aren't allowed, so I tried using a character array, now when I try to define the array, it says that char [10 for example]array; is not allowed because of [10 for example]. (in eclipse, the red squiggle appears)

This is the problem source:

public char[10] name;//this is the problem

Recommended Answers

All 2 Replies

public String[10] name;

in Java is public char = 'a';

Hi jackmaverick
Not being able to do a switch on Strings is a well known shortcoming of Java and (at last!) its fixed in Java 7 - which will be out later this year. You can switch on a single char because a Java char is a numeric type (16 bit integer) - but not on an array of them.
But either way, if you want to do a switch on a String then you probably should be using an enum anyway - your set of valid Strings must be fixed and known at compile time, so an enum would be the right construct to use and (bet you saw this coming) you can switch on an enum.
Have a look at enums and if you have any questions come back here..

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.