# [HELP] Cardinal Number System...

Page 1 of 1

## 5 Replies - 555 Views - Last Post: 31 January 2013 - 08:47 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=310603&amp;s=195ba51ca4e7f5daaf065a7b93b16050&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 IcedWings

Reputation: 0
• Posts: 12
• Joined: 30-October 12

# [HELP] Cardinal Number System...

Posted 31 January 2013 - 05:14 PM

Here is what is SUPPOSED to happen:

Input Number >> 55
Output >> five-five
Input Number >> 790
Output >> seven-nine-zero

The red is just a system.err message
The green is user input into the Java console
The black is what the application is printing (system.out)

All I am able to produce is single digits and their cardinal outputs.
So, 5 --> five, 8 --> eight, and etc.

When I input numbers with multiple digits such as 425, I don't get any output at all.

My question is: How do I get my program to print multi-digit inputs?
Such as 415 --> four-one-five

Also, a dash is supposed to separate the strings.

Here is my code:
```import java.util.Scanner;

public class Lab3
{
// instance variable
private Scanner scan;
private String cardString;

public static void main (String [] args)
{
Lab3 G = new Lab3();

G.printCardinals();

}

// Initializes the Scanner object for this Lab3 object.
public Lab3()
{
// create Scanner object
scan = new Scanner(System.in);
}

// get user input
// switch statement
// output cardinal number representation
public void printCardinals()
{
int enter;
boolean quit = false;
while (! quit)
{
//while (enter)
// prompt user
System.err.print("Input a number to get a cardinal number\n" +
"(Type any negative number to quit program): ");

int num = scan.nextInt();

// if user input is negative, exit program
if (num < 0)
{
quit = true;
System.err.println("Exiting program, bye!");
} // end if statement

String cardString  = "";

// if num is not negative, run switch
switch (num)
{
case 0:		cardString = "zero";
break;
case 1: 	cardString = "one";
break;
case 2:		cardString = "two";
break;
case 3:		cardString = "three";
break;
case 4:		cardString = "four";
break;
case 5:		cardString = "five";
break;
case 6:		cardString = "six";
break;
case 7:		cardString = "seven";
break;
case 8:		cardString = "eight";
break;
case 9:		cardString = "nine";
break;
} // end switch

// output cardinal numbers
System.out.println(cardString);

} // end while
} // end printCardinals method
} // end class
```

One last thing:

I asked my teacher for a HINT and all he said was... "Loop". Derp.

Is This A Good Question/Topic? 0

## Replies To: [HELP] Cardinal Number System...

### #2 CasiOo

• D.I.C Lover

Reputation: 1490
• Posts: 3,316
• Joined: 05-April 11

## Re: [HELP] Cardinal Number System...

Posted 31 January 2013 - 05:58 PM

You can do it using the / and % operators
For example 12345 % 10 should be 5, and 12345 / 10 should be 1234
The above can be easily implemented using a while loop

As an alternative, you could be working with a String instead of an integer
Then you would simply have to check the char
```char[] digits = num.toString().toCharArray();

for (int i=0; i<digits.length; i++) {
switch (digits[i]) {
case: '0'
break;
}
}

```

### #3 CasiOo

• D.I.C Lover

Reputation: 1490
• Posts: 3,316
• Joined: 05-April 11

## Re: [HELP] Cardinal Number System...

Posted 31 January 2013 - 06:07 PM

Actually if you make a recursive method together with the / and % operators, you can get the number to print out in the correct sequence without having to fiddle around with a String

### #4 IcedWings

Reputation: 0
• Posts: 12
• Joined: 30-October 12

## Re: [HELP] Cardinal Number System...

Posted 31 January 2013 - 07:36 PM

CasiOo, on 31 January 2013 - 05:58 PM, said:

You can do it using the / and % operators
For example 12345 % 10 should be 5, and 12345 / 10 should be 1234
The above can be easily implemented using a while loop

As an alternative, you could be working with a String instead of an integer
Then you would simply have to check the char />
```char[] digits = num.toString().toCharArray();

for (int i=0; i<digits.length; i++) {
switch (digits[i]) {
case: '0'
break;
}
}

```

I haven't ever used or see the / and % operators in my lessons, so I don't think those will work for me.

For the second part you mentioned.... can you give more detail? So like, my input would be translated to a string and then I need to figure out how to call method numChar or whatever? Then I have a value of the number of chars. and then for each char, do the switch statement?

Also, what does it mean when you do a variable and then ++ next to it?

Quote

i++

### #5 CasiOo

• D.I.C Lover

Reputation: 1490
• Posts: 3,316
• Joined: 05-April 11

## Re: [HELP] Cardinal Number System...

Posted 31 January 2013 - 07:46 PM

/ is for division, don't tell me you haven't seen it before
% is modulus operator which gives you the remainder

### #6 IcedWings

Reputation: 0
• Posts: 12
• Joined: 30-October 12

## Re: [HELP] Cardinal Number System...

Posted 31 January 2013 - 08:47 PM

CasiOo, on 31 January 2013 - 07:46 PM, said:

/ is for division, don't tell me you haven't seen it before
% is modulus operator which gives you the remainder