Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>When learning the language (and after learning too) it helps to have small part of code, that is easy to write , test and maintain. For example comparing a number and remembering could moved to another class and tested.</p> <p>I did not intend to correct your programming error. I would like to take a note on good programming practices <a href="http://rads.stackoverflow.com/amzn/click/0132350882" rel="nofollow">to write better code</a> .</p> <pre><code>import java.io.File; import java.io.IOException; import java.util.Scanner; public class LargenSmall { BigHolder bigHolder= new BigHolder(); SmallHolder smallHolder = new SmallHolder(); public static void main(String[] args) throws IOException { new LargenSmall().doSearch(args[0]); } void doSearch (String filename) throws IOException { //Open the file File file = new File(filename); Scanner inputFile = new Scanner(file); iterateAndFetchNumbers(inputFile); inputFile.close(); printResult(); testNumbers(); } private void iterateAndFetchNumbers(Scanner inputFile) { //Read all the values in Numbers file and find the highest value while (inputFile.hasNext()) { //Read the numbers in the file and compare each value to find highest value double number = inputFile.nextDouble(); smallHolder.accept(number); bigHolder.accept(number); } } private void printResult() { //Print out the lowest value in the list System.out.println("The lowest number in your file called, " + "Numbers.txt is " + smallHolder.small + "."); //Print out the highest value in the list System.out.println("The highest value in the list is " + bigHolder.big); } // private static class SmallHolder { double small= Double.POSITIVE_INFINITY; public void accept(double newNumber) { if ( newNumber &lt; small) { small = newNumber ; } } } private static class BigHolder { double big= 0; public void accept(double newNumber) { if ( newNumber &gt; big ) { big = newNumber ; } } } //self test example - should be in junit class public void testNumbers() { BigHolder b = new BigHolder(); b.accept(1); b.accept(3); b.accept(8); b.accept(0); b.accept(100); assert b.big == 0;//junit assert really } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload