using reverse methode

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • analoveu
    New Member
    • Jan 2007
    • 36

    #1

    using reverse methode

    I wanna print the following shape

    1
    121
    12321
    1234321
    12321
    121
    1

    and I use the following code,put there are alot of errors
    I want to know how i use the method toString to correct these errors

    Code:
    class Diamond { 
    
       // main method begins execution of Java application
       
       public static void main( String args[] )
       {
       
       for(int x=0 ; x<=3 ; x++)
        {
            System.out.print(numbers(x));
            numbers(x);
            String r=numbers(x); 
            String reversedString=reverse(r);
            System.out.print(reversedString);
            System.out.print("\n");
    
    
       if(x==3)
         for(x=x-1; x!=1 ; x--)
          {
          	String y=numbers(x);
          	y.toString();
            System.out.print(y);
            numbers(x);
            String r=numbers(x); 
            String reversedString=reverse(r);
            System.out.prin(reversedString);
            System.out.print("\n");
          }
        }
    
    
    
    }
    String numbers (int x)
    {
       String p="";
    int n=1;
    int k=1;
       for( ; k<n+x ; k++)
        
          
          p+=k; 
        
          
          System.out.print(n+x) ;
          return p;  
    }
    
    }
    Is my using for the method numbers, printing the return string,and reversing the the return string wrong?
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    your algorithm looks very complex - for example why do you want to reverse the string? (if you do you can use the reverse methods in StringBuffer)

    I think you may be trying to do too much at one go
    why not build it in stages? try to write a program to print
    1
    12
    123
    1234

    then when that works
    1
    121
    12321
    1234321

    then complete the top half of the dimond etc
    the bottom half is the reverse of the top - is where you want reverse?

    Comment

    • analoveu
      New Member
      • Jan 2007
      • 36

      #3
      I go upon that algorithm

      x n
      -----------------------------------------------------------------------------------
      0 n
      1 n n+1 n
      2 n n+1 n+2 n-1 n
      3 n n+1 n+2 n+3 n+2 n+1 n
      2 n n+1 n+2 n-1 n
      1 n n+1 n
      0 n
      where x represents vertical column of the numbers 0123210 and n represents the diamond n numbers
      the method numbers returns a string that contain the numbers from n to n+x ,for example while x=3 it return 123 , and prints the number n+3 or 4
      In the Main Method
      first I call the method in print method to print the number from n to n+x-1 for example 123 then call the method to print the number n+x or 4 then reverse the return value to reverse 123 to obtain 321 and print it in the same line
      But as you see i have a problem in using the reverse method with the method which i create it "numbers" and also in printing the return value of this method
      especially in these lines

      Code:
      System.out.print(numbers(x));
      String r=numbers(x); 
      String reversedString=reverse(r);

      Comment

      • horace1
        Recognized Expert Top Contributor
        • Nov 2006
        • 1510

        #4
        would reverse do what you want, e.g. line 3 should be
        12321

        but using reverse would give
        123321

        with an extra 3 in the middle

        Comment

        • analoveu
          New Member
          • Jan 2007
          • 36

          #5
          Originally posted by horace1
          would reverse do what you want, e.g. line 3 should be
          12321

          but using reverse would give
          123321

          with an extra 3 in the middle

          noo it will give 12321
          because my method returns the string 12 and print the number 3 and reversing my method print 21
          so after concatinate all of them give 12321

          but my problem is how to use the reverse method

          Comment

          • vieri3217
            New Member
            • Mar 2007
            • 1

            #6
            i want to implement printMe() method so that the array content is printed out in

            revese order (last entered, first printed).

            heres the code:
            _______________ _______________ _______________ _______________
            Hi ... user input
            there ... user input
            cp2 ... user input
            cp2 : there : Hi ... program output

            import java.util.*;
            class A1{
            public static void main(String[] args){
            String[] arr = new String[3];
            Scanner in = new Scanner(System. in);
            arr[0] = in.next();
            arr[1] = in.next();
            arr[2] = in.next();
            System.out.prin tln(arr[2] + " : " + arr[1] + " : " + arr[0]);
            }
            }

            Comment

            • Ganon11
              Recognized Expert Specialist
              • Oct 2006
              • 3651

              #7
              Originally posted by vieri3217
              i want to implement printMe() method so that the array content is printed out in

              revese order (last entered, first printed).

              heres the code:
              _______________ _______________ _______________ _______________
              Hi ... user input
              there ... user input
              cp2 ... user input
              cp2 : there : Hi ... program output

              import java.util.*;
              class A1{
              public static void main(String[] args){
              String[] arr = new String[3];
              Scanner in = new Scanner(System. in);
              arr[0] = in.next();
              arr[1] = in.next();
              arr[2] = in.next();
              System.out.prin tln(arr[2] + " : " + arr[1] + " : " + arr[0]);
              }
              }
              Please ask your question in your own thread and not in someone else's thread.

              Comment

              • reon
                New Member
                • Feb 2007
                • 80

                #8
                i think you didnt declared that variable named numbers....
                27 th line spelling mistake in print....

                Comment

                Working...