Search code examples
javanestedprintfprintwriter

Trouble getting PrintWritter to correctly print to .txt after my nested loop


I'm asked to use PrintWriter to print grids of multiplication problems to a .txt file. Everything is working well but I'm having some serious trouble with getting my code to print the way I'm asked. I've been using printf with left/right indentation. Any help would be very appreciated.

I didn't realize that my account wouldn't upload pictures so sorry about the confusion. I had thought I posted the output needed. This is how it should look.

Basically it's creating 3 columns of numbers by 10 rows. I can get this to work but without the separation of columns and rows. The left side is the odd numbers with the right side being the even.

1 * 1 = 1                 1 * 2 = 2
2 * 1 = 2                 2 * 2 = 4
3 * 1 = 3                 3 * 2 = 6
4 * 1 = 4                 4 * 2 = 8
ect
10 * 1 = 10              10 * 2 = 20



1 * 3 = 3
2 * 3 = 6
ext.....




import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.File;

public class LabPrintTimeTables {

public static void main(String[] args) throws IOException
{
        printTimeTable();

}

private static void printTimeTable() throws FileNotFoundException
{
    try {
        File file = new File ("Lab Print Time Table");

        PrintWriter printWriter = new PrintWriter("TimeTables.txt");

        printWriter.println ("\tTimes Tables:\n");
        for(int i = 0; i <= 10; i++)
        {
            for(int j = 1; j <= 10; j++ )
            {
                if (i % 10 == i) {
         System.out.printf("%-2d * %-2d = %-2d", j, i + i + 1, i   * j + 1);
         System.out.printf("%10d * %d = %d\n", j, i + i + 2, i * j + 1);
                }


            }

        }
        printWriter.close ();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
   }
}

Solution

  • You are not writing to the printWriter, but to Console only. Write to printWriter object also in inner for loop to store the content in file. Like.

    for(int j = 1; j <= 10; j++ )
                {
                    if (i % 10 == i) {
             System.out.printf("%-2d * %-2d = %-2d", j, i + i + 1, i   * j + 1);
             System.out.printf("%10d * %d = %d\n", j, i + i + 2, i * j + 1);
    
             printWriter.printf("%-2d * %-2d = %-2d", j, i + i + 1, i
                                * j + 1);
             printWriter.printf("%10d * %d = %d\n", j, i + i + 2, i
                                * j + 1);
                    }
    

    EDIT

    I hope you tried to get the desired output, but if you still have the problem then here you go.

    private static void printTimeTable() throws FileNotFoundException {
        try {
            File file = new File("Lab Print Time Table");
    
            PrintWriter printWriter = new PrintWriter("TimeTables.txt");
    
            printWriter.println("\tTimes Tables:\n");
            for (int i = 1; i <= 10; i += 2) {
                for (int j = 1; j <= 10; j++) {
    
                    System.out.printf("%-2d * %-2d = %-2d", j, i, i * j);
                    System.out
                            .printf("%10d * %d = %d\n", j, i + 1, j * (i + 1));
    
                    printWriter.printf("%-2d * %-2d = %-2d", j, i, i * j);
                    printWriter.printf("%10d * %d = %d\n", j, i + 1, j
                            * (i + 1));
    
                }
                System.out.println("");
    
                printWriter.println("");
            }
            printWriter.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }}