Welcome to Dream.In.Code
Become a Java Expert!

Join 150,383 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,179 people online right now. Registration is fast and FREE... Join Now!




Fun with number conversions

 
Reply to this topicStart new topic

Fun with number conversions

zzthejimzz
8 Feb, 2008 - 05:28 AM
Post #1

New D.I.C Head
*

Joined: 8 Feb, 2008
Posts: 2

Hello

I'm fairly new to java so bare with me! I am writing a program to do a number conversion. User input is a float up to 999999.99 and i need a sentinel loop to exit. So if i enter 456, the output will be in english, four hundred fifty six. Here is what i have so far, i'm getting an error with my switch statements (unreachable statements). I have tried and tried and i'm stuck and don't know what to do.

CODE

import java.util.Scanner;


public class NumberTranslator
{
    

    public static void main (String[]args)
    {
        
    
        float input;
        
        Scanner scan = new Scanner (System.in);
      
        System.out.println ("Welcome to my number translator!");
        System.out.println ("Enter an amount [0-999999.99; 0 to exit]: ");
        input = scan.nextFloat();
        
        String value = Float.toString(input);
        char ones = '0', tens = '0', teens = '0', hundreds ='0', thousand='0';
              
      
        
            
         if (value.length() == 2)
         {
             tens = value.charAt(0);
             System.out.print(convertDigitToTens(tens));
             ones = value.charAt(1);
             System.out.print(convertDigitToOnes(ones));
         }
         if (value.length() == 2 && value.charAt(0)== 1)
         {
             teens = value.charAt(1);
             System.out.print(convertDigitToTeens(teens));
            
         }    
         if (value.length() == 3)
         {
             hundreds = value.charAt(0);
             System.out.print(convertDigitToOnes(ones)+"hundred");
             tens = value.charAt(1);
             System.out.print(convertDigitToTens(tens));
             ones = value.charAt(2);
             if(value.charAt(2) != 0)
             {    
             System.out.print(convertDigitToOnes(ones));
             }
         }
         if (value.length() == 4)
         {
             thousand = value.charAt(0);
             System.out.print(convertDigitToOnes(ones) + "thousand");
             hundreds = value.charAt(1);
             tens = value.charAt(2);
             ones = value.charAt(3);
            
         }    
         else
             if (value.length() == 1)
                 ones = value.charAt(0);
              
        if (input < 10)
            System.out.print(convertDigitToOnes(ones));
            
        else
            if (input > 19 && input <= 99)
            {
                System.out.print(convertDigitToTens(tens));
                if (input % 10 != 0)
                {
                    System.out.print("-");
                    System.out.println((convertDigitToOnes(ones)));
                }
            }
            else
                if (input > 9 && input < 20)
                    System.out.print((convertDigitToTeens(teens)));
                    System.out.println("\n");
                    
                  
                
              
          
                      
          
        
        
    }
        
    static String convertDigitToOnes (char digit)
    {
        switch (digit)
        {
            case 1 : return "one"; break;
            case 2 : return "two"; break;
            case 3 : return "three"; break;
            case 4 : return "four"; break;
            case 5 : return "five"; break;
            case 6 : return "six"; break;
            case 7 : return "seven"; break;
            case 8 : return "eight"; break;
            case 9 : return "nine"; break;
            default : return "\nFatal Error!\n";        
                    
         }
    }
    
    static String convertDigitToTens (char digit)
    {
        switch (digit)
        {
            case 2 : return "twenty"; break;
            case 3 : return "thrity"; break;
            case 4 : return "fourty"; break;
            case 5 : return "fifty"; break;
            case 6 : return "sixty"; break;
            case 7 : return "seventy"; break;
            case 8 : return "eighty"; break;
            case 9 : return "ninety"; break;
            default : return "\nFatal Error!\n";
        }
    }
    
    static String convertDigitToTeens (int digit)
    {
        switch (digit)
        {
            case 0 : return "ten"; break;
            case 1 : return "eleven"; break;
            case 2 : return "twelve"; break;
            case 3 : return "thirteen"; break;
            case 4 : return "fourteen"; break;
            case 5 : return "fifteen"; break;
            case 6 : return "sixteen"; break;
            case 7 : return "seventeen"; break;
            case 8 : return "eighteen"; break;
            case 9 : return "nineteen"; break;
            default : return "\nFatal Error!\n";
        }        
    }
    
    
}  


User is offlineProfile CardPM
+Quote Post

KYA
RE: Fun With Number Conversions
8 Feb, 2008 - 05:58 AM
Post #2

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 5,951



Thanked: 159 times
Dream Kudos: 1375
My Contributions
I'm sure this isn't the problem, but I don't like how the case structure is set up. I like it more like this:

CODE

case 1:
     return "one";
     break;
case 2:
    //etc...


I would also change your if statements to a cascade of else if's

You could also define the length of the input and use a swiutch statement from there, does this program have to be robust?

This post has been edited by KYA: 8 Feb, 2008 - 06:00 AM
User is online!Profile CardPM
+Quote Post

zzthejimzz
RE: Fun With Number Conversions
8 Feb, 2008 - 06:06 AM
Post #3

New D.I.C Head
*

Joined: 8 Feb, 2008
Posts: 2

QUOTE(KYA @ 8 Feb, 2008 - 06:58 AM) *

I'm sure this isn't the problem, but I don't like how the case structure is set up. I like it more like this:

CODE

case 1:
     return "one";
     break;
case 2:
    //etc...


I would also change your if statements to a cascade of else if's

You could also define the length of the input and use a swiutch statement from there, does this program have to be robust?


It's funny you pointed that out, because i noticed i needed to get rid of the break statements! Now the code will compile, however it id not doing what i want it to do. Are my "println" methods in the right spot?
User is offlineProfile CardPM
+Quote Post

baavgai
RE: Fun With Number Conversions
8 Feb, 2008 - 09:05 AM
Post #4

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,290



Thanked: 136 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua, Cheese

My Contributions
I rather like the layout of the switches. tongue.gif Of course, I do hate switches.

You problem is that yous "digit" of type char. Try comparing it to values in single quotes.

I like if..else for this kind of thing:
CODE

if (digit=='1') {
    return "one";
} else if (digit=='2') {
    return "two";
} else if (digit=='3') {
...
} else {
    return "you lose!";
}



Or, even better, an array:
CODE

String [] toOnesLookup = new String [] {"One", "Two", "Three"};
int n = 2;
System.out.println(n + " = " + toOnesLookup[n-1]);


Of course, the array example depends on an int value. Which brings up a point. You might have an easier time if you delt with the input as a number.

Hope this helps.

This post has been edited by baavgai: 8 Feb, 2008 - 09:06 AM
User is online!Profile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 04:32PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month