Jerry
Jerry

Reputation: 91

Adding integers to Multiplication Table

Using the command line command:

java class -table 7

is supposed to print a table:

 7     7     7     7     7     7     7     7     7     7
 7     8     9    10    11    12    13    14    15    16
 7     9    11    13    15    17    19    21    23    25
 7    10    13    16    19    22    25    28    31    34
 7    11    15    19    23    27    31    35    39    43
 7    12    17    22    27    32    37    42    47    52
 7    13    19    25    31    37    43    49    55    61
 7    14    21    28    35    42    49    56    63    70
 7    15    23    31    39    47    55    63    71    79
 7    16    25    34    43    52    61    70    79    88

which is a 10 x 10 multiplication table with "7" added to each number. In other words, it adds 7 to each number in the following table:

 0     0     0     0     0     0     0     0     0     0
 0     1     2     3     4     5     6     7     8     9
 0     2     4     6     8    10    12    14    16    18
 0     3     6     9    12    15    18    21    24    27
 0     4     8    12    16    20    24    28    32    36
 0     5    10    15    20    25    30    35    40    45
 0     6    12    18    24    30    36    42    48    54
 0     7    14    21    28    35    42    49    56    63
 0     8    16    24    32    40    48    56    64    72
 0     9    18    27    36    45    54    63    72    81

The following code is what I have so far. It prints out the original table starting at 0. However I do not understand how I am suppose to add args[1] to each value. (args[1] being 7 in the case above). How do I add args[1] to each value in the table?

I am also supposed to print "Argument count mismatch" if the user does not put exactly 1 int after -table in the command line.

For the code I have, it prints "Argument count mismatch" at the appropriate times, but it prints for all numbers, so it prints the line 100 times. I understand that I should put that part of the code outside of the loops, because I only want it to print once. How could I implement it to do that?

private static void table(String[] args) {
    int[][] table = new int[10][10];
    for (int i = 0; i < 10; i++) {
        for (int j = 0; j < 10; j++) {
            if (args.length != 2)
                System.out.println("Argument count mismatch");
            else
                System.out.printf("%6d", i * j);  
        }
        System.out.println();
    }    
}

Upvotes: 1

Views: 179

Answers (3)

Ordous
Ordous

Reputation: 3884

You pretty much already answered yourself:

You should put the checking outside the loop. args does not change from number to number, there is not point to check it every time you need to add the number.

It would be a lot better to decompose the problem into 2 parts:

  • Validate the arguments and find the number you need to add.
  • Print the actual table with the number added.

After you have completed the first part, the second is trivial. You have some int toAdd with the number, so just print i * j + toAdd instead of i * j.

The first part is a bit more tricky:
You need to first verify the input, i.e. there are exactly 2 arguments and the second is a valid number. You also need to actually convert the String to a number. The checking number and conversion part can be done in one go via Integer.parseInt().

Hence, the combined code, in shorthand:

if (args.length != 2) {
    System.out.println("Argument count mismatch");
    return;
}
int toAdd;
try {
    toAdd = Integer.parseInt(args[1]);
catch (NumberFormatException e) {
    System.out.println("Second argument is not a number!");
    return;
}

// Your modified loop goes here

Upvotes: 0

mistahenry
mistahenry

Reputation: 8724

Okay, well parse the second command line argument as an Int and add it to each value:

private static void table(String[] args) {
    int numToAdd = Integer.parseInt(args[1]);
    int[][] table = new int[10][10];
    for (int i = 0; i < 10; i++) {
        for (int j = 0; j < 10; j++) {
            System.out.printf("%6d", i * j + numToAdd);  
        }
        System.out.println();
    } 
}

Not sure why you created a table double array, since you never use it.

And the output:

java Test -test 7
     7     7     7     7     7     7     7     7     7     7
     7     8     9    10    11    12    13    14    15    16
     7     9    11    13    15    17    19    21    23    25
     7    10    13    16    19    22    25    28    31    34
     7    11    15    19    23    27    31    35    39    43
     7    12    17    22    27    32    37    42    47    52
     7    13    19    25    31    37    43    49    55    61
     7    14    21    28    35    42    49    56    63    70
     7    15    23    31    39    47    55    63    71    79
     7    16    25    34    43    52    61    70    79    88

I would create a different method:

public static boolean validateArgs(String[]args){
   //all code to validate the args here.  
   //print validation specific errors here
}

Then your main:

public static void main(String[]args){
    if(validateArgs(args)){
         table(args);
    }
}

Upvotes: 3

D&#225;vid Horv&#225;th
D&#225;vid Horv&#225;th

Reputation: 4320

For example:

private static void table(String[] args) {
    int[][] table = new int[10][10];
    if (args.length != 2) {
        System.err.println("Argument count mismatch");
        exit(1);
    }
    for (int i = 0; i < 10; i++) {
        for (int j = 0; j < 10; j++) {
            System.out.printf("%6d", i * j + 7);  
        }
        System.out.println();
    }    
}

(If you don't prefer exit, use if...else instead.)

Upvotes: 1

Related Questions