Help with syntax to reverse a positive integer

I am new to java.

I am learning with a book and on line videos.

an assignment i am trying to complete requires me to read in a positive integer and to print out the integer in reverse

heres what I have so far

thanks in advance for any help hints or suggestions :)

Code:

`/*`

* File: ReverseDigits.java

* -------------------

* Programming exercise 7 from Page 97 (Chapter 4)

* The Art and Science of Java by Eric S. Roberts

*

*/

import acm.program.*;

public class ReverseDigits extends ConsoleProgram {

public void run() {

println("This program reverses the digits in an integer.");

int n = readInt("Enter a positive integer: ");

int nReversed = 0;

int digits = 0;

int countdown = n; // ive duplicated (int n) cos i need (int n) again later

while (countdown > 0) { // this while loop counts the digits in (int n)

countdown /= 10;

digits++;

}

/*

* what i had in mind to do next was a for loop repeated the same amount

* of times as there are digits in (int n)

* then use n % 10 to get the last digit in its own

* if int n was lets say 1234

* I could say that (4E+3) plus (3E+2) plus (2E+1) plus (1E+0) would give me 4321

*/

int Eplus = digits;

for (int i = 0;i<digits;i++){

Eplus--;

nReversed += n%10;// how do i do this bit // nReversed += (n%10)E+(Eplus)

n/=10;

}

println("The reverse of the digits is " + nReversed);

}

}

Many Thanks,, problem solved

Many thanks, for your help, brilliant, works great :)

I couldnt work out why you used Eplus-1 but then i realised you moved the Eplus--; below the nReversed bit where as i had it above.

you would think that they might have mentioned Math.pow

before setting the task eh!

All they have mentioned so far is scientific notation with the E+ which is why i was going along that way. I had tried assigning nReverse as a double, casting it as a double and using all manner of parenth variations trying to get the syntax to work.

I saw Math.pow while I was looking round but i thought i would have to import some math library to use it (we're only using console program at the moment)

More thanks, gee you guys here are so helpful!

thanks Fubarable, and Darryl,

I shall endeavour to play with those bits of code and see what it does,

The math pow was what i was aiming for, we haven't done anything with strings as yet.

Gee you guys in here are so helpful, thanks very much.