#Inserting Numbers As Strings Into A Hashmap

1 messages ยท Page 1 of 1 (latest)

summer pebbleBOT
#

Detected code, here are some useful tools:

#

<@&987246399047479336> please have a look, thanks.

fluid quail
#

because hashmap doesn't have a defined iteration order

#

due to how it stores data

#

if you want iteration order to be consistent with insertion order, use LinkedHashMap

#

well, for this kind of structure you don't even need a map

#

an array will do

#

another option is to use a TreeMap where you provide a comparator which sorts your entries

#

it might be

sinful shadow
#

which order to you want ?

sinful shadow
#

alphabetical order ? numeric order ?
of what ? keys ? values ?

summer pebbleBOT
#

Detected code, here are some useful tools:

sinful shadow
#

No it doesn't, linked variant only keep insertion order

fluid quail
#

it's sensitive to insertion order
TreeMap will preserve it no matter the insertion order

fluid quail
sinful shadow
#

you can't insert in an array

fluid quail
#

array is not sensitive because you manually select a position to insert

sinful shadow
#

Being sensitive to insertion order is the only reason why linkedhashmap exists

fluid quail
#

linkedhashmap always appends to the end

sinful shadow
#

No I don't, you manually say where you want to put elements in an array, you can't insert

#

insert means that the structure grows in size

#

an array has fixed size and you tell where you want elements to be

#

so not only you can't insert

#

but order is also defined by the user

#

which is totally unrelated to other data structures

#

because you inserted in same order

#

also

#

what's the point of this

#

why 1=A ?

#

is it always equal to A ?

#

but

#

why can't you just do 'A' - character

#

?

summer pebbleBOT
#
Alathreon's result

Snippets

Snippet 24, VALID

for(char c = 'A'; c <= 'Z'; c++) {
  print(c);
}```
## System out

ABCDEFGHIJKLMNOPQRSTUVWXYZ```

sinful shadow
#

Do you understand what I mean here ?

#

you can do aritchmetic on chars

summer pebbleBOT
#
Quinteger's result

Snippets

Snippet 23, REJECTED

public statis char numberToChar(int number) {
    if (number < 0 ||| number > 25) throw new IllegalArgumentException();
    return 'A' + number;
}```
## [WARNING] The code couldn't end properly...
Problematic source code:
```java
public statis char numberToChar(int number) {
    if (number < 0 ||| number > 25) throw new IllegalArgumentException();
    return 'A' + number;
}```
Cause:
The code doesn't compile:
';' expected
';' expected
';' expected
illegal start of expression
cannot find symbol
  symbol:   variable statis
  location: class 
unreachable statement
unreachable statement
variable number might not have been initialized

Remaining code:
```java

print(numberToChar(3));```
## System out
[Nothing]
fluid quail
#

cool, I made a typo

summer pebbleBOT
#
Quinteger's result

Snippets

Snippet 24, REJECTED

public static char numberToChar(int number) {
    if (number < 0 ||| number > 25) throw new IllegalArgumentException();
    return 'A' + number;
}```
## [WARNING] The code couldn't end properly...
Problematic source code:
```java
public static char numberToChar(int number) {
    if (number < 0 ||| number > 25) throw new IllegalArgumentException();
    return 'A' + number;
}```
Cause:
The code doesn't compile:
illegal start of expression

Remaining code:
```java

print(numberToChar(3));```
## System out
[Nothing]
sinful shadow
#

three |

#

๐Ÿ™‚

#

you can do it ๐Ÿ™‚

#

why ?

fluid quail
#

discord is the new IDE

#

fine

summer pebbleBOT
#
Quinteger's result

Snippets

Snippet 25, VALID

public static String numberToChar(int number) {
    if (number < 0 || number > 25) throw new IllegalArgumentException();
    return String.valueOf('A' + number);
}```
### Snippet 26, VALID
```java

print(numberToChar(3));```
## System out

68```

fluid quail
#

lol 68

summer pebbleBOT
#
Quinteger's result

Snippets

Snippet 25, VALID

public static String numberToChar(int number) {
    if (number < 0 || number > 25) throw new IllegalArgumentException();
    return String.valueOf((char)('A' + number));
}```
### Snippet 27, VALID
```java

print(numberToChar(3));```
## System out

D```

fluid quail
#

there

sinful shadow
#

???

#

why ?

#

But it uses strings ?

sinful shadow
#

converting char to string is just a single line, so I have no idea what you are asking... and why do you wnat strings when you specifically need to work with chars ?

#

heh

#

?

#

How is string related to a job ?
How does it even make sense ?

#

are you a js dev or what ?

#

I don't understand what you are trying to do
If you just want to print this, you could use my code from before and just slightly modify the print

summer pebbleBOT
#
Alathreon's result

Snippets

Snippet 25, VALID

for(char c = 'A'; c <= 'Z'; c++) {
  print((c - 'A') + " = " + c);
}```
## System out

0 = A1 = B2 = C3 = D4 = E5 = F6 = G7 = H8 = I9 = J10 = K11 = L12 = M13 = N14 = O15 = P16 = Q17 = R18 = S19 = T20 = U21 = V22 = W23 = X24 = Y25 = Z```

sinful shadow
#

@craggy bobcat see

#

well

#

forgot to add commas

summer pebbleBOT
#
Alathreon's result

Snippets

Snippet 26, REJECTED

IntStream.range('A', 'Z').mapToObj((c - 'A') + " = " + (char)c).collect(Collectors.joining(", "))```
## [WARNING] The code couldn't end properly...
Problematic source code:
```java
IntStream.range('A', 'Z').mapToObj((c - 'A') + " = " + (char)c).collect(Collectors.joining(", "))```
Cause:
The code doesn't compile:
cannot find symbol
  symbol:   variable c
  location: class 
cannot find symbol
  symbol:   variable c
  location: class 

## System out
[Nothing]
#
Alathreon's result

Snippets

Snippet 27, VALID

IntStream.range('A', 'Z').mapToObj(c -> (c - 'A') + " = " + (char)c).collect(Collectors.joining(", "))```
jshell> `"0 = A, 1 = B, 2 = C, 3 = D, 4 = E, 5 = F, 6 = G, 7 = H, 8 = I, 9 = J, 10 = K, 11 = L, 12 = M, 13 = N, 14 = O, 15 = P, 16 = Q, 17 = R, 18 = S, 19 = T, 20 = U, 21 = V, 22 = W, 23 = X, 24 = Y"`
## System out
[Nothing]
sinful shadow
#

alright

#

@craggy bobcat here you go

summer pebbleBOT
#

Detected code, here are some useful tools:

sinful shadow
#

emulate what ? this does nothing

#

but you are not using map

#

so your code does nothing

#

please show what you actually want to do

#

Filling a map with values isn't what you want

#

since it does nothing in its own

polar pewter
#

"most efficient" is relative

#

you could use a loop to populate the map, with a bit of calculation

#

since numbers & letters already map to each other through ASCII

#

the ASCII value for A is 65. so just add 65 to whatever the number is, cast to char, then convert to String

#
int num = 0;
char value = num + 65; // 'A'
String letter = String.valueOf(value); // "A"```
sinful shadow
#

but how ????
yes I know that, this is obvious
the question is how

#

why are you trying so hard to avoid answering my question

sinful shadow
# polar pewter ```java int num = 0; char value = num + 65; // 'A' String letter = String.valueO...

please don't use 65 but 'A' like I and Quniteger did earlier
But the problem isn't that, the problem is that we still don't know how he is gonna use the app, from what we know, he wants to iterate on it in a certain order, which isn't the job of a map at the first place, we don't know if he will edit the map, etc
And he is constantly dodging to answer the question of how he is using the map -_-

polar pewter
#

so i will definitely not do what you mentioned, cause it doesnt express what i was saying ๐Ÿ˜…

#

i get what you mean, 65 is a magic number. but the purpose is to emphasize the numeric relationship

sinful shadow
polar pewter
#

yes, but that doesnt mean they understand the concept or the code you showed. you proved it, yeah

summer pebbleBOT
#

Detected code, here are some useful tools:

polar pewter
#

why would order matter?

#

as long as 0 maps to A, why does it matter that 0 -> A appears in the map before 1 -> B?

#

that seems to be the question others were wondering. why do you need order? why do you need to iterate?

#

based on your example, doesnt seem like either is needed

#

still have any questions/concerns?

#

could use a loop to populate the map, or a stream, etc..

#

you could use what Alathreon suggested, in terms of populating the map

#

could use a Stream like they did

#

or you could use a loop

#

efficient in what way?

#

high performance code may be messy, and prolongs the time it takes to find bugs. its not efficient for finding bugs

#

"least amount of code" is not a good metric to go by

#

that small amount of code could be cryptic, hard to understand

#

readability & performance

#

and its a balance. the more readable your code becomes, the less performant it may be

#

the more performant your code is, the less readable may be

#

always prefer readability. worry about performance when it becomes a problem

#

when a bug appears, you want to be able to solve it ASAP

#

some bugs could ruin the entire app

#

so readability is critical

#

performance is good. you need it these days. but if a bug breaks your system, "being fast" wont matter

#

doesnt matter how fast your app is, if it doesnt work

#

based on the project, the "bigger picture", and in your specific situation....

#

it doesnt matter

#

solve this, move onto the next problem

#

spent way too much time on this

