954,510 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Char arrays

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
jackmaverick1
Posting Whiz in Training
212 posts since May 2010
Reputation Points: 6
Solved Threads: 7
 

public String[10] name;

in Java is public char = 'a';

mKorbel
Veteran Poster
1,141 posts since Feb 2011
Reputation Points: 480
Solved Threads: 224
 

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

JamesCherrill
Posting Genius
Moderator
6,370 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: