Hello everyone. I'm currently working on a problem shown below:
Magic Squares are square arrays of numbers that have the interesting property that the numbers in
each column, and in each row, all add up to the same total.
Given a 4x4 square of numbers, determine if it is magic square.
The input consists of four lines, each line having 4 space-separated integers.
Output either magic if the input is a magic square, or not magic
if the input is not a magic square.
become ... Sample Input 1
or even ... Sample Input 1
That would look a lot better
I agree with Henry; use the methods of Scanner, one of which probably does exactly what you want. Have you read something in a book which tells you that a char is a letter, which is not actually true. If you look in the Java® Language Specification (=JLS), you find that a char is an integer in its own right, so there is no need to do any conversion to an integer. Beware: the JLS can be very difficult to read. Now, the number value of a char depends on its Unicode/ASCII equivalent; if you look in that link you find that '0' is equivalent to U+0030, which means 0x30, both numbers being in hex. You can convert that to 0 with c − '0', whereas charValue() will give 0x30, Which you will have to covert to 0 with &minus' '0' again.
There is no need for a cast to int because the assignment to int does that automatically. The use of \n for line end does not work, since you will pick up the \r used by Windows as a real character.
Conclusion: Henry is right: use the Scanner method. Much much easier and less error‑prone.
If you are using an array of inputs, it must have at least 16 elements. You don't say anywhere that you require 16 items, and your array input doesn't seem to produce a 16‑element array to pass to the magic method. Don't use a Scanner method in combination with parseInt. Use the Scanner method which produces an int directly. At least that way you will actually get a 16‑element array. Something is going wrong with the splitting, but I shan't try to sort that out because you shouldn't populate your array like that.