#

you can even keep what you already got

summer pebbleBOT
#

Detected code, here are some useful tools:

polar pewter
#

there are faaaaaaar bigger problems out there to be solved

#

i would recommend "stop putting so much time into it"

#

its not a bottleneck

#

its not a bug

#

could it be improved? sure. but are there more important things to worry about? yes, 100% yes

#

id say consider this problem solved & move onto the next. if i were to write it, id probably use Alathreon's approach with the stream

polar pewter
#

but thats only because i know this project wont be touched again. its a throw-away

#

if you plan on using this in a larger system, & have more concerns, you should talk about that

polar pewter
#

now you sound like the who were waiting on you ๐Ÿ˜‰

#

i'd use the stream version, get it done with quickly

sinful shadow
#

Then you can replace all your map by Quinteger code

#

none

#

just do the single line of code that quinteger gave you

#
HashMap<String, String> map = new HashMap<String, String>();

map.put("0", "A");
map.put("1", "B");
map.put("2", "C");
map.put("3", "D");
map.put("4", "E");
map.put("5", "F");
map.put("6", "G");
map.put("7", "H");
map.put("8", "I");
map.put("9", "J");
map.put("10", "K");
map.put("11", "L");
map.put("12", "M");
map.put("13", "N");
map.put("14", "O");
map.put("15", "P");
map.put("16", "Q");
map.put("17", "R");
map.put("18", "S");
map.put("19", "T");
map.put("20", "U");
map.put("21", "V");
map.put("22", "W");
map.put("23", "X");
map.put("24", "Y");
map.put("25", "Z");

...

String character = map.get(String.valueOf(remainder));

Instead of this

#
...

String character = String.valueOf('A' + remainder);
#

you do this

sinful shadow
polar pewter
sinful shadow
polar pewter
#

to populate the map

#

similar to what your stream version does

sinful shadow
#

But you don't need the map

#

He just want to do 'A' + number

polar pewter
#

they want mappings, having a map is the elegant way to approach it

sinful shadow
#

but why ?

#

they are just overcomplicating their life

polar pewter
#

its not a requirement, and we dont know where they plan to use this. sure, they could have a function which returns the letter corresponding to the number

#

but then again, why not have all maps implemented like that?

#

because this is an object environment

sinful shadow
#

?

#

I asked why they are using a map

polar pewter
#

because they want mappings

sinful shadow
#

they said they only want it so they can transform an int into a string

#

just do the math

sinful shadow
polar pewter
#

yeah, its mapped using ascii. you can take advantage of that

sinful shadow
#

just do 'A' + number

polar pewter
#

yes

sinful shadow
#

not only that

#

but that's what Quinteger gaves hours ago

polar pewter
#

and?

sinful shadow
#

and also what I used in my loop at the start of this question

#

we are going in a loop here

polar pewter
#

what value are you adding to this post with all this

sinful shadow
#

this is annoying

#

anyway

#

just do 'A' + number

polar pewter
#

they can do that. they want a map

sinful shadow
polar pewter
#

check the post, and look at how consistently they mention how they want a map through-out this post

sinful shadow
#

then why do they want a map ?

polar pewter
#

why are you still begging for this? you already gave them the anti-map solution

sinful shadow
#

no

#

They don't want the map

#

they just don't know what the other solutions are

#

You might notice that they didn't understand how quinteger solve their problem

polar pewter
#

they might not want the map. so give them solutions for both & let them decide based on what you guys have said

sinful shadow
#

You can't just say that they want the map

#

and even if you say that

#

you have to explain why

polar pewter
#

they said they wanted the map

sinful shadow
#

but they might not know what is better for them

#

expecially here

#

where the better solution isn't a map

polar pewter
#

yeah, that doesnt mean they're wrong though

sinful shadow
#

based on what they said

polar pewter
sinful shadow
polar pewter
#

all options were given

#

i dont get what you're going on about

sinful shadow
#

here
"full use of the map"

polar pewter
#

if they object, they can post

sinful shadow
polar pewter
#

im not saying a map is needed. im saying they never said it

#

and you know what they say about assumpsions

#

so let them chime in, if thats the case. if they dont need a map, cool, they have a solution for that too now

#

this is all bloat

sinful shadow
#

So wait and see I guess

polar pewter
#

you can introduce a function which performs the conversion

#
String toLetter(int number)```
#
var letter = toLetter(0); // A```
#

it'll perform calculations each time to determine which letter should be used

#

that is Integer -> String

#

it accepts an int, it returns a String

#

im not gonna feed you the code ๐Ÿ˜…

#

you'd just take the number, perform the calculation that we've mentioned before

#

convert to char, convert that char to String

polar pewter
#

take a number, convert it to char using the ASCII offset

#

how would toString help here?

#

primitives do not have a toString method. you could use String.valueOf, but it's not needed

#

there already exists a mapping between numbers & letters. it's ASCII

#

all you're doing is taking advantage of that mapping system, rather than using your own

#

yes, it can be done without an array too

#

i wouldn't recommend using an array

#

you want mappings. use a Map in that case

#

an array could be used for mapping, but it's not suitable for your situation

#

could have array[65] = "A"

#

because it doesnt provide good mapping

#

array[65] = "A", thats a good mapping

#

but its index 65 of the array

#

indices 0-64 will go unused

#

so thats wasted space, room for error (what if someone does array[63]?)

#

"map" is the concept that matters

#

whether it's a hash map, or a tree map...

#

that depends on your use case

fluid quail
#

What I wrote yesterday

polar pewter
#

sounds like a hash map would be fit

polar pewter
#

but yes, this is what quinteger & ala have been on about

fluid quail
#

And if you want to cache the strings and not recreate them every time, you can just use an array

polar pewter
#

may as well just use a map, at that point

fluid quail
#

Well, an array will definitely have the required order if a need arises to iterate all entries

polar pewter
#

that's the benefit of using a map, being able to keep that data in memory, not having to re-calculate

#

quinteger was just suggesting that you can use an array to possibly improve performance of the function mentioned before

#

instead of having to re-calculate each time, could have an array store frequently-requested values

#

i was just saying a Map would be the better approach, since it's less complex & covers the whole "have to recalculate" problem

#

it's more descriptive (we have "mappings", a "map" is the tool)

#

it handles the same problem (not having to re-calculate)

#

yes, many

#

hence why i recommend a map

fluid quail
#

Empty index?

polar pewter
#

you can't make the mistake of accessing an empty index

polar pewter
#

array[65] = "A"

#

what about 0-64?

fluid quail
#

If the task is to transform numbers 0 to 25 into letters, there would be no empty indices

#

You have an array of 26

polar pewter
#

yes, you can manually write out the array java array[0] = "A" array[1] = "B"

#

thats what they were doing initially, with the map

fluid quail
#

You can also fill it with a loop

polar pewter
#

yes

#

they could just use a Map

#

it's higher level, provides more capability. should always prefer higher-level abstractions

#

if you want mappings, you should prefer a Map, as it gives you all the ancillary benefits, functions that are focused on mapped data

#

could achieve mapping through arrays, could take advantage of ASCII mappings.. but in the end, what matters is that your code does what it needs

#

and that you can interact with that code fluently

summer pebbleBOT
#

Detected code, here are some useful tools:

sinful shadow
#

I am against because the map is useless, you could just do 'A' + number instead of the whole map

#

add parhentesis around 'A' + 26

polar pewter
#

im not against it

#

use whatever fits best for your situation

sinful shadow
#

Ah oups valueOf

#

and cast it to char

#

char*

polar pewter
#

feels like enough time has been spent on this, there are other problems to solve

#

there are multiple ways to do this, just use what you see fit

sinful shadow
#

what

#

no

#
// Outputs an integer value of zero.
System.out.println("\nA: " + (char)('A' + 26));
#

this @craggy bobcat

summer pebbleBOT
#

Detected code, here are some useful tools:

fluid quail
#

what do you mean

#

just pass the remainder to the function

sinful shadow
#

you just replace
map.get(String.valueOf(remainder))
by
String.valueOf((char)('A' - remainder)))

sinful shadow
#

wait

#

it's a +

#

not a minus

#

you just replace
map.get(String.valueOf(remainder))
by
String.valueOf((char)('A' + remainder)))

#

this @craggy bobcat

#

I have no idea why there was a -

fluid quail
#

isn't that what you want?

sinful shadow
#

๐Ÿค”

#

@craggy bobcat

sinful shadow
#

..yes

summer pebbleBOT
#

@craggy bobcat

Your question has been closed due to inactivity.

If it was not resolved yet, feel free to just post a message below
to reopen it, or create a new thread.

Note that usually the reason for nobody calling back is that your
question may have been not well asked and hence no one felt confident
enough answering.

When you reopen the thread, try to use your time to improve the quality
of the question by elaborating, providing details, context, all relevant code
snippets, any errors you are getting, concrete examples and perhaps also some
screenshots. Share your attempt, explain the expected results and compare
them to the current results.

Also try to make the information easily accessible by sharing code
or assignment descriptions directly on Discord, not behind a link or
PDF-file; provide some guidance for long code snippets and ensure
the code is well formatted and has syntax highlighting. Kindly read through
https://stackoverflow.com/help/how-to-ask for more.

With enough info, someone knows the answer for sure ๐Ÿ‘

summer pebbleBOT
#
OP left

Closing thread...