Hi guys first off i want to say i really enjoy this forum i just signed up but i have referenced it before. so i have a question i need help with. here is the task at hand

A file called tables.txt contains ranges that describe multiplication tables. Each line of input has the range of rows and columns used in a given table. Here is an example input:

1-3 4-6
2-4 3-9

The above input says to produce 2 tables. The first is a table of the results of multiplying the numbers 1 through 3, into the numbers 4 through 6. The output of a correct program would be the following two tables:

this should look like a table with 4,5,6 as columns and 1,2,3 as rows

4 5 6
1 4 5 6
2 8 10 12
3 12 15 18

3 4 5 6 7 8 9
2 6 8 10 12 14 16 18
3 9 12 15 18 21 24 27
4 12 16 20 24 28 32 36

Hints:

1. Use the String.split() method in the Java String class to split the first string (e.g., "1-3") in two. Use the Integer.parseInt(String) method to extract the numbers.

Do the same with the second string.

Repeat for all lines in the file.

2. Use the System.out.format() method to produce fixed width "cells" for each number.

3. Use nested for loops to create the necessary tabular appearance.

this is the code i have thus far :

import static java.lang.System.out;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Scanner;

public class Main {

    @SuppressWarnings("empty-statement")
    public static void main(String[] args) throws IOException {
        Scanner reader;

        out.println("Welcome to Multiple Intelligences ...");
        out.println("\ta program to calculate multiplication tables.\n\n");

        reader = new Scanner(new FileInputStream("tables.txt"));
        while (reader.hasNext()) {

            String col = reader.next();
            String rows = reader.next();
            reader.nextLine();

            String[] multiplicands = col.split("-");
            String[] multipliers = rows.split("-");

            // FOR STUDENT TO COMPLETE
            // get reader.next() and split it into an array called multiplicands
            // get reader.next() and split it into an array called multipliers
            // do a reader.nextLine() to junk the rest of the line

            int iStart = Integer.parseInt(multiplicands[0]);
            int iStop = Integer.parseInt(multiplicands[1]);
            int jStart = Integer.parseInt(multipliers[0]);
            int jStop = Integer.parseInt(multipliers[1]);

            // FOR STUDENT TO COMPLETE
            // print the top line of the table using a for loop

            
            for (int i = iStart; i <= iStop; i++);
            {
            out.println(rows);

            }


            // FOR STUDENT TO COMPLETE
            // use a nested for loop to print the rows of the table

            for (int i = iStart; i <= iStop; i++);
            {

                for (int j = jStart; j <= jStop; j++);
                {
                    //                out.println(i*j);
                }


            }
        }

        // FOR STUDENT TO COMPLETE
        // print an exit message

    }
}

I'm getting stuck on printing the loops if i coded the loops correctly. for example I'm getting an error with this statement [out.println(i*j);] its telling me it can't find i or j but i initialized them in the loop? its probably really simple but i always get hung up on a line or two. any thoughts would be awesome.
Thanks guys :)

Recommended Answers

All 6 Replies

so i fixed my nested for loop and it looks like this:

int i;
            int j;
            int row;
            for (i = iStart; i <= iStop; i++);
            {
                for (j = jStart; j <= jStop; j++);
                {
                    row = i * j;
                    out.println(row);
                }

but something is still wrong :/

What is wrong?? Are you having error messages?? Or the program outputs unexpected results??

Ok well this is what i have been playing with

// FOR STUDENT TO COMPLETE
            // print the top line of the table using a for loop
            System.out.print(" ");
            for (int j = iStart; j <= iStop; j++) {
                System.out.format("%4d", j);
                }
                System.out.println();
            
            // FOR STUDENT TO COMPLETE
            // use a nested for loop to print the rows of the table

            for (int i = iStart; i <= iStop; i++) {
                for (int j = jStart; j <= jStop; j++)
                {
           
                }

            }

when i try and print the first column header i get all the numbers in the text file. also i am getting the numbers for the rows as the column numbers. the first three columns should 4, 5, 6 and i'm getting 1, 2, 3

output:
Welcome to Multiple Intelligences ...
a program to calculate multiplication tables.


1 2 3 <---should be 4 5 6
2 3 4
7 8 9 10
BUILD SUCCESSFUL (total time: 1 second)

the other 2 lines are for another two tables so im having trouble printing the table.... sorry if im not clear i will try and organize my thoughts...
thanks :)

so i'm stilling playing around with this and this is what i have

// FOR STUDENT TO COMPLETE
            // print the top line of the table using a for loop

            for (int j = jStart; j <= jStop; j++) {
                System.out.format("%4d", j);
            }
            System.out.println();

            // FOR STUDENT TO COMPLETE
            // use a nested for loop to print the rows of the table

            for (int i = iStart; i <= iStop; i++) {
                out.println(i);
                for (int j = jStart; j <= jStop; j++) {
                    //System.out.printf("%4d",j*i);
                }
            }

        }
        out.println("");
        out.println("Thank You for useing a  program to calculate multiplication tables.");

its printing the table correctly but i am having trouble filling the table. i know is gotta be i*j but i need that to go on the same row as the row number. here is my output below:

compile:
run:
Welcome to Multiple Intelligences ...
        a program to calculate multiplication tables.


   4   5   6
1
2
3
   3   4   5   6   7   8   9
2
3
4
   9  10  11  12
7
8
9
10

Thank You for useing a  program to calculate multiplication tables.
BUILD SUCCESSFUL (total time: 1 second)

Hello Oblivious21, I think i found your problem. Instead of System.out.printf("%4d", i*j) you want to have System.out.format("%4d", i*j) then after one curly brace (}) you want to write System.out.println(); and it should work. Here is my code:

System.out.println();



            //Nested loop 
            for (int i = iStart; i <= iStop; i++) {
                System.out.print(i);
            for (int j = jStart; j <= jStop; j++) {
               System.out.format("%4d", i*j);
                }
                System.out.println();
                
            }
        }
        //exit message
        System.out.println("\nThank you for using Multiple Intelligences!");


    }
}

@MathMajor2011: it worked :) :) thank you so much! i always get hung up on the smallest things. here's what the output looks like:

compile:
run:
Welcome to Multiple Intelligences ...
        a program to calculate multiplication tables.


   4   5   6
1
   4   5   6
2
   8  10  12
3
  12  15  18
   3   4   5   6   7   8   9
2
   6   8  10  12  14  16  18
3
   9  12  15  18  21  24  27
4
  12  16  20  24  28  32  36
   9  10  11  12
7
  63  70  77  84
8
  72  80  88  96
9
  81  90  99 108
10
  90 100 110 120

Thank You for useing a  program to calculate multiplication tables.
BUILD SUCCESSFUL (total time: 0 seconds)

i just have to play with the formatting little and its perfect.
Thanks again

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.