Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 13 of 13

Thread: Spaces at the start of every other line in a for loop

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Spaces at the start of every other line in a for loop

    I'm attempting to get spaces at the beginning of every other line of the output of this program:

    import java.util.Scanner;
    public class Checkerboard
    {
    public static void main(String[] args)
    {
    int num;

    Scanner input = new Scanner(System.in);

    System.out.println ("What is the integer?");
    num = input.nextInt();

    for (int x = 0; x < num; x++)
    {
    for (int y = 0; y < num; y++)
    {
    System.out.print("A ");
    }
    System.out.println(" ");
    }
    }
    }

    The output of this program, if the user enters "4" for example, is:
    A A A A A
    A A A A A
    A A A A A
    A A A A A

    I'm trying to get it to look like this:
    A A A A A
    (space)A A A A A
    A A A A A
    (space) A A A A A


  2. #2
    Junior Member
    Join Date
    Oct 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Spaces at the start of every other line in a for loop

    Read the announcement to format correctly, but what you're doing wrong is printing the A's one by one, it seems the A's aren't affected by the input so you can print a whole line by itself. Here's what I came up with real quick, didn't test it but I think it works.

    import java.util.Scanner;
    public class Checkerboard{
     
    	public static void main(String[] args){
    		int num;
    		Scanner input = new Scanner(System.in);
    		System.out.println ("What is the integer?");
    		num = input.nextInt();
     
    		for (int x = 0; x < num/2; x++){
    			System.out.println("A A A A A");
    			for (int y = 0; y < num; y++){
    				System.out.print(" ");
    			}
    			System.out.println("A A A A A");
    		}		
    	}
     
    }

  3. #3
    Member mjr's Avatar
    Join Date
    Jan 2012
    Location
    127.0.0.1
    Posts
    36
    My Mood
    Fine
    Thanks
    8
    Thanked 2 Times in 1 Post

    Default Re: Spaces at the start of every other line in a for loop

    I'd have to agree with the above poster.

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Spaces at the start of every other line in a for loop

    I'd have to disagree with both above posters!

    It appears that the space needs to be displayed on every even numbered line. Use the mod operator to work out if the current line is odd or even.
    Improving the world one idiot at a time!

  5. The Following User Says Thank You to Junky For This Useful Post:

    copeg (October 21st, 2013)

  6. #5
    Junior Member
    Join Date
    Oct 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Spaces at the start of every other line in a for loop

    Quote Originally Posted by Junky View Post
    I'd have to disagree with both above posters!

    It appears that the space needs to be displayed on every even numbered line. Use the mod operator to work out if the current line is odd or even.
    That code does apply the number of spaces on every other line. so it should work out to be the same. The code prints out the first set of lines while going to a new line. Then the spaces are added in the inner nested loop without going to a new line, after the spaces are added the line then prints again while going to a new line, therefore each loop pass prints out 2 lines of output. The one without spaces and one with spaces.

  7. #6
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Spaces at the start of every other line in a for loop

    But your advice to hard code it to print five A chars is just plain wrong!
    Improving the world one idiot at a time!

  8. #7
    Junior Member
    Join Date
    Oct 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Spaces at the start of every other line in a for loop

    Quote Originally Posted by Junky View Post
    But your advice to hard code it to print five A chars is just plain wrong!
    May I ask why? It's probably not the best way to do it but it works.

  9. #8
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Spaces at the start of every other line in a for loop

    Because the whole point of the program is that the user can enter how many lines (and chars per line) they want displayed. That is what the num variable is for. If I enter 10 I should get 10 lines with 10 A's on each line. Your code cannot do that!
    Improving the world one idiot at a time!

  10. #9
    Junior Member
    Join Date
    Oct 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Spaces at the start of every other line in a for loop

    Quote Originally Posted by Junky View Post
    Because the whole point of the program is that the user can enter how many lines (and chars per line) they want displayed. That is what the num variable is for. If I enter 10 I should get 10 lines with 10 A's on each line. Your code cannot do that!
    Not at all, because the input was 4 and there's 5 A's. No where did it specify that you need the amount of A's from the input. The number input was simply for the amount of spaces and lines of A's, 4 spaces were put in front of every other line and 4 lines were produced from the code. If it was the number 10, now there are 10 spaces and 10 lines of A's but if the case was that you needed 10 A's also then that's different but this didn't specify that.

  11. #10
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Spaces at the start of every other line in a for loop

    In the original post they probably made a mistake but if you look at the code both inner and outer loops iterate num number of times. So output is determined by the value the user inputs.
    Improving the world one idiot at a time!

  12. #11
    Junior Member
    Join Date
    Oct 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Spaces at the start of every other line in a for loop

    Quote Originally Posted by Junky View Post
    In the original post they probably made a mistake but if you look at the code both inner and outer loops iterate num number of times. So output is determined by the value the user inputs.
    Yes but his code wasn't correct to begin with, from what the output proves that code is fine. Until the output is set to something else then that code is wrong. You're assuming that he wants it to print the number of A's also but the original post never said that. Now you're assuming the original post made a mistake.

  13. #12
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Spaces at the start of every other line in a for loop

    Jeebus!

    The only thing wrong with the code in the original post was getting the space to print at the start of every even line.

    I don't care what you think but any program that uses hard coded values is not very flexible. You should always strive to write code that can produce the correct output for any input and NOT one single situation. For example:
    int one = ...;
    int two = ...;
    int three = ...;
    There is some code to handle 3 values. What happens if the requirements change to handle 4 variables? Do you just add another variable declaration and recompile? Now what will happen if you need to handle 10,000 values? I'm certainly not going to add another 9997 variables. So what is a better solution? Use a List so the number of values that have to be handle does not affect the code and it does not have to be changed or recompiled.

    Your advice to just hard code it to output 5 A's is wrong. Please learn from this instead of arguing.
    Improving the world one idiot at a time!

  14. #13
    Junior Member
    Join Date
    Oct 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Spaces at the start of every other line in a for loop

    Thanks for the replies everyone. I'd like to point out that Junky is correct! I made a typing error with the 5x5 output of A's. If the user enters 4 it will output 4x4.

Similar Threads

  1. While Loop Confounds JDK's Command Line Debugger (jdb)
    By christerlin in forum Loops & Control Statements
    Replies: 4
    Last Post: August 16th, 2012, 02:10 PM
  2. Replies: 10
    Last Post: September 16th, 2011, 07:49 PM
  3. Replies: 4
    Last Post: May 15th, 2011, 06:03 AM
  4. Cant get spaces
    By Ksmartman in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 13th, 2010, 07:06 AM
  5. g.drawLine doesn't draw line in for loop
    By shumpi in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 12th, 2010, 06:15 PM

Tags for this Thread