creating "FOR LOOP"

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Apr 2009
Posts: 11
Reputation: king_786 is an unknown quantity at this point 
Solved Threads: 0
king_786 king_786 is offline Offline
Newbie Poster

creating "FOR LOOP"

 
0
  #1
Apr 2nd, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,969
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: creating "FOR LOOP"

 
0
  #2
Apr 2nd, 2009
Example of a For-loop:
  1. for(int i = 0; i < 3; i++)
  2. {
  3. /* This loop will repeat 3 times */
  4. }
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 11
Reputation: king_786 is an unknown quantity at this point 
Solved Threads: 0
king_786 king_786 is offline Offline
Newbie Poster

Re: creating "FOR LOOP"

 
0
  #3
Apr 2nd, 2009
but how do i incorporate this in to the scenario???
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,718
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 230
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso

Re: creating "FOR LOOP"

 
0
  #4
Apr 2nd, 2009
Obviously instead of 3 you will have the number of '*' you want to print.
And in the for-loop use the method:
  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:
  1. System.out.println();
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,718
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 230
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso

Re: creating "FOR LOOP"

 
0
  #5
Apr 2nd, 2009
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,656
Reputation: BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold 
Solved Threads: 206
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso

Re: creating "FOR LOOP"

 
0
  #6
Apr 2nd, 2009
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
}
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,718
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 230
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso

Re: creating "FOR LOOP"

 
0
  #7
Apr 3rd, 2009
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:
  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:

/***********\
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 11
Reputation: king_786 is an unknown quantity at this point 
Solved Threads: 0
king_786 king_786 is offline Offline
Newbie Poster

Re: creating "FOR LOOP"

 
0
  #8
Apr 3rd, 2009
buh i have no idea in how 2 do this??
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,718
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 230
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso

Re: creating "FOR LOOP"

 
0
  #9
Apr 3rd, 2009
Originally Posted by king_786 View Post
buh i have no idea in how 2 do this??
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
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 29
Reputation: Superstar288 is an unknown quantity at this point 
Solved Threads: 0
Superstar288 Superstar288 is offline Offline
Light Poster

Re: creating "FOR LOOP"

 
0
  #10
Apr 3rd, 2009
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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 613 | Replies: 10
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC