Problem with converting Numbers to Words! Kindly ask for help:(

• December 21st, 2013, 11:06 PM
ProgrammablePeter
Problem with converting Numbers to Words! Kindly ask for help:(
So the theory is, convert a number to the worded value. 1 = ONE 2 = TWO 3 = THREE etc.. If user enters anything over 5 then display the numerical value of its type. The problem is this:
The output seems to differentiate from what I would hope to be the correct one...

Code :

```// Class NumberProgram public int getE() {   return e;     }   public void setE(int e) {   l = Integer.toString(e); switch(e) { case 1: l = "ONE"; System.out.println(l); break; case 2: l = "TWO"; System.out.println(l); break;   case 3: l = "THREE"; System.out.println(l); break; case 4: l = "FOUR"; System.out.println(l); break;   }     this.e = e; } // Class - NumberTest     import java.util.Scanner;   public class NumberTest {   public static void main(String[] args) { Scanner kbd = new Scanner(System.in);   NumberProgram P = newNumberProgram(); System.out.println("Enter your number: "); int number = kbd.nextInt();   P.setE(number);   System.out.println(P.getE());   } }   OUTPUT :   3 THREE 3```

SO... from what I can see, it seems to be also printing out the value that I enter as well as the worded value... Mhmm.. anyone got any advice?

--- Update ---

Ignore the Integer.toString... I've been working on it for a while and I've been trying alsorts of methods to convert these numbers into worded values :(
• December 22nd, 2013, 01:55 AM
GregBrannon
Re: Problem with converting Numbers to Words! Kindly ask for help:(
But you're so close. I'm not sure what you're asking. If the only problem is undesired output, look at the print statements in the program and figure out which one(s) is/are not needed. Comment them out one at a time if you have to until you figure it out, but I don't think it should be that hard for you to see which print statement is not needed.

Another suggestion: Single letter variable names are only appropriate for loop control variables. Give your variables descriptive names that describe what they are.
• December 22nd, 2013, 09:26 AM
skuch89
Re: Problem with converting Numbers to Words! Kindly ask for help:(
I would say you returning your attribute of the class which is not changed, whenever trying to do : System.out.println(P.getE());. The first output is from method setE(int e), is System.out.println(l) which clearly outputs 'THREE' and as you didn't assign 'i' variable to 'l' attribute variable still hold value of 3. I think in switch statement change l="THREE" to e="THREE".
Hopefully it will help

p.s. and change int variable to String to output String