5 Replies - 317 Views - Last Post: 31 January 2013 - 08:47 PM Rate Topic: -----

#1 IcedWings  Icon User is offline

  • New D.I.C Head

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

[HELP] Cardinal Number System...

Posted 31 January 2013 - 05:14 PM

Hi guys, please help me T_T


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" +
					"representation of your 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  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1373
  • View blog
  • Posts: 3,027
  • 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;
	}
}


Was This Post Helpful? 0
  • +
  • -

#3 CasiOo  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1373
  • View blog
  • Posts: 3,027
  • 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 ^^
Was This Post Helpful? 0
  • +
  • -

#4 IcedWings  Icon User is offline

  • New D.I.C Head

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

Re: [HELP] Cardinal Number System...

Posted 31 January 2013 - 07:36 PM

View PostCasiOo, 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++

Was This Post Helpful? 0
  • +
  • -

#5 CasiOo  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1373
  • View blog
  • Posts: 3,027
  • 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

I advice you read every article you can find about operators
Was This Post Helpful? 0
  • +
  • -

#6 IcedWings  Icon User is offline

  • New D.I.C Head

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

Re: [HELP] Cardinal Number System...

Posted 31 January 2013 - 08:47 PM

View PostCasiOo, 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

I advice you read every article you can find about operators


Oh, excuse me. I read that wrong or misinterpreted your original post. Yeah, I know what division is... lol.

Thanks though, for your help.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1