473,395 Members | 1,870 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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

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
7 2573
horace1
1,510 Expert 1GB
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
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
horace1
1,510 Expert 1GB
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
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
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
3,652 Expert 2GB
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
reon
80
i think you didnt declared that variable named numbers....
27 th line spelling mistake in print....
Mar 22 '07 #8

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

Similar topics

4
by: Alexis | last post by:
Hi, I need to transform one xml document into a second xml document. They both have many nodes so xslt works fine, but there is one node I have not figure out how to transform. Here it is:...
5
by: R. Rajesh Jeba Anbiah | last post by:
I could see that it is possible to have hash array using objects like var hash = {"a" : "1", "b" : "2"}; Couldn't still findout how to declare hash array in Array. var arr = new Array("a" : "1",...
1
by: Marc Falesse | last post by:
Hello, My application needs to serialize a class, which was generated with xsd.exe from a XSD. It was working .NET 1.1 and it is working with .NET 2.0 very well. Now I need my assembly to be a...
0
by: andreas | last post by:
Bill, what are you doing? I see the methode count in vb.net and in vb.net express but not in vb.net 2003 int = treeview.nodes.count Int = treeview1.nodes(i).nodes.count
0
by: tccode97 | last post by:
To whom it may concern, I am developing a socket application in VC++ that uses asynchronous connnection. After doing search on google, I found the following link ...
0
by: milmus tender | last post by:
Hi, we have some customized EditorParts, which inherits from a DefaultEditor. This DefaultEditorPart inherits from the standard EditorPart-Class. In this DefaultEditorPart we override the...
5
by: Thomas Kowalski | last post by:
Hi everyone, recently I have trouble to pass an array (unsigned char color) to a methode by reference and collect the return value by reference. I wanted something like: unsigned char&...
15
by: Boltar | last post by:
Hi I'm going through an STL list container using a reverse iterator but it seems the erase() method only accepts ordinary iterators. Is there a similar method that will accept reverse iterators...
3
by: Babikie | last post by:
Write a program that performs a reverse recursion with following functions. void swop (char,int,int); void reverse (char); void rev(char,int, int); User should enter a string and all character...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.