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-9The 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 183 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 36Hints:
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 :)