Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Switch error

 
ed spithaler
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Every time I enter A to test the switch it prints out 9999.99. I am trying to use charAt(0) which should match case A, but it does not work. Anybody have any ideas? Thanks.

import java.util.Scanner;
/**




*/
 
Winston Gutkowski
Bartender
Pie
Posts: 10492
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ed spithaler wrote:Every time I enter A to test the switch it prints out 9999.99. I am trying to use charAt(0) which should match case A, but it does not work. Anybody have any ideas? Thanks.

Please UseCodeTags (←click), but off the top of my head I would add a print statement right before the switch to see what you're actually switching on.

Winston
 
Henry Wong
author
Marshal
Pie
Posts: 21385
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Considering that there is nothing related to 9999.99 in the switch statement, and there are 9999.99 in the if-else statements that follows, I think it is safe to say that you are looking in the wrong place for the issue.

Henry


ed spithaler wrote:Every time I enter A to test the switch it prints out 9999.99. I am trying to use charAt(0) which should match case A, but it does not work. Anybody have any ideas? Thanks.





 
Shamsudeen Akanbi
Ranch Hand
Posts: 85
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey man! looks like you've not actually initialised the variable "d".
 
Campbell Ritchie
Sheriff
Pie
Posts: 49752
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shamsudeen Akanbi wrote:Hey man! looks like you've not actually initialised the variable "d".
Yes, he has. By making the mistake of using d as a field rather than a local variable, he has initialised it (implicltly) to \0. So it does not == 'R'.
Try changing the value d to a local variable in that method.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic