943,692 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 873
  • Java RSS
You are currently viewing page 1 of this multi-page discussion thread
Apr 2nd, 2009
0

creating "FOR LOOP"

Expand Post »
hello i am stuk on this piece of work which i have no understaning at all!!! this is the question could someone please show me or give me gudlines in how to do it!!!

Write a Java application that asks a user for their name and displays it in a ‘box’ similar to that you implemented in week 1, exercise 1. For this application, you use a for loop to repeated printing * and space characters to result in the boundaries and padding, respectively. Below is what your application should produce.

Please enter your name> king 786

/********\
| |
| king 786 |
| |
\********/

The number of characters (* or space) is determined by the length of the name entered (hint: use String length method). Remember that you have to add 2 to this length in order to take into account the space either side of the name.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
king_786 is offline Offline
11 posts
since Apr 2009
Apr 2nd, 2009
0

Re: creating "FOR LOOP"

Example of a For-loop:
Java Syntax (Toggle Plain Text)
  1. for(int i = 0; i < 3; i++)
  2. {
  3. /* This loop will repeat 3 times */
  4. }
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009
Apr 2nd, 2009
0

Re: creating "FOR LOOP"

but how do i incorporate this in to the scenario???
Reputation Points: 10
Solved Threads: 0
Newbie Poster
king_786 is offline Offline
11 posts
since Apr 2009
Apr 2nd, 2009
0

Re: creating "FOR LOOP"

Obviously instead of 3 you will have the number of '*' you want to print.
And in the for-loop use the method:
Java Syntax (Toggle Plain Text)
  1. System.out.print("");
The above method doesn't change line.
Of course when the loop is done remember to call the one that changes line in order to continue writing at the next line.

Also you can call this with no arguments:
Java Syntax (Toggle Plain Text)
  1. System.out.println();
Sponsor
Featured Poster
Reputation Points: 1014
Solved Threads: 446
Nearly a Senior Poster
javaAddict is offline Offline
3,258 posts
since Dec 2007
Apr 2nd, 2009
0

Re: creating "FOR LOOP"

Sponsor
Featured Poster
Reputation Points: 1014
Solved Threads: 446
Nearly a Senior Poster
javaAddict is offline Offline
3,258 posts
since Dec 2007
Apr 2nd, 2009
0

Re: creating "FOR LOOP"

And your teacher is trying to tell you that the String's length method will tell you how long their name is. So what everyone else here has said applies, let me put it all together for you:

1. Prompt the user, asking them what their name is
2. Use a Scanner Object and the readLine method to read their name into a String
3. use the length() method of the String class to figure out how long their name is. Lets call this "nameLength"
4. Use a for loop to print out the *'s. Inside the for loop, have an if statement that says for (int i = 0; i < nameLength; i++){
//print the *'s and other stuff in here
}
Reputation Points: 874
Solved Threads: 352
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008
Apr 3rd, 2009
0

Re: creating "FOR LOOP"

Quote ...
The number of characters (* or space) is determined by the length of the name entered (hint: use String length method). Remember that you have to add 2 to this length in order to take into account the space either side of the name.
Also don't forget to add 2 as stated above, and you will need to play with the methods:
Java Syntax (Toggle Plain Text)
  1. System.out.println();
  2. System.out.print();

As I said, one changes line the other doesn't. So decide which one goes inside the loop and which doesn't in order to have this result:

/***********\
Sponsor
Featured Poster
Reputation Points: 1014
Solved Threads: 446
Nearly a Senior Poster
javaAddict is offline Offline
3,258 posts
since Dec 2007
Apr 3rd, 2009
0

Re: creating "FOR LOOP"

buh i have no idea in how 2 do this??
Reputation Points: 10
Solved Threads: 0
Newbie Poster
king_786 is offline Offline
11 posts
since Apr 2009
Apr 3rd, 2009
0

Re: creating "FOR LOOP"

Click to Expand / Collapse  Quote originally posted by king_786 ...
buh i have no idea in how 2 do this??
Quote ...
We can only show you the door; you are the one that has to go through it
Meaning that we have already told you which methods to use and how. You need to think the way. You already know the commands. Only the System.out. ..... and the for-loop which was explained are needed. Coding it is easy. But you need to think how to use the commands, we cannot post the solution, just use the command to print what is required.

Also, at first if you don't know how to read the input try using the:
> argument array of the main:
> public static void main (String [] args)

Or put a value to a variable and try work with it
Sponsor
Featured Poster
Reputation Points: 1014
Solved Threads: 446
Nearly a Senior Poster
javaAddict is offline Offline
3,258 posts
since Dec 2007
Apr 3rd, 2009
0

Re: creating "FOR LOOP"

Alot of information has been given and you should be able to get it working. just do some trial and error that is the best way with programming.

also you can use
BufferedReader to get the users input that is how we were taught at uni.
check the internet there are lots of examples for it. please let us know how you get on
Reputation Points: 10
Solved Threads: 1
Light Poster
Superstar288 is offline Offline
46 posts
since Oct 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: good words!!!
Next Thread in Java Forum Timeline: accessing variables from a void method in one file and using it in another





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC