The Artima Developer Community
Sponsored Link

Java Answers Forum
Precision Loss Error

2 replies on 1 page. Most recent reply: Apr 30, 2003 2:28 AM by Rahul

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
Kris K

Posts: 6
Nickname: krisk
Registered: Apr, 2003

Precision Loss Error Posted: Apr 29, 2003 11:31 PM
Reply to this message Reply
Advertisement
Hi,

Just a little for loop, where I wish to change a char to an int and then add 13 (ROT13 encoding). Though when I do this, I keep getting precision loss error.

for(int count=0;count<totalNum;count++)
{
int current1 = input1.charAt(count);
int current2 = current1 + 13;
encoded = encoded + current2;
}

Hopefully itll just get every char from the input1 string, then get the ascii value of this char and add 13 to it and then add to the encoded string for display.

Though doesnt seem to work =P

Thanks
Kris


Adam Duffy

Posts: 168
Nickname: adamduffy
Registered: Feb, 2003

Re: Precision Loss Error Posted: Apr 30, 2003 2:22 AM
Reply to this message Reply
I suspect that the variable encoded is of type String.

If that is the case then you will need to change the code as follows
encoded = encoded + String.valueOf( (char) current2 );


If your variable encoded is not of type String then post up the full method and I'll advise as appropriate.

Adam

Rahul

Posts: 52
Nickname: wildhorse
Registered: Oct, 2002

Re: Precision Loss Error Posted: Apr 30, 2003 2:28 AM
Reply to this message Reply
You may try something like this,

public class Rot13 
{
	public static void main(String[] args) 
	{
		String encoded = "";
		String str = args[0];
 
		// just to make things easy, converting input string to lowercase
		str = str.toLowerCase();
		int temp=0;
		for (int i=0;i<str.length() ;i++ )
		{
			temp = str.charAt(i);
			temp = temp+13;
			
			// if the value goes beyond 'z' it should start counting from 'a' again.
			if (temp > 122)
			{
				temp = (temp - 122) + 96;
			}
			
			// converting int back to char representation
			encoded = encoded + ((char)temp);
			temp = 0;
		}
		
		System.out.println(encoded);
	}
}
 
 

Flat View: This topic has 2 replies on 1 page
Topic: Serial Port Previous Topic   Next Topic Topic: Helper Classes

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use