First off: I am not looking for somebody to do my homework. Although it would make it easier for me in the short term, please don't "help" me by writing my code for me. I would rather lose points for being late and understanding the code than get the grade and fail later. Snippets for examples are nice but not if its the whole thing. Either an explanation or a link to somewhere that will help me will be greatly appreciated.

If this has already been discussed somewhere in the forum, I apologize, but I couldn't find it after a while of searching.

My assignment:
Write a program that asks for user input of a number and a character. The number dictates how many times the character will be output in the form of a square like the below example.

Enter number: 5
Enter Character: x
xxxxx
xxxxx
xxxxx
xxxxx
xxxxx

This is what I have so far:

package lab5a;

/**
 *
 * @author 24x24
 */
import java.util.Scanner;

public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        Scanner keyboard = new Scanner(System.in);
        System.out.print("Enter the length (1 - 16) of the square side: ");
        int number = keyboard.nextInt();
        while ((number <= 0) || (number >=17)) { //boolean decides if input is valid
            System.out.print("Your input was out of range\nEnter the length (1 - 16) of the square: ");
            number = keyboard.nextInt();
        }

      
        System.out.print("What character is to be used: ");

        String letter = keyboard.nextLine();
//this isnt pulling in an input for some reason but not the concern atm. Just declaring prior to loop with char letter = x;
        
        
//this is where the for loop would go, if thats what I should be using


    }//end of method
}//end of main

I had some iterations of a nested for loop sequence but I can only seem to get the character to print out one at a time
x
x
x
x
until I stop the program. I am not very good with for loop logic. I know it should be something like for (declare integer; condition; increment) but I just can't seem to wrap my head around how this should tie in with what I am trying to accomplish.

Thanks for any help. Ill keep perusing google and trying to understand the awful book I paid a fortune for and if I figure out what I'm doing before someone helps me I'll post how I did it.

Thanks again,
24x24

Well for me your for loop will be fine if you get the conditions right.

But its what u put in your for loop to print out the right number of chars on one line. I know how you can do this but dont know if you want telling.

What is inside your for loop atm?

are you using a System.out.println(""); to print out ur text?

for (initialization; termination; increment) {
    statement(s)
}

Meaning on what value you wish to start like int i = 0 , termination is like while statement i < 10 and increment can go either way like i++ or i-- .

So in your scenario you need to create nested for loops where the outside one will print new line (println) and inner will print character number of time on the line (print)

Does it make a sense?

currently this is what i have

for (int i = 1; i <= number; i++) {
            for (int j = 1; j <= number; j++) {
               // r = r + letter;
                System.out.print(letter);
            }

It prints a line of letter vertically. I did get it to stop going indefinitely though so I am making progress.

That is fine you just need second print for new line after inner for loop

Ok I now have

for (int i = 1; i <= number; i++) {
            System.out.print(letter);
            for (int j = 1; j <= number; j++) {
                
                
            }
            System.out.println(letter);
            
        }

and I get 2 columns of letter but it is outputing in the correct amount of rows.
ie if i put in 4 i get
xx
xx
xx
xx

and if i put in 1 i get
xx
Any suggestions?
I commented out each print separately and they do what they should. One prints 5 vertical, the other 5 horizontal.

Nope, wrong. As I said in my first post PRINT will be executed statement of inner for loop to print character and PRINTLN will follow after inner for loop to just print new line

print will print everything on one line, println will print then go to next line.

You're right, I misread. Thank you for your help on this one. Now to get the character input functioning properly. I think I can handle it from here though. Thanks again!

You welcome

Glad you got it. And I tested out program so far and I dont have a clue why the char input aint working. Hope u get it working

That will be down to fact that after character is read in there are no more commands to be executed :P

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.