The Artima Developer Community
Sponsored Link

Java Answers Forum
Newbie code Please help if u can .

3 replies on 1 page. Most recent reply: May 14, 2004 4:41 PM by Tara

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
Tara

Posts: 3
Nickname: suzan
Registered: May, 2004

Newbie code Please help if u can . Posted: May 11, 2004 9:37 PM
Reply to this message Reply
Advertisement
HI guys , im stuck with this code trying to display an int in words for example 96 as ninety six
can u please help
thanks alot




pubilic class Number
{

public static void main (String []args)

{
System.out.println("Enter A Number");

int num = SavitchIn.readLineInt();
inWord(num);
System.out.println();
}
public static void inWord(int num)
{

if (num<10)
System.out.println(Word(num)+" ");
else
{
inWord(num/10);
System.out.print(Word(num%10)+ " ");}
}
private static String Word (int digit)
{
String result = null ;
switch(digit)
{
case 0:result="zero";break;
case 1:result="one";break;
case 2:result="two";break;
case 3:result="three";break;
case 4:result="four";break;
case 5:result="five";break;
case 6:result="six";break;
case 7:result="seven";break;
case 8:result="eight";break;
case 9:result="nine";break;
default:


break;
}
return result;
}
}


Daniel Ray

Posts: 53
Nickname: budoray
Registered: Oct, 2002

Re: Newbie code Please help if u can . Posted: May 12, 2004 3:09 AM
Reply to this message Reply
The code below is incomplete and untested. It may have syntax errors.

My approach would be to declare some static final strings for 1-20, 30, 40, 50 ... 90 like so

public static final String ONE = "one";
...
public static final String NINETEEN = "nineteen";
...
public static final String SIXTY = "sixty";
...

***
Do the same for "and", "hundreds", "thousands" etc (in case you want to use this for values greater than an int)

Will you parse the elements of the array as ints .. easier

int[] ints = {1, 2, 3} // 123

(You will need some exception handling to parse a String using the Integer method parseInt("1") to ensure you have a valid int.)

I would then start at the end of the array .. ints.length
and step backwards in groups of three because we write larger numbers like 32,500 and no matter where 500 ends up we still say it as five hundred .. five hundred thousand .. five hundred million.

If the length of the group of ints is less than 3 .. then I would do something similar to the above for teens and so on.

The only tricky thing that sticks out is how to deal with 0, 00, and 000. The only time you might print zero is if ints.length = 1. This is where the "and" comes in from where I marked ***.

I used to help out here quite a bit, but I got hammered for 'supplying' code. Give this an honest attempt and if you still need help .. I'll do what I can.

Regards,
Ray

SRV001

Posts: 13
Nickname: srv001
Registered: May, 2004

Re: Newbie code Please help if u can . Posted: May 13, 2004 9:07 AM
Reply to this message Reply
/*
 Good effort Tara...see how close you were!!!
*/
import java.io.*;
import java.util.*;
 
public class NumConverter {
 
    public int inputNumber() {
        InputStreamReader reader = new InputStreamReader(System.in);
        BufferedReader console = new BufferedReader(reader);
        String input = null;
 
        System.out.print("Please enter a number: ");
 
        try {
            input = console.readLine();
            System.out.print (input + " is ");
        } catch (Exception e) {
            System.out.println("Error entering number");
            System.exit(0);
        }
 
        return Integer.parseInt(input);
    }
 
    public void inWord(int number) {
        if (number < 10) {
            System.out.print( Word(number) + " " );
        } else {
            inWord(number / 10);
            System.out.print( Word(number % 10) + " " );
        }
    }
 
    private String Word (int digit) {
        String result = null ;
 
        switch(digit) {
            case 0:result="zero";break;
            case 1:result="one";break;
            case 2:result="two";break;
            case 3:result="three";break;
            case 4:result="four";break;
            case 5:result="five";break;
            case 6:result="six";break;
            case 7:result="seven";break;
            case 8:result="eight";break;
            case 9:result="nine";break;
            default: break;
        }
 
        return result;
    }
 
    public static void main (String[] args) {
        int num = 0;
        NumConverter numConvert = new NumConverter();
 
        num = numConvert.inputNumber();
        numConvert.inWord(num);
 
        System.out.println("\n");
    }
}

Tara

Posts: 3
Nickname: suzan
Registered: May, 2004

Re: Newbie code Please help if u can . Posted: May 14, 2004 4:41 PM
Reply to this message Reply
thanks alot Daniel and SRV001 very helpful tips and code ,
take care guys


tara

Flat View: This topic has 3 replies on 1 page
Topic: Charts in JAVA Previous Topic   Next Topic Topic: Type Casting

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use