| | |
creating "FOR LOOP"
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Apr 2009
Posts: 11
Reputation:
Solved Threads: 0
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.
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.
Example of a For-loop:
Java Syntax (Toggle Plain Text)
for(int i = 0; i < 3; i++) { /* This loop will repeat 3 times */ }
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Obviously instead of 3 you will have the number of '*' you want to print.
And in the for-loop use the method:
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:
And in the for-loop use the method:
Java Syntax (Toggle Plain Text)
System.out.print("");
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)
System.out.println();
Check out my New Bike at my Public Profile at the "About Me" tab
•
•
Join Date: Sep 2008
Posts: 1,656
Reputation:
Solved Threads: 206
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
}
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.
Java Syntax (Toggle Plain Text)
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:
/***********\
Check out my New Bike at my Public Profile at the "About Me" tab
•
•
•
•
We can only show you the door; you are the one that has to go through it
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
•
•
Join Date: Oct 2008
Posts: 29
Reputation:
Solved Threads: 0
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
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
![]() |
Similar Threads
- Where is SearchControl("xxx")? (ASP.NET)
- Please help me "I WANT IT FOR TOMORROW" (C++)
- system("PAUSE") (C++)
- The definiton of "Memory leaks" (C++)
Other Threads in the Java Forum
- Previous Thread: good words!!!
- Next Thread: accessing variables from a void method in one file and using it in another
Views: 613 | Replies: 10
| Thread Tools | Search this Thread |
Tag cloud for Java
actuate android api apple applet application arguments array arrays automation balls binary bluetooth business c++ chat class classes client code codesnippet collections component database defaultmethod dragging draw ebook eclipse error event exception file fractal froglogic game givemetehcodez graphics gui helpwithhomework hql html ide image input integer intersect invokingapacheantprogrammatically ip j2me java javaprojects jmf jni jpanel julia linux list loop looping map method methods mobile mysql netbeans newbie number numbers object oracle parameter php print problem program programming project recursion scanner screen server set size sms socket sort sql string sun swing swt tcp test threads time transfer tree udp windows






