Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 08-01-2007, 04:30 PM
Member
 
Join Date: Jul 2007
Posts: 40
baltimore is on a distinguished road
Problem with a switch statement in Java
I've been having this odd problem with a switch statement in Java.

The problem I'm having is that the switch statement doesn't seem to be recognizing the correct input. As an example, I'm checking for various characters, and i read them in as ints. The ASCII code for a ':' is 58, and when I test it like this:

Code:
if( input == 58){ do stuff; }
it works fine, but the same input in a switch statement doesn't do anything:
Code:
switch( input){ case 58: do stuff: break; }
Thanks.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 08-01-2007, 08:18 PM
Senior Member
 
Join Date: Jul 2007
Posts: 1,191
hardwired is on a distinguished road
According to this test/exploration your code should work. I wonder if the variable input could be something other than a char.
Code:
public class Test { public static void main(String[] args) { char[] chars = { 'a', 'b', 'c', 'd' }; explore(chars); String s = ""; for(int j = 0; j < chars.length; j++) { char c = chars[j]; switch(c) { case 'a': // both seem to // case 97: // work okay s = "a"; break; case 'b': // case 98: s = "b"; break; case 'c': // case 99: s = "c"; break; case 'd': // case 100: s = "d"; break; default: s = "default"; } System.out.printf("j = %d s = %s%n", j, s); } } private static void explore(char[] chars) { for(int j = 0; j < chars.length; j++) { char c = chars[j]; int n = (int)chars[j]; System.out.printf("j = %d c = %s n = %3d c == n = %b%n", j, c, n, c == n); } } }
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 08-02-2007, 05:43 AM
Senior Member
 
Join Date: Jul 2007
Posts: 130
cruxblack will become famous soon enough
Maybe cuz of the typo where u use ':' instead of the ';'
The char could be used as in input when i tested it, input, bytes, chars, all available i think
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Method in a Switch Statement cart1443 New To Java 6 03-14-2008 04:48 AM
Switch Statement Help bluegreen7hi New To Java 6 02-06-2008 06:16 AM
Switch statement to display the name of the month Java Tip Java Tips 0 01-04-2008 10:32 AM
JavaScript to Java Statement mutuah Advanced Java 0 08-08-2007 06:16 AM
Help with gigantamous switch statement trill New To Java 2 08-06-2007 09:11 AM


All times are GMT +3. The time now is 06:52 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org