Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java method to output difference between two numbers as a comparison showing the highest number

 
William Kovacs
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have an issue trying to create a method successfully to get the difference of two integers (sales) and displaying them. I have a working program and it's all good but I need to find a way to add this to what I have. It's not the end if I can't get it but I have a personal interest in it now. Should I post code too? It's two classes....It is a homework question but I want to know for myself. I've been doing this over 10 hours now and have a pretty decent program going but I need this element to finalize my brain and get peace...


________________________________________________________________________________________________________________end of main class



visual example.jpg
[Thumbnail for visual example.jpg]
program screenshot with needed element
 
Winston Gutkowski
Bartender
Pie
Posts: 10498
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
William Kovacs wrote:I have an issue trying to create a method successfully to get the difference of two integers (sales) and displaying them.

Right.

1. Please UseCodeTags (←click). And please read the page thoroughly, because some of your lines are far too long. You can use the Edit button to add the tags and break up those enormous lines.

2. I'm not quite sure what your problem is. Given two integers, i1 and i2, the simplest way to display their difference is:
System.out.println(i1 - i2);
however I suspect there's more to your problem than just that, so please explain it fully.

Winston
 
Campbell Ritchie
Sheriff
Pie
Posts: 49761
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is not only the lines that are too long (if you look, I have added code tags and broken the lines, so you can see how it is done, but you would have done it better yourself), but the whole thing is too long. You should write 5 lines, compile and run them, and see what happens. Then write another 5 lines, etc. That way you find out much sooner when something goes wrong. As it is, there is too much code for us to read.
One thing I noticed, however, which I didn’t like, is that you are combining different ways to format output. Use printf or DecimalFormat, but not both. I would suggest you get rid of DecimalFormat altogether. Get rid of the \n character too; use the %n tag instead and printf throughout.

And welcome to the Ranch
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic