Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Java Programming Fix - using recursion to print integer in reverse order vertically?

  1. #1
    Junior Member
    Join Date
    Jul 2012
    Posts
    15
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Java Programming Fix - using recursion to print integer in reverse order vertically?

    Hey so here's the program I have so far - instead of printing on one line though, I need to print the integers vertically - any ideas? I included the full prompt if that helps anyone!

    /* Write a short recursive program (name the class that contains the main method Activity1) that calls a
    recursive method (named writeVertical), which in turn takes one (nonnegative) int argument from the
    user (prompted at run-time) and writes that int to the screen with the digits going down the screen one per
    line from least significant to most significant. Your recursive method should not return any values.
    Sample Input to the recursive method:
    writeVertical(1234);
    Sample Output (from the program):
    4
    3
    2
    1
    Your implementation should hard-code at least three different calls to the writeVertical method using
    different values of differing lengths. One of the calls must be of length (or size) 1 (that is, one digit long).
    The other two calls can be of any other lengths, so long as they are of differing lengths. */

    public class Activity1
    {
    public static void main(String[]args)
    {	
    int intValue=3052;
     
    String stringValue=Integer.toString(intValue);
     
    writeVertical input1 =new writeVertical();
    input1.reverseResult(stringValue,0);
    }
    }
     
    class writeVertical {
    public int reverseResult(String b,int c) {
    if(c==b.length()-1) {
    System.out.print(b.charAt(c));
    return 0;
    }
    else {
    int temp=c;
    c=c+1;
    reverseResult(b,c);
    System.out.print(b.charAt(temp));
    return 0;
    }
    }
    }


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Java Programming Fix - using recursion to print integer in reverse order vertical

    Try:
    System.out.println(myString);
    println includes a new line character after the myString where print does not.

  3. The Following User Says Thank You to jps For This Useful Post:

    ash12 (August 5th, 2012)

Similar Threads

  1. Reverse Engineering An EXE made with Java
    By BigBoi2010 in forum Object Oriented Programming
    Replies: 0
    Last Post: June 11th, 2012, 04:02 PM
  2. BPEL -> WSDL -> JAVA -> JSP How do I do it? In which order?
    By x_maras in forum JavaServer Pages: JSP & JSTL
    Replies: 0
    Last Post: June 12th, 2011, 07:21 AM
  3. Implementing order of operations for a Java calculator.
    By mjpam in forum Java Theory & Questions
    Replies: 2
    Last Post: May 15th, 2011, 06:11 PM
  4. Memoization (dyanamic programming) for edit distance recursion
    By rph12 in forum Algorithms & Recursion
    Replies: 4
    Last Post: January 28th, 2011, 10:37 AM
  5. How to reverse a String using java.lang.StringBuilder
    By JavaPF in forum Java SE API Tutorials
    Replies: 0
    Last Post: July 22nd, 2009, 09:42 AM