Results 1 to 4 of 4
- 08-17-2009, 10:34 PM #1
Member
- Join Date
- Jun 2009
- Posts
- 3
- Rep Power
- 0
How to write an app that reverses
Greetings,
I am wondering how to modify this program to reverse an integer input by a user, for example 12345 would be 54321.
here is the completed code so far (my significant other helped me out alot already) ;) he's good with Java.
I just need to fill in the blanks (or make corrections) :o
//class Reverse.Java
import java.util.Scanner
public class Reverse
{
public static void main(String args[])
{
Scanner input=new Scanner(System.in);
System.out.print("Enter an Integer value" );//display message prompt
int userValue=input.nextInt();//prompt for a 5-digit integer
valueOf=(int userValue);
System.out.println()
String = newString
for (int=i stringValue.length(); i>0; i--)
{
charAt(int -1)
}
what I want to do is turn the int into a string, reverse the string and then turn it back into an int and print it. :) I am a super n00b to Java so help is greatly appreciated! the concepts kinda take some getting used to.
-
1) First I recommend that you fire your significant other as your assistant and just go by your notes from class, your text books and tutorials on line. The above code isn't close to working or even compiling.
2) When creating a program, only add a small amount of code at a time, and compile early and often as you add code. Most importantly -- do not add any more code until current compile errors are fixed. If you don't follow this simple rule, you will create code that is nothing but a mess of compile errors.
3) There are two ways to tackle this problem: first and easiest is to treat the input as a String not as an int, don't worry about converting back and forth from int to string and visa versa (just leave as a String) and just iterate through the String backwards concatenating a new String as you go.
4) If you are not allowed to do this, and must treat the input as an int, then you'll likely have to use a little math to solve the problem. Get familiar with int division and mod arithmetic (the % symbol) as they will help you solve this. You will need to come up with an algorithm. to do so, figure out how you'd solve this on paper first then take those steps and translate into Java.
- 08-18-2009, 12:47 AM #3
Member
- Join Date
- Jun 2009
- Posts
- 3
- Rep Power
- 0
Hey thanks for the reply, well honestly I suck at math in response to your last suggestion, and I was trying to use the Java API premade classes to follow the software usability offered by java. :P and it's not the fault of my significant other he tried his best to help and did a good job I just have a hard head :rolleyes::o
well all that my assignment asks is that I input an int, reverse it then print the reversed number.
-
run and play with this code. perhaps it will give you ideas regarding the math:
Java Code:public class Fu1 { public static void main(String[] args) { int val1 = 91358; System.out.println(val1); System.out.println(); System.out.println(val1 / 10); System.out.println(val1 / 100); System.out.println(val1 / 1000); System.out.println(val1 / 10000); System.out.println(); System.out.println(val1 % 10); System.out.println((val1 / 10) % 10); System.out.println((val1 / 100) % 10); System.out.println((val1 / 1000) % 10); System.out.println((val1 / 10000) % 10); } }
Similar Threads
-
What are you using to write your code?
By CaptainMorgan in forum New To JavaReplies: 948Last Post: 05-21-2013, 12:41 PM -
Problem with what to write in URL
By Manoeuvre in forum EclipseReplies: 0Last Post: 12-01-2008, 02:56 PM -
How to write....**
By krichait in forum New To JavaReplies: 9Last Post: 08-07-2008, 02:09 PM -
Write to file
By esadeghi in forum Advanced JavaReplies: 1Last Post: 05-21-2008, 01:13 PM -
How to write your own Comparator
By Java Tip in forum java.langReplies: 0Last Post: 04-15-2008, 07:38 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks