Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Default values for getters and setters  RSS feed

 
George McCoy
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:
 
Campbell Ritchie
Marshal
Posts: 55751
163
  • 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.
 
Junilu Lacar
Sheriff
Posts: 11153
160
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!