Hi all, this is a homework assignment that I'm not sure how to begin with. We are given all the code and I just need to finish a function to convert digits to words. For example, the string "1, 2, and 3." becomes "One, Two, and Three." The String "10" becomes "OneZero." We aren't allowed to use any loop constructs either ("for" or "while"). We are supposed to use the switch-case statements somehow. The missing function is the "public void append(char c)" This is what I have but I'm getting no results.

public abstract class Builder {
    // Appends just one character
    public abstract void append(char c);
 
    // Force implementer to create a toString method.
    public abstract String toString();
 
    // Makes use of the append(char) method
    // to append a whole string.
    public void append(String s) {
        for(int i=0;i<s.length();i++) {
            append(s.charAt(i));
        }
    }
 
    // Makes use of the fact that all Objects
    // have a toString() method to format all
    // other objects.
    public void append(Object o) {
        if(o == null)
            append("null");
        else
            append(o.toString());
    }
}
public class NumBuilder extends Builder {
    StringBuffer sb = new StringBuffer();
    public void append(char c) {
       switch(c)
       {
           case 1:  sb.append("One");
                    break;
           case 2:  sb.append("Two");
                    break;
           case 3:  sb.append("Three");
                    break;
           case 4:  sb.append("Four");
                    break;
           case 5:  sb.append("Five");
                    break;                     
       }
    }
    public String toString() {
        return sb.toString();
    }
    public static void main(String[] args) {
        NumBuilder nb = new NumBuilder();
        nb.append("1, 2, and 3.");
        System.out.println(nb);
        nb.append("4 and 5");
    }
}

I'm not sure what techniques you can use to parse the String: "1, 2, and 3." and find the numeric digits that are to be replaced with text without looping.
switch statements are a way to "jump" to a block of code based on a value. Some think it a cleaner way to code than to use if/else if statements.

Can you use recursive methods? They can do things similar to looping.

I'm not sure what techniques you can use to parse the String: "1, 2, and 3." and find the numeric digits that are to be replaced with text without looping.
switch statements are a way to "jump" to a block of code based on a value. Some think it a cleaner way to code than to use if/else if statements.

Can you use recursive methods? They can do things similar to looping.

I did receive a hint from my TA, this is what he told me: If you are unfamiliar with switch, you may recall from the last class that it is used to set off case statements for handling certain input cases. If you want your append method to be flexible and handle arguments of different return types, use overloading. Remember what happens if you try to compare strings using the == comparative operator.

I'm not sure if we can use recursion. We haven't gone over that yet in class.

I'm not sure what techniques you can use to parse the String: "1, 2, and 3." and find the numeric digits that are to be replaced with text without looping.
switch statements are a way to "jump" to a block of code based on a value. Some think it a cleaner way to code than to use if/else if statements.

Can you use recursive methods? They can do things similar to looping.

Hmm yes i do agree with Norm... maybe you could take the string entered and convert to a char array via toCharArray(), then make an integer counter equal to 0, and the use an if statement to see when the value reaches the arrays size by charArrayName.length, call the method to check each char for a digit by isDigit() (the method should accept one char and return a string of the char using switch)and repeat this for the entire length of the array and dont forget to increment your counter used to call the method

I did receive a hint from my TA, this is what he told me: If you are unfamiliar with switch, you may recall from the last class that it is used to set off case statements for handling certain input cases. If you want your append method to be flexible and handle arguments of different return types, use overloading. Remember what happens if you try to compare strings using the == comparative operator.

I'm not sure if we can use recursion. We haven't gone over that yet in class.

well yes you cant compare a string using '==' only primitive values... so checking StringName==null would still hold true, but not StringName=="test". to test strings for comparison use equals() method

The switch statement can be replaced with if/else if statements that will do almost exactly the same thing.
To scan the contents of a String, you need a loop.
It is conceivable to write a program as if and else statements to cover all the possible combinations, but it would be huge, like a tree with many many many branches.

The switch statement can be replaced with if/else if statements that will do almost exactly the same thing.
To scan the contents of a String, you need a loop.
It is conceivable to write a program as if and else statements to cover all the possible combinations, but it would be huge, like a tree with many many many branches.

Sweeeet, I ended up doing this and it works perfectly now!

public void append(char c)
    {
       switch(c)
       {
           case '1':    sb.append("One");
               break;
           case '2':    sb.append("Two");
               break;
           case '3':    sb.append("Three");
               break;
           case '4':    sb.append("Four");
               break;
           case '5':    sb.append("Five");
               break;
            default:    sb.append(c);
                break;
       }
    }

How do you get the char values from the String, skipping over the "," the "and" and the "."
and how do you call the method with each value that is found in the String
without a loop?

How do you get the char values from the String, skipping over the "," the "and" and the "."
and how do you call the method with each value that is found in the String
without a loop?

The Builder class has functions that scans in each character of the String and passes it back over to my NumBuilder class. If the char matches anything in my switch statement then it appends what's in the switch statement. The default is to append what the char was scanned in originally so it all works!! Woot

So this was all a big waste of time. The parsing of the String is done by code that is not shown.

So this was all a big waste of time. The parsing of the String is done by code that is not shown.

The code is posted in my first original post...? I'm sorry if I worded my question badly. That seems to be the case and I apologize.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.