• Post Reply Bookmark Topic Watch Topic
  • New Topic

Comparison of floats  RSS feed

 
Adam Dodgson
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hiya guys, new here. Trying to put together my own little program, and I've pretty much done it all, just have one or two things to throw together. Basically I need to compare three numbers from a file, to a scanner input file. I've done the inFile fine and got the numbers in. What I need to do is compare these floats with the scanner input number, and find the one which is closest to the scanner.in number.
So I just need to find the one which is closest to the user input.
Not sure how to approach this, really. Any suggestions?
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

First think about the logic to do this, without thinking about how to write it down in code.

So, you have three numbers A, B and C, and you want to find out which of the numbers B and C is closest to A. How would you do that? (Explain it to yourself in plain English, not in Java code).
Hint: Think about the difference between A and B, and A and C. When you know the differences, how can you find out which one is closest to A?

Once you understand the logic, you can think about how to write that down in Java code.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!