2 Replies - 2919 Views - Last Post: 22 October 2009 - 06:38 PM Rate Topic: -----

#1 mandy123   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 18-September 09

break down the number and add each digit

Post icon  Posted 22 October 2009 - 01:17 PM

Hello, I need some help with my program There is a point in my program where I need to break down a number and add each digit, example in the code.
If you could help me that would be great!!

Thanks



import java.util.Scanner;
public class Numerology
{
	public static void main ( String args[] )
	{

		int date;
		int month;
		int day;
		int year;
		int future;
		char slash;
		char slashx;

		Scanner input = new Scanner ( System.in );

		System.out.print ("Enter the birth date (mm/dd/yyyy) : ");
		month = input.nextInt();
		slash = input.next().charAt(0);
		day = input.nextInt();
		slashx = input.next().charAt(0);
		year = input.nextInt();

		date = month + day + year;

		if (date >= 10)
		{

		// I need help here, lets say the date adds up to 23, I need to write a code to add 2 + 3
		// the result would be 5 and then set future equal to that to outprint the future for case 5

		}


		future = date;
		switch (future)
		{
			case 1:
				System.out.println (":1: The lesson is in the struggle, not in the victory. Life may be difficult now, but you will gain much within a short period of time!");
				break;

			case 2:
				System.out.println (":2: Your everlasting patience will be rewarded sooner or later");
				break;

			case 3:
				System.out.println (":3: Pray for what you want, but work for the things you need.");
				break;

			case 4:
				System.out.println (":4: You will inherit some money or a small piece of land");
				break;

			case 5:
				System.out.println (":5: You will spend old age in comfort and material wealth");
				break;

			case 6:
				System.out.println (":6: You can't always get what you want, but if you try sometimes you might find you get what you need.");
				break;

			case 7:
				System.out.println (":7: If you eat your vegetables you'll grow up big and strong like Popeye");
				break;

			case 8:
				System.out.println (":8: Every exit is an entrance to new experiences.");
				break;

			case 9:
				System.out.println (":9: Embrace joy and slap evil. Avoid violence.");
				break;
		}
	}
}




Is This A Good Question/Topic? 0
  • +

Replies To: break down the number and add each digit

#2 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12800
  • View blog
  • Posts: 45,992
  • Joined: 27-December 08

Re: break down the number and add each digit

Posted 22 October 2009 - 02:04 PM

To find the sum of the digits of a number, try converting the number to a String and parsing the characters back to numbers. Like so:
public int sumDigits(int x){
  String y = x + "";
  int sum = 0;
  for(int i = 0; i < y.length(); i++) 
	 sum += Integer.parseInt(y.charAt(i) + "");
  return sum;
}


Was This Post Helpful? 0
  • +
  • -

#3 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: break down the number and add each digit

Posted 22 October 2009 - 06:38 PM

// that might read mm
month = input.nextInt();
// but this input.next() will read "/dd/yy" as a String
slash = input.next().charAt(0);
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1