Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-21-2009, 11:11 PM
Member
 
Join Date: Apr 2009
Posts: 8
Rep Power: 0
natep67 is on a distinguished road
Post Adding
is there any way to add up the numbers? when i run the program, someone types a name then presses done . After this the name is split up by letter and each character is assigned a value. It prints out the values but i want to print out a number that is all the numbers added together. is there any way to do this?
Code:
	import java.util.ArrayList;
	import java.util.Iterator;
	import java.util.Scanner;
	import java.util.Vector;
	import java.io.*;
	public class People
	
	{
	     enum Letter
	     {
	         A(1),J(1),S(1),
	         B(2),K(2),T(2),
	         C(3),L(3),U(3),
	         D(4),M(4),V(4),
	         E(5),N(5),W(5),
	         F(6),O(6),X(6),
	         G(7),P(7),Y(7),
	         H(8),Q(8),Z(8),
	         I(9),R(9);
	         int val;
	         Letter(int n)
	         {
	             this.val = n;
	         }
	         public int getValue(){return this.val;}
	     }
	     public static void main(String[] args)
	     {
	    	 
	          ArrayList<int[]> list = new ArrayList<int[]>();
	          Vector names = new Vector();
	          System.out.print("Enter names: ");
	          System.out.print("When finished type done ");
	          
	          Scanner input = new Scanner(System.in);
	     
	           while (input.hasNext())
	           {
	               String aName = input.next();
	               if (aName.equalsIgnoreCase("done"))
	                   break;
	               names.add(aName);
	           }
	           
	           Iterator itr = names.iterator();
	           while(itr.hasNext())
	           {
	               String s = (String)itr.next();
	               char[] ca = s.toCharArray();
	               int[] temp = new int[ca.length];
	               int count = 0;
	               for(char c: ca)
	               {
	                   temp[count++] = Letter.valueOf(Character.toUpperCase(c)+"").getValue();
	               }
	               list.add(temp);
	           }
	           int count = 0;
	           itr = names.iterator();
	           while(itr.hasNext())
	           {
	               String s = (String)itr.next();
	               System.out.println("Name = "+s+"\n");
	               int[] temp = list.get(count++);
	               int letterCount = 0;
	               for(int i: temp)
	               {
	                   System.out.print(s.charAt(letterCount++)+" = "+i+" , ");
	               }
	               System.out.println("\n");
	                Vector bday = new Vector();
	      System.out.print("Enter birthday(mmddyyyy): ");
	      System.out.print("When finished type done ");


	       while (input.hasNext()) {
	     String birthDay = input.next();
	         if (birthDay.equalsIgnoreCase("done"))
	           break;
	         bday.add(birthDay);
	       }

	       System.out.println("how many birthdays?");
	       System.out.println(bday.size());
	       System.out.println("Birthdays");
	       System.out.println(bday);
	       Iterator<String> iter = bday.iterator();
	       int sum = 0;
	       while(iter.hasNext())
	       {
	           String element = iter.next();
	           sum = Integer.parseInt(element.substring(0,2)) + Integer.parseInt(element.substring(2,4)) + Integer.parseInt(element.substring(4,8));
	           System.out.println("lifepath Number"+sum);
	           sum = 0;
	       }
	           }
	  }
	}
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 04-22-2009, 04:53 AM
Member
 
Join Date: Apr 2009
Posts: 7
Rep Power: 0
hadesflames is on a distinguished road
Default
just get the values of the array list and += them to a regular int var.

if the values are ints here would be an example:
Code:
int y = 0;
for(int i = 0; i < list.size(); i++)
  y+=list.get(i);
System.out.println(y);
and that should give you the needed value.

Last edited by hadesflames; 04-22-2009 at 04:56 AM.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 04-22-2009, 08:51 AM
RamyaSivakanth's Avatar
Senior Member
 
Join Date: Apr 2009
Location: Chennai
Posts: 589
Rep Power: 1
RamyaSivakanth is on a distinguished road
Default
Hi,
output for the below code.

Enter names: When finished type done ramya
done
Name = ramya

r = 9 , a = 1 , m = 4 , y = 7 , a = 1 ,
22
-------------------------------------
//Added for suming up the number
int numberCount=0;
while(itr.hasNext())
{
String s = (String)itr.next();
System.out.println("Name = "+s+"\n");
int[] temp = list.get(count++);
int letterCount = 0;
for(int i: temp)
{
System.out.print(s.charAt(letterCount++)+" = "+i+" , ");
numberCount =numberCount +i;
}
System.out.println("\n");
Vector bday = new Vector();
System.out.println(numberCount);
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
adding GUI to Inventory voyager91 New To Java 8 02-01-2009 06:20 AM
Help in adding some validation.. tornado New To Java 5 11-30-2008 02:36 AM
adding thread amith Java 2D 0 07-05-2008 05:44 PM
adding thread amith Java 2D 0 07-05-2008 05:31 PM
Adding taglibs in JSP Java Tip Java Tips 0 01-14-2008 12:43 AM


All times are GMT +2. The time now is 02:55 PM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org