This week's book giveaway is in the Beginning Java forum.
We're giving away four copies of Murach's Java Programming and have Joel Murach on-line!
See this thread for details.
Win a copy of Murach's Java Programming this week in the Beginning Java forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

filling a 2D array with alphanumeric values -- type errors  RSS feed

 
Frank Poupard
Ranch Hand
Posts: 65
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I'll like to store some mixed data types, i.e int and char in a 2 dimensional array. I did some conversion but I'm still having issues.
The goal is to store in a for example 15*15 grid, alphanumeric values, 'A0', 'A1', till 'A14' for the first rank, then 'B0', 'B1' etc... to the last 'O14'.
I'm doing some stuff with that afterwards but just want to focuse on the following code :



the last line gives this error :

Multiple markers at this line
- Type mismatch: cannot convert from
String to int
- Type mismatch: cannot convert from
String to int


Any idea what I should do ? Thanks in advance;
 
Rob Spoor
Sheriff
Posts: 21019
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Each index should be an int or something that's assignable to one, so you can also use byte, short or char. One of them should probably be lig, the other one col. Definitely not the two Strings you're using now.
 
Frank Poupard
Ranch Hand
Posts: 65
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:Each index should be an int or something that's assignable to one, so you can also use byte, short or char. One of them should probably be lig, the other one col. Definitely not the two Strings you're using now.

Thanks but could you elaborate a bit more? How to deal with the type char here ?
 
Fred Kleinschmidt
Bartender
Posts: 544
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no reason to convert the characters or integers to intermediate Strings that contain only a single character.
Note here that the indices for the grid array are both ints, and
xxx is formed by generating the appropriate letter and digit combination.
For example, ('A'+ 0) results in the character 'A', and ('A'+1) results in the character 'B', etc.
Also, (s + n) where s is a String and n is an integer, will create a String that concatenates the string representation of n to the.string s.
Thus ("" + 'A' + 3) results in the String "A3".

You could also create the String alphas ="ABCDEFGH..." and just refer to alphas.charAt(col) to get the letter.

 
Frank Poupard
Ranch Hand
Posts: 65
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I solved the problem in this way :

There may be better way though!
 
Henry Wong
author
Sheriff
Posts: 23260
124
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Frank Poupard wrote:
There may be better way though!


Well, the code is only around 10 lines long. It is short and clear... meaning while I can think of two (or maybe three) ways to clean it up a bit, the changes would be minor.

What kind of "better way" you are looking for?

Henry

PS... have a cow for returning to post your solution.
 
Fred Kleinschmidt
Bartender
Posts: 544
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your solution is incorrect:
will not get you the correct string. I think you have interchanged  "num" and "letter".
You could eliminate the intermediate String variable and just use
Note that the order of placing "" is important.
To ensure that everything is done as a String, start with a String (i.e., "").
If you write

then the result of the first operation ('A' + 2) will be 'C', and then you add "" which uses String concatenation, resulting in "C".
When you instead write the result of the first operator is a String "A", and the result of the second operator is "A2"


 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!