• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

conversion program terminates

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


This program terminates after my first input. I have no idea why.
 
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 8857
637
Mac OS X VI Editor BSD Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 8857
637
Mac OS X VI Editor BSD Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 8857
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic