0

I need to make a program that find the minimum difference of an array of float by sorting it first. The problem is the Scanner wont take period (.) as a decimal point (for example if i input 2.251 it will read it as 2251.0). Here is a part of the code

import java.util.Scanner;
public class Selisih {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int m = sc.nextInt();
        float[] arr = new float[m];
        for(int i = 0; i < m; i++){
            arr[i] = sc.nextFloat();
        }
        quickSort(arr, 0, m - 1);
        System.out.printf("%.3f", minDiff(arr));
    }
  • Is there any chance you could reduce the example a bit so we can focus on the `Scanner` problem you're having? This may also help you debug the issue. – arcadeblast77 Sep 13 '19 at 13:43
  • 1
    Maybe this depends on your locale, have you tried it with a comma instead of a dot? Oh, I see, that's not the problem, you are reading an `int` from `System.in` by doing `int m = sc.nextInt();`. Change it to `int m = sc.nextFloat();`, maybe that helps. – deHaar Sep 13 '19 at 13:43
  • 1
    @deHaar just tried, it works with comma – Gian Martin Sep 13 '19 at 13:45
  • @deHaar the m is only used as an index for the array – Gian Martin Sep 13 '19 at 13:49
  • @GianMartin ok, I see... Thanks – deHaar Sep 13 '19 at 13:57

0 Answers0