0
int leccs1 = q1+q2+r1+swhw1;
System.out.println("LecCS = "+ leccs1+ "/60");
int lecprelim = lcp+lcpo;
System.out.println("LecPrelim = "+ lecprelim+ "/100");
double x = 0.50;
int lecgrade = ((x * leccs1) + (x * lecprelim));
System.out.println("LecGrade = "+ lecgrade);



C:\FINALS OBE\OBE1\ICS112_1ISB_LecFinals_OBE.java:61: error: possible loss of precision
    int lecgrade = ((x * leccs1) + (x * lecprelim));
                                 ^
  required: int
  found:    double
1 error

Process completed.

I am trying to compute grade. The user enters the grades as input. I want x to be double not int.

alpakyol
  • 2,402
  • 2
  • 29
  • 35

3 Answers3

4

change

   int lecgrade = ((x * leccs1) + (x * lecprelim));

to

double lecgrade = ((x * leccs1) + (x * lecprelim));

int grade=lecgrade.intValue();

or in a single line you can write

int lecgrade = ((x * leccs1) + (x * lecprelim)).intValue();

use intValue() to convert from double to int

check intValue()

Sajad Karuthedath
  • 14,987
  • 4
  • 32
  • 49
1

What i came to know is you want lecgrade as an int you can do this by type casting

Try this,

int lecgrade =(int) ((x * leccs1) + (x * lecprelim));
akash
  • 22,664
  • 11
  • 59
  • 87
1

If you need lecgrade to be of type int, you probably want to apply some rounding before casting, e.g.:

        double lecgrade = 1.9d;

        int result = (int) lecgrade;
        int resultRounded = (int) Math.round(lecgrade);

        System.out.println(result); // prints 1
        System.out.println(resultRounded); // prints 2

Please note that

  1. intValue() is not applicable to primitives, you'd have to box your value first
  2. intValue() may truncate your value instead of rounding it

        Double k = Double.valueOf(1.9d);
        System.out.println(k.intValue()); // prints 1 for me
    
oschlueter
  • 2,596
  • 1
  • 23
  • 46