Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

problem with precision loss

i know this is probably a common one, but here is my question:

public class getMakeModel
{

public static void main(String[] args)
{
ConsoleIO console = new ConsoleIO();

String input;
String make;
String model;
String plate;
int plate2;
double remainder;
double sum;
double num123;

//grab make

System.out.print("Make = ");
make = console.readLine();

//grab model

System.out.print("\nModel = ");
model = console.readLine();

//grab plate number

System.out.print("\nThree letters for plate number = ");
plate = console.readLine();
System.out.print("\nThree digit number of license place = ");
plate2 = console.readInt();

//code for license plate...?

int num1 = plate.charAt(0);
int num2 = plate.charAt(1);
int num3 = plate.charAt(2);

num123 = (int)(num1 + num2 + num3);

sum = num123 + plate2;
remainder = sum % 26;
double E = (int)(remainder + 65);
char F = (int) E;

System.out.print("\n" + plate + " = " + F + sum);



}
}

now apparantly i have possible precision lost in the line char F = (int) E;

i don't understand why i have precision loss, and how can i fix it?
[1363 byte] By [shaishai] at [2007-11-11 6:55:04]
# 1 Re: problem with precision loss
oh yeah by the way this is a school project so i need an answer asap, if possible, thanks...
shaishai at 2007-11-11 22:39:55 >
# 2 Re: problem with precision loss
import chn.util.*;

public class getMakeModel
{

public static void main(String[] args)
{
ConsoleIO console = new ConsoleIO();

String input;
String make;
String model;
String plate;
int plate2;

//grab make

System.out.print("Make = ");
make = console.readLine();

//grab model

System.out.print("\nModel = ");
model = console.readLine();

//grab plate number

System.out.print("\nThree letters for plate number = ");
plate = console.readLine();
System.out.print("\nThree digit number of license place = ");
plate2 = console.readInt();

//code for license plate...?

int num1 = plate.charAt(0);
int num2 = plate.charAt(1);
int num3 = plate.charAt(2);

int num123 = (int)(num1 + num2 + num3);

int sum = num123 + plate2;
double remainder = sum % 26;
int E = (int)(remainder + 65);
char F = (int) E;

System.out.print("\n" + plate + " = " + F + sum);



}
}
shaishai at 2007-11-11 22:40:55 >
# 3 Re: problem with precision loss
The error in the code

char F = (int) E;occurs because you are trying to store and integer into a char. There is your precision loss.

You could just write:

char F = (char)(E);or
int F = E;
Choose whether you need a char or an int.

:WAVE: For more help, www.NeedProgrammingHelp.com
NPH at 2007-11-11 22:41:59 >
# 4 Re: problem with precision loss
thanks man!
shaishai at 2007-11-11 22:42:58 >
# 5 Re: problem with precision loss
i don't know why, but i assumed that you need the int to transform it into a char. i thought that just having a value could be transformed into a char...
shaishai at 2007-11-11 22:44:02 >