Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Java multiplication table, Writer writes in second line instead of first

It’s supposed to be a multiplication table in the shape of a square,
but the writer doesn’t write the numbers in the first line and the first column.

try {
    File file = new File("multi.txt");
    // FileWriter Writer = new FileWriter("multi.txt");

    BufferedWriter Writer = new BufferedWriter(new FileWriter(file));
        
    for (int o = 1; o <= n; o++) {
        Writer.write ("\n");
        for (int s = 1; s <= n; s++) {
            if (o * s < 10) Writer.write( " ");
            if (o * s < 100) Writer.write( " ");
            // System.out.print( "  " + z * s);
            Writer.write ("    " + o * s);
        }
    }
    Writer.close();

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

This should do it:

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;

public class tmp {

    public static void main(String[] args) {

        try {

            File file = new File("multi.txt");
            // FileWriter Writer = new FileWriter("multi.txt");

            BufferedWriter Writer = new BufferedWriter(new FileWriter(file));
            int n = 10;
            
            for(int o = 1; o <= n; o++) {
                for(int s = 1; s <= n; s++) {
                    Writer.write(o*s+"\t");
                }
                Writer.write("\n");
            }
            Writer.close();
        } catch (Exception e) {
            System.out.println("exception thrown");
        }
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading