r/javahelp • u/C0fk0 • Apr 17 '21
How to convert from Int [][] to Char [][]
Hello, I need some help on how to convert an Int [][] to Char [][]. The values in Int table are numbers from 0 to 5 and I need to change their value to a certain character for each of them (?:/% _). Thank you for your time and help in advanced :)
8
u/xenomachina Apr 17 '21
First, break the problem down into smaller pieces. Three "phases" seem reasonable here:
Create a method to translate a single int into a char
Create a method that converts an int[] into a char[] using the above
Create a method that converts an int[][] into a char[][] using the above
For the first phase, I'd recommend you start with a chain of ifs:
if (x == 0) {
...
} else if (x == 1) {
...
} else {
...
}You can alternatively use a switch statement, a lookup table in a map, or (since you're dealing with a fixed, contiguous range of integers) a lookup table in an array. Any of these seem pretty reasonable for this case, but you have a very small number of cases, and the chain of ifs is the most straightforward approach for a beginner. Once you've got that working, try changing it into a switch statement. Lookup tables are great for dealing with larger numbers of inputs, but for 5 possibilities it's really overkill.
For the other two phases, I'd recommend you use a for-loop:
for (int i = 0; i < input.length; i++) {
output[i] = methodThatTransformsElement(input[i]);
}Both phases are iterating+transforming. You could alternatively use streams+map, which is a good technique to learn later.
3
9
u/Roachmeister Java Dev Apr 17 '21
I would probably make a Map<Integer, Character> that gives the conversions. Then loop through the int array and create the char array by doing a lookup in the map.
6
u/C0fk0 Apr 17 '21
Thank you very much for answer :)
I never created a map before so how do create it and set my values?
16
u/iPissVelvet Apr 17 '21
Don’t take this advice as rude, because I know you’re learning!
But try googling your question. A big skill is knowing “what to Google”. For this one, I would suggest “java how to create map”
1
u/C0fk0 Apr 17 '21 edited Apr 17 '21
Of course not, I completly agree with you. I am very thankfull for his help and I know that looking for solutions myself will help me improve faster :) I solved my issue with string and .charAt(), but still wanted to check how would he do it with maps since I am sure I will have to deal with it once in the future.
3
u/trisul-108 Apr 17 '21
You might get better responses this way, if you say "Please look at what I have done, is there a better way to do this" then people might take the time to answer. When you just ask how to do something simple, people think you just want them to do your work for you, so they ignore you.
4
u/C0fk0 Apr 17 '21
Will keep this in mind for my next questions :)
2
u/iPissVelvet Apr 17 '21
No worries. Learning how to ask questions is probably the most critical skill in software and programming, and is worth improving at no matter your skill level.
2
u/AvoidingCares Apr 17 '21 edited Apr 17 '21
So you need a method that reads the Int[][] and then returns a Char[][]. I would recommend a switch statement to do this. Google the switch case syntax carefully because it is easy to mess up. But it's very efficient to do this sort of thing, as opposed to nested if statements.
Also make sure you include "break;" in between each case. If you have more than 3 cases and don't include it, weird things can happen.
2
2
u/missionmeme Apr 17 '21
There are a few ways to do this, the one you probably know already is pass each value through 5 if-else statements. A faster/cleaner way would run through a switch statement. Probably the way I would go if you have 5 inputs with 1 to one outputs. Another way yet would be create a hashmap that links the ints to the chars you want. There's 2 situations I would use the hashmap instead of the switch. Once the amount of choices becomes very long(10+) and/or if you don't know what the inputs are going to need to output when your writing the code.
1
2
u/HotRodLincoln Apr 17 '21
Do you? I mean it's just two nested for loops and a switch.
It's likely that the better solution is to do make a function that gets the values in the int array, and translates them just in time. That's going to be O(1) instead of O(N).
0
Apr 18 '21
[deleted]
3
u/AreTheseMyFeet Apr 18 '21
You can cast an
intto acharbut in this instance theintvalues do not correspond directly to thecharvalues.
The numbers here are a key index rather than the numeric values of chars.
•
u/AutoModerator Apr 17 '21
Please ensure that:
Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions
You include any and all error messages in full
You ask clear questions
You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.
Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar
If any of the above points is not met, your post can and will be removed without further warning.
Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.
Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.
Code blocks look like this:
You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.
If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.
To potential helpers
Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.