By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,034 Members | 822 Online
Bytes IT Community
+ Ask a Question
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
  1. class Diamond { 
  2.  
  3.    // main method begins execution of Java application
  4.  
  5.    public static void main( String args[] )
  6.    {
  7.  
  8.    for(int x=0 ; x<=3 ; x++)
  9.     {
  10.         System.out.print(numbers(x));
  11.         numbers(x);
  12.         String r=numbers(x); 
  13.         String reversedString=reverse(r);
  14.         System.out.print(reversedString);
  15.         System.out.print("\n");
  16.  
  17.  
  18.    if(x==3)
  19.      for(x=x-1; x!=1 ; x--)
  20.       {
  21.           String y=numbers(x);
  22.           y.toString();
  23.         System.out.print(y);
  24.         numbers(x);
  25.         String r=numbers(x); 
  26.         String reversedString=reverse(r);
  27.         System.out.prin(reversedString);
  28.         System.out.print("\n");
  29.       }
  30.     }
  31.  
  32.  
  33.  
  34. }
  35. String numbers (int x)
  36. {
  37.    String p="";
  38. int n=1;
  39. int k=1;
  40.    for( ; k<n+x ; k++)
  41.  
  42.  
  43.       p+=k; 
  44.  
  45.  
  46.       System.out.print(n+x) ;
  47.       return p;  
  48. }
  49.  
  50. }
Is my using for the method numbers, printing the return string,and reversing the the return string wrong?
Jan 30 '07 #1
Share this Question
Share on Google+
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 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

Expand|Select|Wrap|Line Numbers
  1. System.out.print(numbers(x));
  2. String r=numbers(x); 
  3. 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[3];
Scanner in = new Scanner(System.in);
arr[0] = in.next();
arr[1] = in.next();
arr[2] = in.next();
System.out.println(arr[2] + " : " + arr[1] + " : " + arr[0]);
}
}
Mar 22 '07 #6

Ganon11
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[3];
Scanner in = new Scanner(System.in);
arr[0] = in.next();
arr[1] = in.next();
arr[2] = in.next();
System.out.println(arr[2] + " : " + arr[1] + " : " + arr[0]);
}
}
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

Post your reply

Sign in to post your reply or Sign up for a free account.