0

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

Edited by jackmaverick1: n/a

3
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by JamesCherrill
0

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..

Edited by JamesCherrill: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.