-2

Hey every one I just need some help with formatting some code, I'm supposed to make a table, however I can't seem to get the alignment correct. So far this is the code I've gotten:

 public class assignment2 {
   public static void main(String[] args) { 
    int line = 0;
    int down = 0;
    int num =0;


    for (line = 1; line < 21; line++){ 
        System.out.print("     " + line );  //prints the numbers across with 5 spaces

    } 
        System.out.print("\n");  //moves the compiler to a new line, same thing as System.out.println 


    for (down = 1; down <= 20; down++) { //prints out the numbers down with 4 spaces to the first number 
        System.out.print(down + "    "); 
        for (int i = 1; i <= 20; i++){ //Counter to stand in place for the x-axis numbers
            num = i % down; 
            if (num != 0) { 
                System.out.print("    "); // 5 spaces, checks to see if the number are multiples of each other 
                if (i > 9){
                    System.out.print(" " ); //1 spaces, also aligns the numbers going diagonally 
                }
                } else {
                if (i >= 10){
                    if (i % 2 == 0 ) { 

                        System.out.print("E"+"      "); // 6 spaces 
                    } else  
                        System.out.print("O"+ "      "); // 6 spaces

                } else {
                    if (i % 2 == 0 ) { 
                        System.out.print("E"+"     "); // 5 spaces
                    } else { 
                        System.out.print("O"+ "     "); // 5 spaces

                    }
                } 
            }
            if (down >=2){
                System.out.print(" "); // 1 space 
            }
            if (i == 20){
                System.out.print("\r\n");
            }
        } 

    } 
}

So far the text looks like this: Screen shot of display with code playing. Any help would be appreciated, it you can point in the right direction and point out any mistakes I've done in the code it would be great appreciated!

  • You may want to take a look at the System.out.format() and System.out.printf() methods. – Ali Feb 24 '17 at 02:15

2 Answers2

0

The easiest way to do this is not use spaces at all. Simply use the Tab tag (\t), like this:

System.out.print("\t" + line);  //for your top line

Now when you want to apply either E or O you can do:

System.out.print("\tE");

OR

System.out.print("\tO");

If however, for some unknown reason you are not allowed to use the Tab tag (\t) then you can utilize the String.format() method in place of the Tab tag, like this:

System.out.print(String.format("%"+spaces+"s", "E"));

where the spaces variable would hold the number of spaces you want to left-pad your text. A possible rendition of your task may look like this:

int line, down, num, spaces;
for (line = 1; line <= 25; line++){ 
    System.out.print(String.format("%6s", line));  
} 
System.out.print("\n");  
for (down = 1; down <= 25; down++) { 
    System.out.print(down); 
    for (int i = 1; i <= 25; i++){ 
        num = i % down;
        if (down > 0 && down < 10 && i == 1) { spaces = 5; }
        else if (down > 9 && i == 1) { spaces = 4; }
        else { spaces = 6; }
        if (num != 0) { 
            System.out.print(String.format("%"+spaces+"s", " ")); 
        } 
        else {
            if (i % 2 == 0 ) { 
                System.out.print(String.format("%"+spaces+"s", "E"));
            } 
            else { 
                System.out.print(String.format("%"+spaces+"s", "O"));
            }
        }
    }
    System.out.println("");
}

But...then again...you're probably not allowed to use the String.format() method either :/

DevilsHnd - 退職した
  • 8,739
  • 2
  • 19
  • 22
  • Problem is I'm not allowed to use the tab function at all, so I have to figure out how to do with spaces instead – Oscarpon96 Feb 24 '17 at 02:34
0

I just created your program. To align the text I had to do. Do a similar thing for "O" and for the cases where neither "E" nor "O."

System.out.print("E");
System.out.print("     ");   
if(ii >= 10)
{
    System.out.print(" ");
}

I used one nested for loop with a bunch of if else statements to complete the program :

Example:

for(int i = 0; i <= numberEntered; i++)
{
    for(int ii = 0; ii <= numberEntered; ii++)
    {
         //A bunch of code here
    }
    System.out.print("\n");
}
SedJ601
  • 12,173
  • 3
  • 41
  • 59