Web Analytics Made Easy -
StatCounter loop!!!! - CodingForum


No announcement yet.


  • Filter
  • Time
  • Show
Clear All
new posts

  • loop!!!!

    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.

  • #2
    You will have to start by knowing how many asterisks and spaces will be needed for your loops. So your first task is to find the length of the user's name that they enter. Do a search (or look in your book) on how to find the length of a string.

    Then, since you'll probably want one space on each side of the user's name, you'll take the length plus 2 and store that in another variable. Now you have the total amount of asterisks and spaces for above and below the user's name.

    A basic for loop is what you'll use. An example of a for loop is:
    for(int i=0; i<10; i++) {
    The above will print the numbers 0 - 9 right next to each other on one line. Use your book or search online to find out exactly how the loop works.

    With that knowledge, begin to construct your box:
    |       |
    | Shane |
    |       |
    You'll see that for me, my name is 5 letters plus 2 spaces (one on each side), which gives me 7 characters total. I'd then print the /, then use a for loop that would print the 7 asterisks, and then print the \. The next line would be the same, but with spaces rather than asterisks, and the beginning and ending characters would be |. You should know how to go on from there.

    Work on this a little and if you get stuck again, post the code that you have, and we'll try to guide you in the right direction. We will not do it for you.