438,034 Members | 822 Online Need help? Post your question and get tips & solutions from a community of 438,034 IT Pros & Developers. It's quick & easy.

# using reverse methode

 P: 36 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 Expand|Select|Wrap|Line Numbers 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
7 Replies

 Expert 100+ P: 1,510 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? Jan 30 '07 #2

 P: 36 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 nwhere 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 Expand|Select|Wrap|Line Numbers System.out.print(numbers(x)); String r=numbers(x);  String reversedString=reverse(r); Jan 30 '07 #3

 Expert 100+ P: 1,510 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 Jan 30 '07 #4

 P: 36 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 Jan 30 '07 #5

 P: 1 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; Scanner in = new Scanner(System.in); arr = in.next(); arr = in.next(); arr = in.next(); System.out.println(arr + " : " + arr + " : " + arr); } } Mar 22 '07 #6

 Expert 2.5K+ P: 3,652 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; Scanner in = new Scanner(System.in); arr = in.next(); arr = in.next(); arr = in.next(); System.out.println(arr + " : " + arr + " : " + arr); } } Please ask your question in your own thread and not in someone else's thread. Mar 22 '07 #7

 P: 80 i think you didnt declared that variable named numbers.... 27 th line spelling mistake in print.... Mar 22 '07 #8 