0

So I have a file with the following values : 3,455;1,67;83,98;0,1;23,178;2.45;3.5;16,88. The code shows the values to the user and it must decide which number is largest and smallest. This is what I've written but I get a different output that what it's supposed to show.

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


public class NumbersNew {

    public static void main(String[] args) throws IOException {

        //Create a scanner object which will read the data from the file

        Scanner sc = new Scanner(new File("Numbers.txt"));
        sc.useDelimiter("\\s*;\\s*");
        while (sc.hasNextLine()) {

            System.out.println(sc.nextLine());
        }


            //Determine which number was the greatest and which one was the least
        double largest = Double.MIN_VALUE;
        double smallest = Double.MAX_VALUE;

        while(sc.hasNextDouble()) {
            double val = sc.nextDouble();

            if (val < smallest) {
                smallest = val;

            }
            if(val > largest) {
                largest = val;
            }

            System.out.println(largest);
            System.out.println(smallest);

        }

        sc.close();

        //Print these numbers
        System.out.println("The biggest number in the file is: " + largest);
        System.out.println("The smallest number in the file is: " +smallest);
    }
}

This is the output that I get and I don't understand why:

3,455;1,67;83,98;0,1;23,178;2.45;3.5;16,88 The biggest number in the file is: 4.9E-324 The smallest number in the file is: 1.7976931348623157E308.

Can anyone make a suggestion or point me in the right direction? Thank you!

  • 2
    After your `while (sc.hasNextLine())` loop, you are at end-of-file, so `while(sc.hasNextDouble())` will find nothing. Remove first loop. – Andreas May 14 '18 at 14:37
  • @Andreas makes a good point. If you need to scan the file multiple times, either open up two Scanners if the file is too large to read into memory. Or read the file into an `ArratList` where each element in the list is a line in the file, then process the `ArrayList`. You will be able to loop through it as many times as you want. –  May 14 '18 at 14:40
  • plus... commas in number makes Scanner to do not treat value as double e.g. 3,455 is not nextDouble() – Vadim May 14 '18 at 14:44
  • @Vadim That depends on locale. – Pshemo May 14 '18 at 14:44
  • @Andreas okay, so I removed the while(sc.hasNextLine()) loop but the output is still the same : The biggest number in the file is: 4.9E-324 The smallest number in the file is: 1.7976931348623157E308. I'm thinking maybe to try Math class. – Jack Duvall May 14 '18 at 14:53
  • @Pshemo as I understood it is a thousands grouping symbol not a decimal point symbol, as long as there are others values with a period (2.45). So, I have it in my locale as comma and Scanner does not accept that... – Vadim May 14 '18 at 15:24
  • @Vadim `3,455` *is* nextDouble(), in both `US` (1,234.56) and `GERMANY` (1.234,56) locales, however the value is not the same, 3455.0 vs 3.455 (US style), and there are *some* locales where it's not a nextDouble(). The second token `1,67` is however only a nextDouble() in a GERMANY style locale. – Andreas May 14 '18 at 16:00
  • @JackDuvall If you remove `while (sc.hasNextLine())` loop, then code will work partially for both a `US` and a `GERMANY` locale, however your file contains mixed formatting, e.g. `1,67` and `2.45`, so there is no locale which will parse all the numbers. – Andreas May 14 '18 at 16:12
  • @Andreas I did some changes in the code and now it displays the largest and smallest number :) – Jack Duvall May 14 '18 at 17:59

0 Answers0