• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

[JAVA] Error in Code

Status
Not open for further replies.
Level 6
Joined
Oct 10, 2013
Messages
173
Code:
package com.neel.TestingObjects;

public class Numbers {
private  int start,end;
public Numbers(int a, int b)
{
    start = a;
    end = b;
}
public void Palindrome()
{
    int rev = 0,temp;
    for(start=start;start<=end;start++)
    {
        temp = start;
        do
        {
        rev = rev*10 + temp%10;
        System.out.println(rev);
        temp = temp/10;
        }while(temp>0);
        if(rev == temp)
        {
            System.out.println(temp);
        }
        }
}
}
When I access the method,by creating an object , all i get on output screen is

0
0

Here is the code of main class
Code:
package com.neel.TestingObjects;

public class Main {

    public static void main(String[] args) {
        Numbers myNumber = new Numbers(10,100);
        myNumber.Palindrome();
    }

}

Current Output.
Code:
0
1
11
111
1112
11121
111213
1112131
11121314
111213141
1112131415
-1763587737
-456008180
-265114503
1643822273
-741646453
1173470070
-1150201187
1382890027
943998383
850049238
-89442210
-894422099
-354286396
752103338
-1068901210
-2099077505
484061432
545647028
1161502986
-1269872023
186181660
1861816606
1438296878
1498066899
2095767104
-517165432
-876687022
-176935619
-1769356188
-513692696
-841959661
170337983
1703379833
-146070852
-1460708517
-1722183279
-41963603
-419636026
98607039
986070395
1270769361
-177208272
-1772082717
-540957979
-1114612491
1738776986
207900679
2079006799
-684768487
1742249722
242628040
-1868686895
-1506999762
2109871566
-376120816
533759139
1042624098
1836306392
1183194740
-1052954483
-1939610234
2078734146
-687495016
1714984439
-30024790
-300247892
1292488380
39981921
399819214
-296775156
1327215741
387255523
-422412061
70846688
708466885
-1505265739
2127211799
-202718486
-2027184855
1202987935
-855022533
39709268
397092685
-324040439
1054562911
1955694526
-1917891215
-1999042957
1484406915
1959167262
-1883163854
-1651769355
662175640
-1968178190
1793054586
750676679
-1083167796
2053223932
-942597154
-836036943
229565168
-1999315610
1481680386
1931901979
2139150612
-83330352
-833303514
256899461
-1725972680
-79857616
-798576153
604173063
1746763341
287764228
-1417325009
-1288348199
1419905
14199054
141990547
1419905475
1314152869
256626808
-1728699209
-107122899
-1071228983
-2122355230
251284187
-1782125417
-641384979
-2118882494
286011548
-1434851815
-1463616254
-1751260650
-332737308
967594219
1086007606
-2024825824
1226578248
-619119403
-1896226726
-1782398070
-644111508
-2146147777
13358718
133587188
1335871888
473817001
443202722
137059924
1370599249
821090603
-379028553
504681768
751850393
-1071430659
-2124371989
231116594
-1983801347
1636823015
-811639025
473544348
440476193
109794641
1097946419
-1905437690
-1874507707
-1565207877
1527790423
-1901964954
-1839780356
-1217934375
 
Last edited:

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,201
Not sure what was wrong with your code. Instead I just wrote my own based on stuff I found on google.

Java:
public class Numbers {
	private int start, end;

	public Numbers(int a, int b) {
		start = a;
		end = b;
	}

	private static boolean checkPalindrome(final int n) {
		int num = n;
		int rev = 0;
		while (num > 0) {
			rev = rev * 10 + num % 10;
			num = num / 10;
		}

		return n == rev;
	}

	public void printPalindromes() {
		for (int n = start; n <= end; n++) {
			if (checkPalindrome(n)) {
				System.out.println(n);
			}
		}
	}

	public static void main(String[] args) {
		Numbers myNumber = new Numbers(10, 100);
		myNumber.printPalindromes();
	}
}
Output...
Code:
11
22
33
44
55
66
77
88
99
 
Last edited:
Level 6
Joined
Oct 10, 2013
Messages
173
Not sure what was wrong with your code. Instead I just wrote my own based on stuff I found on google.

Java:
public class Numbers {
    private int start, end;

    public Numbers(int a, int b) {
        start = a;
        end = b;
    }

    private static boolean checkPalindrome(final int n) {
        int num = n;
        int rev = 0;
        while (num > 0) {
            rev = rev * 10 + num % 10;
            num = num / 10;
        }

        return n == rev;
    }

    public void printPalindromes() {
        for (int n = start; n <= end; n++) {
            if (checkPalindrome(n)) {
                System.out.println(n);
            }
        }
    }

    public static void main(String[] args) {
        Numbers myNumber = new Numbers(10, 100);
        myNumber.printPalindromes();
    }
}
Output...
Code:
11
22
33
44
55
66
77
88
99
I have that solution Sir, just wanted to rectify my block of code, enhance my understanding of what was wrong.

You do not reset rev inside your loop.

After rectifing that, the output I get is.
Code:
0
1
1
11
2
21
3
31
4
41
5
51
6
61
7
71
8
81
9
91
0
2
1
12
2
22
3
32
4
42
5
52
6
62
7
72
8
82
9
92
0
3
1
13
2
23
3
33
4
43
5
53
6
63
7
73
8
83
9
93
0
4
1
14
2
24
3
34
4
44
5
54
6
64
7
74
8
84
9
94
0
5
1
15
2
25
3
35
4
45
5
55
6
65
7
75
8
85
9
95
0
6
1
16
2
26
3
36
4
46
5
56
6
66
7
76
8
86
9
96
0
7
1
17
2
27
3
37
4
47
5
57
6
67
7
77
8
87
9
97
0
8
1
18
2
28
3
38
4
48
5
58
6
68
7
78
8
88
9
98
0
9
1
19
2
29
3
39
4
49
5
59
6
69
7
79
8
89
9
99
0
0
1
 
Level 6
Joined
Oct 10, 2013
Messages
173
Sorry for double posting, I found the solution there was error on the loop interpretation of variables.
I reversed the order of some variables,
examples
temp = start , was start = temp,
print start was print temp

The correct code which runs perfectly is.
Code:
package com.neel.TestingObjects;

public class Numbers {
private  int start,end;
public Numbers(int a, int b)
{
    start = a;
    end = b;
}
public void Palindrome()
{
    int rev = 0,temp;
    for(start=start;start<=end;start++)
    {
        temp = start;
        do
        {
        rev = rev*10 + temp%10;
        temp = temp/10;
        }while(temp>0);
        if(rev == start)
        {
            System.out.println(start);
        }
        rev = 0;
        }
}
}

Although DSG , figured out that solution last night.To split up the method for ease of maintainence.
 
Status
Not open for further replies.
Top