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.

Dani AI

Generated

Short, concrete plan (what the program must do and why): compute the display width from the name (use the String length), add two characters for the single space on each side of the name, then print five lines in this order: top border (/ + stars + ), empty padding line (| + spaces + |), name line (| space name space |), another padding line, bottom border (\ + stars + /). Use a loop to build the repeated characters (the assignment wants a for-loop) and read the full name with a method that captures spaces.

A few clarifications based on the replies already here: is right to point you at the String length; however Scanner does not have a method called readLine() — use nextLine() to get a whole name (BufferedReader has readLine()). and are pointing you toward looping and the difference between printing with and without a newline; use those ideas but combine them into a small helper that repeats a character N times so the main logic stays clean. Remember to escape the backslash in Java string literals (use "\" in code).

Example implementation (compact, works on any Java version):

import java.util.Scanner;

public class BoxName {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Please enter your name> ");
        String name = sc.nextLine();
        int inner = name.length() + 2; // space either side

        System.out.println("/" + repeat(inner, '*') + "\\");
        System.out.println("|" + repeat(inner, ' ') + "|");
        System.out.println("| " + name + " |");
        System.out.println("|" + repeat(inner, ' ') + "|");
        System.out.println("\\" + repeat(inner, '*') + "/");

        sc.close();
    }

    private static String repeat(int n, char ch) {
        StringBuilder sb = new StringBuilder(n);
        for (int i = 0; i < n; i++) sb.append(ch);
        return sb.toString();
    }
}

Troubleshooting tips: if the name contains leading/trailing spaces, call trim() before measuring; if output looks misaligned, print the numeric inner value to debug; test first with a hard-coded name, then switch to reading input.

Recommended Answers

All 10 Replies

Example of a For-loop:

for(int i = 0; i < 3; i++)
{
    /* This loop will repeat 3 times */
}

but how do i incorporate this in to the scenario???

Obviously instead of 3 you will have the number of '*' you want to print.
And in the for-loop use the method:

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:

System.out.println();

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
}

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:

System.out.println();
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:

/***********\

buh i have no idea in how 2 do this??

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

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

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

I believe that his problem is printing what is asked based on the "name" input. Trying to get the "name" input using BufferedReader will not help much since he cannot do the simpler things.

So I suggest first to try to get the program work with some fixed value and then decide how to get the input

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.