Win a copy of Pro Spring MVC with WebFlux: Web Development in Spring Framework 5 and Spring Boot 2 this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

Switch error

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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;
/**




*/
 
Bartender
Posts: 10777
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 23899
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.





 
Ranch Hand
Posts: 85
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey man! looks like you've not actually initialised the variable "d".
 
Marshal
Posts: 72930
330
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic