• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Default values for getters and setters  RSS feed

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And I'm back. :/ So, I'm writing a class file that interacts with a driver to get a student's info but I'm having a bit of trouble with the input validation. Basically, if I enter any ID number via the driver that doesn't start with the letter E, then the ID number should be set to the default value of E####### and if the concentration isn't CS, IT or IS, it's supposed to default to XX. But lines 17 and 29 keep throwing me the error message "Incomparable types: char and String" and the default values don't kick in so when I run the print statements in the driver, it displays an ID of "Not a valid ID" and a concentration of "Messing up Java programs" that I entered with the driver. I have no idea why it thinks there any chars in there since I declared everything as Strings. Any idea where I messed up and how to fix it?

Here's the class file:


And the driver:
 
Marshal
Posts: 64493
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In line 17 you are trying to see whether the char is equal to the String "E". Maybe you meant myString.startsWith("E") or maybe that the char is equal to 'E'. Subtle difference in the quote marks.
I would suggest you be a bit more aggressive than choosing a default value.You will have problems later on when you use the == operator on Strings. Don't worry; you won't notice that because of your incorrect use of boolean algebra in that line

Why have you got a no‑arguments constructor? Why are you letting your number be set to E###### rather than requiring a “real” number be supplied. To get a “real” number make sure only to expose contructors requiring a “real” number.
 
Sheriff
Posts: 13454
222
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Using || on line 29 is not correct. Think about it a little bit and run through some scenarios to see why.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!