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

conversion program terminates  RSS feed

 
Cyran Meriel
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


This program terminates after my first input. I have no idea why.
 
Liutauras Vilda
Marshal
Posts: 4634
316
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Cyran Meriel,

That is correct, your program executes and reaches the end of code and terminates. Main issue why it is not behave as you expect is, because you're comparing Strings as these were of primitive data type, but it is not true. Strings equality suppose to be checked with method "equals". I am refering to lines 13 and 20. Can you see the problem now?
 
Cyran Meriel
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:Hi Cyran Meriel,

Hi!

So I can only use == if I'm checking for numbers? In that case I dont know how to fix the problem and check for String equality.
 
Liutauras Vilda
Marshal
Posts: 4634
316
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On a positive side, it is worth to mention, that your code indentation is nice, I like the way you think about indenting your code - it is rather very important part of code readability, well done.

Now a bit more about the "==" equality when you compare String objects. What actually happens. When you write "==" in between two String you actually comparing references are these two string objects refering to the same memory location, but not comparing their content itself. So, at the moment think about it as: when you compare primitive data types as for example integers 2 == 2 leads to true, think about that "Cyran" == "Cyran" will lead to false. It is not always true, but I don't want to confuse you at this stage.
 
Liutauras Vilda
Marshal
Posts: 4634
316
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cyran Meriel wrote:So I can only use == if I'm checking for numbers?
Yes, at the moment think this way.
Cyran Meriel wrote:In that case I dont know how to fix the problem and check for String equality.
For Strings equality use method "equals". Which would look like:
or
 
Cyran Meriel
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:
Cyran Meriel wrote:So I can only use == if I'm checking for numbers?
Yes, at the moment think this way.
Cyran Meriel wrote:In that case I dont know how to fix the problem and check for String equality.
For Strings equality use method "equals". Which would look like:
or


Thank you! Love it when I learn something thats easy to understand and remember. What you said in your previous post is starting to sink in slowly hehe.

And thanks for the compliment. I've learned the indentation from people in this forum

This is just the best place to learn, I would have quit Java programming long ago if it wasn't for this forum. Because I have no teacher or friends that could help me.
 
Liutauras Vilda
Marshal
Posts: 4634
316
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. Maybe another few better practices to follow than you have now would be:
write "[]" next to the type String. I know it works that way, but not in all cases. If you were use variable arguments (varargs) and would put elipsis right after the variable name, you would get compile error. And the line would look like this:
It would work in this case only when elipsis you put after the type.

2. double amount, kp, pk; (line 7). Better declare variables on separate lines, this habit could lead to compile error at some point too. Example:
that would require to write

3. Try to choose more descriptive variable names, because "kp", "pk" are not descriptive and requires comment itself what they are meant to store. But commenting each variable is not a good idea too, so better to choose better naming approach.

4. Also, try to simplify your "main" method, so it would require to split it to more methods. Main method actually suppose to instantiate your class and start your program. That is actually it, ideally 1 line of code. You could find good advices here (<- link to click on). Winston Gutkowski wrote this FAQ, and can't remember how many times already I refered to it, see how good it is
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!