This is my code:: public class NumPal{ String current; String reverse; boolean d
ID: 3850643 • Letter: T
Question
This is my code::
public class NumPal{
String current;
String reverse;
boolean d;
public NumPal(String s){
current=s;
for (int k=0; k<s.length(); k--)
reverse=s.charAt(k)+"";
if(current==reverse)
d=true;
else
d=false;
}
public boolean pal(){
return d;
}
public String toString(){
String t = current+"reverse->" +reverse;
return t;
}
public String getCur(){
return current;
}
public String getRev(){
return reverse;
}
public NumPal next(){
String newNum= current+reverse;
NumPal q = new NumPal(""+newNum);
return q;
}
}
Here is the driver:
What my code produces:
start value [997]
NumPal@67424e82
new sum: 997 + 799 = 800796
NumPal@42110406
new sum: 800796 + 697008 = 697009497804
NumPal@531d72ca
new sum: Exception in thread "main" java.lang.NumberFormatException: For input string: "408794900796408794900796"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Long.parseLong(Long.java:592)
at java.lang.Long.parseLong(Long.java:631)
at NumPal.getRev(NumPal.java:37)
at NumPalDriver.main(NumPalDriver.java:13)
What it should produce:
start value [997]
997 reverse-> 799
new sum: 997 + 799 = 1796
1796 reverse-> 6971
new sum: 1796 + 6971 = 8767
8767 reverse-> 7678
new sum: 8767 + 7678 = 16445
16445 reverse-> 54461
new sum: 16445 + 54461 = 70906
70906 reverse-> 60907
new sum: 70906 + 60907 = 131813
131813 reverse-> 318131
new sum: 131813 + 318131 = 449944
final value: 449944
number of steps: 6
I would appreciate any help or cleaning up my code.
Thank you.
Explanation / Answer
The issue is with the for inside the NumPal constructor. Istead of using k-- u have to use k++.
And the other issue is that ,the values are being stored in the form of string. So when you use '+' between two strings they get appended instead of getting added.
for example :
String str1 = "123",
String str2 = "456".
so str1 + str2 gives "123456".
Before added you have to convert them to Integers using Integer.parseInt(str1);
I have modified getCur() and getRev() and next() function to use values as integers instead of strings. Below is the code . No code changes required in drivr code .
NumPal.java
public class NumPal {
String current ="";
String reverse="";
boolean d;
public NumPal(String s) {
current = s;
for (int k = 0; k < s.length(); k++) {
reverse = s.charAt(k) + reverse;
}
if (current == reverse) {
d = true;
} else {
d = false;
}
}
public boolean pal() {
return d;
}
public String toString() {
String t = current + "reverse->" + reverse;
return t;
}
public int getCur() {
return Integer.parseInt(current);
}
public int getRev() {
return Integer.parseInt(reverse);
}
public NumPal next() {
int newNum = Integer.parseInt(current) + Integer.parseInt(reverse);
NumPal q = new NumPal(Integer.toString(newNum));
return q;
}
}
OUTPUT:
start value [125]
125reverse->521
new sum: 125 + 521 = 646
646reverse->646
new sum: 646 + 646 = 1292
1292reverse->2921
new sum: 1292 + 2921 = 4213
4213reverse->3124
new sum: 4213 + 3124 = 7337
7337reverse->7337
new sum: 7337 + 7337 = 14674
14674reverse->47641
new sum: 14674 + 47641 = 62315
62315reverse->51326
new sum: 62315 + 51326 = 113641
113641reverse->146311
new sum: 113641 + 146311 = 259952
259952reverse->259952
new sum: 259952 + 259952 = 519904
519904reverse->409915
new sum: 519904 + 409915 = 929819
final value: 929819
number of steps: 10
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.