Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

float type with . and ,

Tags:

java

I wrote this code: For each line, it should take the weighted average of these numbers with weights 0.2, 0.4, and 0.4, and it should print out the averaged value on a seperate line on the standard output. For example, for the input below

45.00 67.00 98.00
89.00 23.00 89.00

it should produce

75.00
62.60

It works when i write, for example 45,6 but it doesn't when i write 45.6. This is my code:

import java.util.Scanner;

public class paket {
   public static void main (String [] args){
       Scanner input=new Scanner(System.in);
       float num1,num2,num3,result;

       while(input.hasNext()){

            num1=input.nextFloat();
            num2=input.nextFloat();
            num3=input.nextFloat();

            result= (float) (num1*0.2+num2*0.4+num3*0.4);

            System.out.println("Result is  " +result);
       }
   }
} 
like image 611
merve Avatar asked Dec 04 '25 11:12

merve


1 Answers

Change your locale before parsing the numbers:

Locale.setDefault(new Locale("en", "US"));
like image 176
Aurasphere Avatar answered Dec 07 '25 00:12

Aurasphere



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!