|
|
Back to UserFriendly Strip Comments Index
| Warning: Long post begging for help with Java |
by wisteria456 |
2003-03-18 18:56:29 |
I have this Java program that is supposed to calculate a numeric palindrome from a positive numeric input. It will report the palindrome and how many reversals it took to find it. Also, if the palindrome will exceed the Max_Value for int it is supposed to report that it will overflow if it continues and how many times it reversed before it would overflow (hope that sentence made sense).
Ultimately, I will fix it before anyone posts any ideas, as that's the nature of the beast... ;) The main problem is that when you enter a value with length>1, it WON'T STOP until it overflows. I've been trying to debug since yesterday and I can't find it. Here's the code (it's a nasty mess right now, but I'm trying to fix it.)
I don't have most of the print statements in right now, but it should be relatively self-explanitory to anyone with much programming experience... it's for a Lvl 1 Java class... and please bear with me, this forum likes to take out the spaces...
public class Palindrome
{
String input=null;
int pal=0, rvs=0, diff=0, palcount=0, orig=0, ini=0, countTemp=0;
int inputTemp=0, powTemp=0;
boolean overflow=false, boopal=true;
public Palindrome(String x)
{
input=x;
orig=Integer.parseInt(x);
ini=Integer.parseInt(x);
}
public void isPalindrome()
{
int f=0, t=0;
String g=null;
g=input;
t=Integer.parseInt(g);
f=g.length();
if(f==2 && t%11!=0)
{
boopal=false;
}
else if(f>2 && f%2==0)
{
int right=f/2, left=right-1;
while(left<right && right<f && left>-1)
{
if(g.charAt(left)!=g.charAt(right))
{
boopal=false;
}
left--;
right++;
}
}
else if(f>1 && f%2==1)
{
int mid=(f-1)/2, left=mid-1, right=mid+1;
while(left<right && right<f && left>-1)
{
if(g.charAt(left)!=g.charAt(right))
{
boopal=false;
}
left--;
right++;
}
}
else
{
boopal=true;
}
if(boopal==true)
{
System.exit(0);
}
}
public void makeReversal()
{
int b=0, iniTemp=0;
String c=ini+"";
int d=c.length()-1;
do
{
iniTemp=Integer.parseInt(c);
b=iniTemp%10;
iniTemp=iniTemp/10;
c=iniTemp+"";
rvs=(int) (rvs+((b*Math.pow(10, d))));
d=c.length()-1;
}
while(d>0);
}
public void willOverflow()
{
diff=Integer.MAX_VALUE-rvs;
if(ini<=diff)
{
overflow=false;
}
else if(ini>diff)
{
overflow=true;
System.out.println("The program will overflow before calculating"+
" your palindrome.");
System.out.println("Your initial input was: "+orig);
System.out.println("The program will iterate "+palcount+
" times before the overflow will occur.");
System.exit(0);
}
}
public void makePalindrome()
{
isPalindrome();
if(overflow==false)
{
while(boopal==false)
{
makeReversal();
willOverflow();
System.out.println("makePalindrome: pal: " + pal);
pal=ini+rvs;
System.out.println("makePalindrome: pal: " + pal);
palcount++;
ini=pal;
input=""+pal;
isPalindrome();
if(boopal==true)
{
break;
}
}
}
}
}
|
|
[ Reply ] |
|
Initial thoughts... | by imperito | 2003-03-18 19:02:22 |
|
I'm sorry, I wasn't sure how to do that... | by wisteria456 | 2003-03-18 19:03:33 |
|
<pre> and </pre> are pre tags | by cybergeek | 2003-03-18 19:35:58 |
|
Thanks for the info. :D | by wisteria456 | 2003-03-18 19:39:01 |
|
"Preformatted Text" (n/t) | by Llyr | 2003-03-18 19:39:35 |
|
One thing to be careful about, tho. | by Freakazoid | 2003-03-18 19:43:38 |
|
that, and the "preview" window... | by cybergeek | 2003-03-18 19:47:12 |
|
Yup, and also keep in mind other resolutions | by Freakazoid | 2003-03-18 19:48:22 |
|
1152 x 864 here :) (n/t) | by Llyr | 2003-03-18 19:48:42 |
|
Kernel indentation... | by imperito | 2003-03-18 20:06:57 |
|
Darn friggin' skippy! 1TBS forever! :) (n/t) | by Llyr | 2003-03-18 20:15:03 |
|
The answer, perhaps? | by saxguy02 | 2003-03-18 21:35:39 |
|
|
[Todays Cartoon Discussion]
[News Index]
|
|