-
Solve this problems for me, any help will be appreaciated
Design, write in Java, test and document a program (application) that accepts a short (i.e. less
than 20 character) string from the user and prints it out on the screen in a certain pattern. In
response to the user keeping pressing enter, the program moves it across the page to bounce off
column 20 and return. Then the program terminates. Use “*” characters to fill in the unused
columns.
For example, suppose that the user enters “dipsy”. Here is the output; each line of output is in
response to the user pressing the “enter” key.
Please enter your string: dipsy
dipsy**************
*dipsy*************
**dipsy************
***dipsy***********
...etc ...
*************dipsy*
**************dipsy
*************dipsy*
************dipsy**
...etc...
*dipsy*************
dipsy**************
Program terminated.
-
So what exactly is your problem? If you're more specific then we can help, but we're not going to write it all for you :P
-
well,
I decided to use stringbuffer to approach the question but i have trouble moving the word "dipsy" to the right and back again...
Please help
thanks
-
Well to move the word dipsy to the right you add something to the beginning of the string (add a space if you don't want it to be seen). To move it back again you take something away from the beginning.
The way I would do it would be to write a method that repeatedy prints out a line of spaces followed by the word. Each time it runs it will print out an extra space. To move it back write another method that starts off with a large number of spaces then prints one less each time.
-
PHP Code:
//THIS IS A JAVA CODE
public class fill
{
public static void main(String[] args)
{
String input = new String();
StringBuffer buffer = new StringBuffer();
int startdex, finishdex;
do {
System.out.print("Please enter a string (no more than 20 characters): ");
input = SavitchIn.readLine();
}while (input.length() > 20); //Loop again if string.length is more than 20
buffer.append(input); //append String to StringBuffer
buffer.setLength(20); //create buffer to store 20 characters
startdex = 0;
finishdex = input.length() - 1;
//int counter=buffer.length()-input.length();
//int maincount =0;
//for(int maincount=0 ;maincount < 9 ;maincount++) {
int counter = 0; //TRYING TO USE A LOOP TO INCREMENT THE COUNTER BUT FAILED
do {
for (int count1 = startdex; count1 <= counter; count1++) { //check start
char fill = '*';
buffer.setCharAt(count1, fill);
}
for (int counter2 = finishdex + counter; counter2 < buffer.length(); counter2++) { //check finish
char fill = '*';
buffer.setCharAt(counter2, fill);
}
for (int count = startdex; count <= finishdex; count++) { //fill word
buffer.setCharAt(counter, input.charAt(count));
counter++;
}
input = buffer.toString();
System.out.println("Filled: " +input);
counter++;
}while(counter<9);
}
}
I tried using the above code, well having several problems , if you look carefully int counter = 0; when if change the value of the counter,let says to 2 , then the output will be
**dispy***************
then i tried to use a for loop to increment the counter.... but it faled 
Please help out....
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks