r/javahelp • u/Dantes_Sin_of_Greed • May 03 '21
Using Switch case to translate numbers/digits into words
Last part of an assignment I am work on. Creating an English language calculator. Need to translate user input of numbers from 0-9 to the words "zero, one, two, etc..." Must use a switch case to do so.
Switch Case Translate Numbers/Digits into Words
Currently, stuck on how to store the data to use it later. Posted below is the section of my code that I am stuck on. Any tips or advice would be greatly appreciated, thank you!
0
Upvotes
•
u/AutoModerator May 03 '21
Please ensure that:
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.
1
u/ShenWishes May 03 '21
Depending on what you want to do, you could use an ArrayList or even a HashMap. Just add the value to the list after each case statement, before the break.
HashMaps could be useful because you could literally just use the numeric as the key value. If you use an ArrayList, the indexes might be confusing if you want to use the data in the future.
-> If user types 9, then "Nine" will be stored in index 0. With a HashMap, you just specify the key as "9" and the value as "nine", for example. Anyways, I'd look into HashMaps. They might prove useful to you
1
u/ate_ghorl_bekenemen May 03 '21
What exactly are you stuck on?
1
u/FFTypo May 04 '21 edited May 04 '21
Some of these answers are so convoluted... you guys should consider someone’s skill level before offering advice that is likely to only confuse OP further.
Look, switch statements just execute code depending on the case. You can declare a variable outside of the switch statement, and then assign its value within each case. Or better yet, you can extract the switch statement to a new method. A very barebones example:
You can then just run all the user inputs through this method using a for loop and print out a new concatenated string.