Results 1 to 3 of 3

Thread: Adding

  1. #1
    natep67 is offline Member
    Join Date
    Apr 2009
    Posts
    8
    Rep Power
    0

    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?
    Java 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;
    	       }
    	           }
    	  }
    	}

  2. #2
    hadesflames is offline Member
    Join Date
    Apr 2009
    Posts
    7
    Rep Power
    0

    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:
    Java 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 03:56 AM.

  3. #3
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    828
    Rep Power
    6

    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);

Similar Threads

  1. adding GUI to Inventory
    By voyager91 in forum New To Java
    Replies: 8
    Last Post: 02-01-2009, 05:20 AM
  2. Help in adding some validation..
    By tornado in forum New To Java
    Replies: 5
    Last Post: 11-30-2008, 01:36 AM
  3. adding thread
    By amith in forum Java 2D
    Replies: 0
    Last Post: 07-05-2008, 04:44 PM
  4. adding thread
    By amith in forum Java 2D
    Replies: 0
    Last Post: 07-05-2008, 04:31 PM
  5. Adding taglibs in JSP
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-13-2008, 11:43 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •