Results 1 to 4 of 4
  1. #1
    CaseyRenee87 is offline Member
    Join Date
    Jun 2009
    Posts
    3
    Rep Power
    0

    Default 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.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  3. #3
    CaseyRenee87 is offline Member
    Join Date
    Jun 2009
    Posts
    3
    Rep Power
    0

    Default

    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.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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

  1. What are you using to write your code?
    By CaptainMorgan in forum New To Java
    Replies: 989
    Last Post: 06-27-2014, 07:23 PM
  2. Problem with what to write in URL
    By Manoeuvre in forum Eclipse
    Replies: 0
    Last Post: 12-01-2008, 02:56 PM
  3. How to write....**
    By krichait in forum New To Java
    Replies: 9
    Last Post: 08-07-2008, 02:09 PM
  4. Write to file
    By esadeghi in forum Advanced Java
    Replies: 1
    Last Post: 05-21-2008, 01:13 PM
  5. How to write your own Comparator
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-15-2008, 07:38 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •