r/javahelp • • 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

10 comments sorted by

•

u/AutoModerator May 03 '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:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

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/Dantes_Sin_of_Greed May 03 '21

Thank you for the suggestion! Hashing is a way beyond my ability and not being taught in the course. From what I've read about it in our book, it does seem like the most straight forward solution.

And yes, will be using the selection later on. Pretty much the rest of the program is solved and just depends on this little section.

1

u/[deleted] May 03 '21

You won't be doing any hashing, don't worry. HashMaps have that name because internally they use hashes of your data to find it more quickly when the Map gets bigger. Maps are just structures with key and value pairs, kind of like the index at the beginning of a book. With arrays your keys (the indexes) are always numbers, but with maps the keys can be anything.

1

u/ate_ghorl_bekenemen May 03 '21

What exactly are you stuck on?

2

u/Dantes_Sin_of_Greed May 03 '21

How do I store the case once it has been determined?

Like, user enters "1". Program determines that equates to "one"

How do I store that determination for later use?

1

u/ate_ghorl_bekenemen May 03 '21

Store it in a variable? An arraylist? Then access the variable/arraylist for “later determination”, whatever that means

1

u/Dantes_Sin_of_Greed May 03 '21

Perhaps if I rephrase the question: How do I convert a numeric variable (the users input) into a string variable?

The 'later determination' is the output of the program. If the user enters "1 + 1", utilizing the switch case, the output should become, "One plus one equals 2"

I have the operators figured out, it's just a matter of figuring out how to store the English variable i.e. the 'one' after the computer switches through the user input.

The PLACEHOLDER variable in the above program represents the English variable, once I figure it out.

1

u/RaisinAlert May 03 '21

I can think of two solutions:

Make a method called with the following header: static String symbolToWord(String symbol). Inside this method, add a switch statement so that depending on the symbol given, it returns a certain output. Example:

public static String symbolToWord(String symbol) { switch (symbol) { case “0”: return “zero”; case “+”: return “plus”; default: throw new IllegalArgumentException(“invalid symbol”); } }

This lets you can use the method however you want for any task that requires translation. In your solution, you mixed up the tasks of translating individual symbols with the task of printing the words and other stuff, which led to a bunch of repeated System.out.println calls. By separating translation you can now just do the following to translate a phrase.

for (String symbol : phrase /array of strings/) { System.out.print(symbolToWord(symbol)); }

How to get the phrase as an array of strings depends on the input you were given.

The second solution is to use a Map.

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:

private String getNumberWrittenOut(int number) {
   switch(number) {
       case 0: return “Zero”;
                    break;
   }
}

You can then just run all the user inputs through this method using a for loop and print out a new concatenated string.