So I have this question: Write a program that creates a constant MULTIPLE of type int. Set MULTIPLE to 3. Output the times table between 1 and 10 for MULTIPLE. Modify the program to output the times table of 7.

And so far I've written this:

//5. - Homework
class Hw8{
    public static void main (String args[]){
        final double MULTIPLE = 3;
        int a = 1 * 1;
        int b = 2 * 1;
        int c = 3 * 1;
        int d = 4 * 1;
        int e = 5 * 1;
        int f = 6 * 1;
        int g = 7 * 1;
        int h = 8 * 1;
        int i = 9 * 1;
        int j = 10 * 1;
        System.out.println(a, b, c, d, e, f, g, h, i, j);

Though I have an error in the final part can anyone point out what I did wrong?

Edited by Nick Evan: Fixed formatting

8 Years
Discussion Span
Last Post by jon.kiparsky

what I did wrong?

Can you give us a clue as to what is right and what is wrong?
Do you get an error message? Post it here.

Edited by NormR1: n/a


Can you give us a clue as to what is right and what is wrong?
Do you get an error message? Post it here.

I get this error:

--------------------Configuration: <Default>--------------------
C:\Users\Stephane\Documents\JCreator Programs\Hw8.java:15: cannot find symbol
symbol : method println(int,int,int,int,int,int,int,int,int,int)
location: class java.io.PrintStream
System.out.println(a, b, c, d, e, f, g, h, i, j);
1 error

Process completed.


Have you read the API doc for the println() method? Do you see any overloaded versions of the println() method that takes so many ints?

If all the ints were to print they might look like this: 2354323421123123545434344545
All digits with no spaces.

Try to build a String for the println() method to print.


Ah. Yeah, println expects a String, or something it can convert to a string. So you can println(a) and it'll convert it to a String and give you the ascii representation of a (in this case, 1 * 1, or 1)

So println(int, int, int...) is not something the compiler is capable of handling. There is a string concatentaion operator, +, but if you just do println (a+b+c...) you'll find that it does the addition before the concatenation. Not what you want.

So you could force the compiler to treat everything as strings, by giving it an empty string: println(""+a+b+c...) - but then you just have a string of digits, and no way to separate them. So you try inserting the spaces println(""+a+" " +b+" "+c+" "+...). Well, that works, kinda sorta, but it seems pretty grotesque, right?
Yeah - the trouble is that you're handling these as independent variables, which means you can't treat them as a group. Try working with the data as an array of ints, and using a for loop to do your multiplication and your printing. You say this is for a class - look at whatever text you're using and read up on arrays and for loops, and try rewriting this.

Note - None of this addresses the problem that you're not actually doing what the assignment asks for - you never multiply by your MULTIPLE constant.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.