0

I am new to java and thanks to this site figured out how get the lowest value from the list but I am still struggling with how to get the same code to work for the highest value. I've been working on it for the past 2 hours. Again any help is appreciated

import java.io.File;
import java.io.IOException;
import java.util.Scanner;

public class LargenSmall {
    public static void main(String[] args) throws IOException {
        String filename;        //Numbers file
        double lowest = Double.POSITIVE_INFINITY;//lowest number in list
        double highest; //highest number in list

        //Open the file
        File file = new File("Numbers.txt");
        Scanner inputFile = new Scanner(file);

        //Set lowest to zero


        //Read all the values in Numbers file and find the lowest value
        while (inputFile.hasNext()) {
            //Read the numbers in the file and compare each value to find lowest value
            double number = inputFile.nextDouble();
            if (number < lowest) lowest = number;
        }

        //Set highest to zero
        highest = 0.0;

        //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();
            if (number > highest) highest = number;
        }

        //Close file
        inputFile.close();

        //Print out the lowest value in the list
        System.out.println("The lowest number in your file called, " +
                "Numbers.txt is " + lowest + ".");

        //Print out the highest value in the list
        System.out.println("The highest value in the list is " + highest);
    }
}

I have tried a few variations and highest value keeps coming back as 0.0

Jayan
  • 18,003
  • 15
  • 89
  • 143

3 Answers3

0

First, you shoud declare the variable before the loop to improve speed:

double number;
while (inputFile.hasNext())
{
number = inputFile.nextDouble();
if (number > highest) highest = number;
}

Second, it seems that you are not start reading from the beggining of the file before start to find the highest value, so, this block is probably not being executed (because you've already reached the end of the file):

while (inputFile.hasNext())
{
double number = inputFile.nextDouble();
if (number > highest) highest = number;
}
Yuri
  • 1
  • 2
    Your first statement is not necessarily correct. See http://stackoverflow.com/questions/407255/difference-between-declaring-variables-before-or-in-loop for more info. – ddmps Mar 04 '13 at 02:59
0

You were there almost. You don't need two While loops. The second While loop wont work in your case. You can include the condition in your first while loop as below.

    double lowest = Double.POSITIVE_INFINITY;
    double highest = 0d;
    // Read all the values in Numbers file and find the lowest value
    while (inputFile.hasNext()) {
        // Read the numbers in the file and compare each value to find
        // lowest value
        double number = inputFile.nextDouble();
        if (number < lowest)
            lowest = number;
        else if (number > highest)
            highest = number;
    }
Jayamohan
  • 12,734
  • 2
  • 27
  • 41
0

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.

I did not intend to correct your programming error. I would like to take a note on good programming practices to write better 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 < small) {
                small = newNumber ;
            }
        }

    }

    private static class BigHolder {
        double big= 0;
        public void accept(double newNumber) {
            if ( newNumber > 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
    }


}
Jayan
  • 18,003
  • 15
  • 89
  • 143