Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

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

  1. #1
    Junior Member
    Join Date
    Dec 2013
    Posts
    22
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default 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...


    // 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


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default 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.

  3. #3
    Junior Member
    Join Date
    Dec 2013
    Location
    London
    Posts
    11
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default 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

Similar Threads

  1. Replies: 6
    Last Post: October 26th, 2013, 01:59 PM
  2. need help converting numbers to word
    By mia_tech in forum What's Wrong With My Code?
    Replies: 5
    Last Post: May 15th, 2012, 09:02 AM
  3. Replies: 4
    Last Post: November 14th, 2010, 05:02 PM
  4. Question: Converting number to words.
    By shamed in forum Java Theory & Questions
    Replies: 6
    Last Post: January 1st, 2010, 04:28 AM
  5. Java coding for writing telephone bill calculator program
    By eyeore in forum What's Wrong With My Code?
    Replies: 2
    Last Post: September 8th, 2009, 10:06 AM