Win a copy of Modern frontends with htmx this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to convert char to integer?

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello everyone. I'm currently working on a problem shown below:

Problem Description
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.

Input Specification
The input consists of four lines, each line having 4 space-separated integers.

Output Specification
Output either magic if the input is a magic square, or not magic
if the input is not a magic square.

Sample Input 1
16 3 2 13
5 10 11 8
9 6 7 12
4 15 14 1

Output for Sample Input 1
magic


How do I convert char to integer?

Here is the code I'm working on:

 
author
Posts: 23950
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Well, the Scanner class that you are using actually have a method that will return an int as the token -- that is likely your easiest option.

Otherwise, you will have to parse it. First, to extract the number tokens from the line, and then, to parse the number token to an int value.

Henry
 
Marshal
Posts: 79082
376
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch
I suggest you use code tags round your input, and maybe add spaces judicoously; with the text option in th dropdown list you can make this:-

. . .Sample Input 1
16 3 2 13
5 10 11 8
9 6 7 12
4 15 14 1. . .

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.
 
Tarius Peddle
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks.
Here is another problem. I'm getting

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 7
       at Testj22016.magic(Testj22016.java:48)
       at Testj22016.main(Testj22016.java:26)

What should I do? I ond't see anything wrong with line 48 and 26.



 
Tarius Peddle
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now I have this exception:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 12
       at Testj22016.magic(Testj22016.java:76)
       at Testj22016.main(Testj22016.java:52)

 
Campbell Ritchie
Marshal
Posts: 79082
376
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 s‍houldn't populate your array like that.
 
Campbell Ritchie
Marshal
Posts: 79082
376
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I merged your stuff with the following thread. I hope that is okay by you.
 
Tarius Peddle
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is there a way of using something else instead of split?

 
Campbell Ritchie
Marshal
Posts: 79082
376
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes. Two people have already told you to look through the methods of Scanner. You have been given good advice, so kindly take that advice.

Don't start new threads for a minor digression I shall put this post back into the original thread.
 
reply
    Bookmark Topic Watch Topic
  • New Topic