problem with precision loss


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 6 of 6

Thread: problem with precision loss

  1. #1
    Join Date
    Sep 2005
    Posts
    15

    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?

  2. #2
    Join Date
    Sep 2005
    Posts
    15
    oh yeah by the way this is a school project so i need an answer asap, if possible, thanks...

  3. #3
    Join Date
    Sep 2005
    Posts
    15
    Code:
    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);
    	
    	
    	
    	}	
    }

  4. #4
    Join Date
    Aug 2005
    Posts
    17
    The error in the code

    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:

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

    For more help, www.NeedProgrammingHelp.com

  5. #5
    Join Date
    Sep 2005
    Posts
    15

    holy crap!

    thanks man!

  6. #6
    Join Date
    Sep 2005
    Posts
    15
    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...

Similar Threads

  1. Problem with Search
    By Irina in forum ASP.NET
    Replies: 0
    Last Post: 11-29-2002, 10:47 PM
  2. Replies: 0
    Last Post: 12-13-2001, 12:06 PM
  3. a problem with font and language
    By Roseta in forum VB Classic
    Replies: 0
    Last Post: 11-14-2001, 03:24 AM
  4. Arabic problem view
    By Ayman in forum VB Classic
    Replies: 0
    Last Post: 04-03-2000, 01:08 AM
  5. Problem with CryptoAPI and JCE
    By Jason Bock in forum VB Classic
    Replies: 0
    Last Post: 03-21-2000, 06:48 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center