HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Dividing a number half into words and half number

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
numberintowordsdividingandhalf

Problem

Example:

If I have a number 1000:

-
I first put commas (In English Standard) that makes it: 1,000

-
I split the above at the first comma: I get 1

-
I convert 1 into words: I get One, name it replaceNumber

-
I convert the original number into words:

==> One Thousand

-
I convert the first half of the number back to digits with a simple string replace:

originalNum.replace(replaceNumber, "");


The desired result is: For any number convert the first half of the number (on the left before the comma) into numbers and the rest into words.

So for the above example I get 1 thousand

I get 1 and Thousand. I want to know if I can improve it. Please ignore the static keyword; I will remove that in production code. Eclipse has enforced this onto me.

public class FunctionsFun {
    static NumberToWords num;
    public static void main(String[] args){

        String number= "600000";
        int number1 = 600000; 
        num = new NumberToWords(); 

        String numberwithcommas = formatNumbers(number); 

        System.out.println("Number with commas"+numberwithcommas); 

        String numberInWords = num.start(number1);
        System.out.println("Number in words "+numberInWords);

        String originalNum = numberwithcommas;
        String[] parts = originalNum.split(",");
        String part1 = parts[0];
        System.out.println("First Half is "+part1); //This is the top text
        String replace = num.start(Integer.valueOf(part1));
        System.out.println("Before replace "+numberInWords);
        String finnumberInWords = numberInWords.replace(replace, " "); 
        System.out.println(part1+"\n"+finnumberInWords);
    }

    public static String formatNumbers(String input) {

        String number = input;
        double dispString = Double.parseDouble(number);
        DecimalFormat formatter = new DecimalFormat("#,###");
        return (formatter.format(dispString));
    }
}


Here is my number to words class:

```
public cla

Solution

Your code is generally pretty good. I would change a few things...
First of all...

Your formatting makes the code hard to read. For example:

System.out.println("Number with commas"+numberwithcommas); 

   String numberInWords = num.start(number1);
   System.out.println("Number in words "+numberInWords);


Should be formatted like:

System.out.println("Number with commas " + numberwithcommas); 

    String numberInWords = num.start(number1);
    System.out.println("Number in words " + numberInWords);


Also,

String numberwithcommas = formatNumbers(number);


Should be like:

String numberWithCommas = formatNumbers(number);


The last one:

public static String formatNumbers(String input) {

    String number = input;
    double dispString = Double.parseDouble(number);
    DecimalFormat formatter = new DecimalFormat("#,###");
    return (formatter.format(dispString));
}


should be (notice the last line excluding the braces):

public static String formatNumbers(String input) {

    String number = input;
    double dispString = Double.parseDouble(number);
    DecimalFormat formatter = new DecimalFormat("#,###");
    return formatter.format(dispString);
}


In other words, the parentheses is not necessary.

Not a big problem, but it will be if you have other people trying to read your code.
FunctionsFun.java

Let's take a look at the first part...

public class FunctionsFun {  
   static NumberToWords num;  
   public static void main(String[] args){  

       String number= "600000";
       int number1 = 600000; 
       num = new NumberToWords();


NumberToWords num; can be put inside the main method without harming anything. This allows you to omit the static.

I don't see the use of theString and int representing the same number. Remove String number = "600000", change int number1 to int number, and replace:

String numberwithcommas = formatNumbers(number);


with

String numberWithCommas = formatNumbers(Integer.toString(number));


You also have the numbers hard-coded. You should ask for input using 'Scanner, like so:

Scanner sc = new Scanner(System.in);

int number = 0;

for (boolean validInput = false; validInput; ) {
    System.out.print("Number: ");

    String input = sc.next();

    try {
        number = Integer.parseInt(input);
        validInput = true;
    } catch {
        System.out.print("Oops. That's not a number. Try again: ");
    }
}
sc.close();


But of course, if you want the numbers to be hard-coded, then don't do this (but I don't see any reason why not to).
NumberToWords.java

First of all, the inner class:

static public class ScaleUnit {
    private int exponent;
    private String[] names;
    private ScaleUnit(int exponent, String...names) {
        this.exponent = exponent;
        this.names = names;
    }
    public int getExponent() {
        return exponent;
    }
    public String getName(int index) {
        return names[index];
    }
}


names does not have to be an array. There is only one name, so it should be:

private String name;


and the constructor gets to change to:

ScaleUnit(int exponent, String name) {
    this.exponent = exponent;
    this.name = name;
}


The
getName() method also changes:

public String getName() {
    return name;
}


which means you have to go through your code and remove all the arguments.

OR

Add a second name to match the long scale.

I recommend the second option and add a input option to ask the user for what scale to use, which changes the main method to:

Scanner sc = new Scanner(System.in);

int number = 0;

for (boolean validInput = false; validInput; ) {
    System.out.print("Number: ");

    String input = sc.next();

    try {
        number = Integer.parseInt(input);
        validInput = true;
    } catch {
        System.out.print("Oops. That's not a number. Try again: ");
    }
}

Scale scale = Scale.SHORT; //default

for (boolean validInput = false; validInput; ) {
    System.out.print("Scale: ");

    String input = sc.next();

    if (input.equalsIgnoreCase("short")) {
        // already short, so nothing to be done
        validInput = true;
    } else if (input.equalsIgnoreCase("long")) {
        scale = Scale.LONG;
        validInput = true;
    }
}
sc.close();


And of course you have to move the enum
Scale` to a new file, then get the result of the input to connect to the other class (I won't go into that).

I think that's it! Great Job!

Code Snippets

System.out.println("Number with commas"+numberwithcommas); 

   String numberInWords = num.start(number1);
   System.out.println("Number in words "+numberInWords);
System.out.println("Number with commas " + numberwithcommas); 

    String numberInWords = num.start(number1);
    System.out.println("Number in words " + numberInWords);
String numberwithcommas = formatNumbers(number);
String numberWithCommas = formatNumbers(number);
public static String formatNumbers(String input) {

    String number = input;
    double dispString = Double.parseDouble(number);
    DecimalFormat formatter = new DecimalFormat("#,###");
    return (formatter.format(dispString));
}

Context

StackExchange Code Review Q#68151, answer score: 6

Revisions (0)

No revisions yet.