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

    Default People class and Printlns

    I created the class and set it up to recive the values from the first portion of code. to test to see if the values are recived correctly i set up a print line. while this is not part of my code its just a test to make sure the values are right. When i try to set up the Printlns its asks me to make the name and age variables static. this will not work because they have to change. so how can i set it up to where i can print out the values???
    First Part
    Java Code:
    import java.util.ArrayList;
        import java.util.Iterator;
        import java.util.Scanner;
        import java.util.Vector;
        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 String aName;
            public static int numberCount;
             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();
                   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("lifepath Number: "+ numberCount);
              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;
               }
                   }
                   System.out.println(Info.name+ Info.age);
          }
        }
    Person Class
    Java Code:
    public class Info{
       //Attributes
       String name;{
       name = People.aName;
       }
       int age;{
       age= People.numberCount;
       }
      //Constructor
      public void Person(String name, int age){
          this.name = name;
          this.age = age;
      }
    
     /**
    * Method that return the name of the person
    */
     public String getName(){
         return name;
     }
    
     public int getBDay(){
         
         return age;
     }
    
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    what information are you trying to have Info hold? You understand that the Info class will have no knowledge of any of the other data in your program, that it is completely independent and so as written will not help you.

    Also, the method that you mark as a "constructor" in the Info class is not a constructor but rather a method. This is because its name is different from the class name (constructors must share the same name as the class) and it has a return type of void (constructors have no return type).

    My recommendations are that you do one of two things:
    simplest and easiest is to just call println on whatever data you want to display when you want to display it. Don't bother with a separate class to do this.

    Better still is to change Person into a true OOP class. Currently it is just a "holder" class whose purpose is mainly to hold a static main method. Better would be to give it non-static variables such as a String to hold a name, perhaps an int to hold an age, a String to hold a birthday (or a Date object if you want to get a little fancier), and perhaps an int array to hold the Letter int values corresponding to the name (what do you use this for?).

    Then another class could be created to hold an ArrayList of Person objects and could have methods to get user input to allow you to create Person objects and fill your ArrayList with them.

    Best of luck!

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

    Default

    Well i am going to use this Class to put into a map to allow the user to pull up both pieces of information when they recall the name of the person. so i need this holder to act as the value and the name as the key.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    My recommendation is unchanged: make Person a true class with non-static fields and methods.

Similar Threads

  1. For the people who love Calculus
    By tim in forum Forum Lobby
    Replies: 9
    Last Post: 12-01-2009, 01:53 PM
  2. Replies: 8
    Last Post: 03-21-2009, 10:34 AM
  3. Wats up, Java people
    By littlebrat in forum Introductions
    Replies: 0
    Last Post: 02-12-2009, 06:48 PM
  4. Calling a method on original class from created class
    By kpedersen in forum Advanced Java
    Replies: 4
    Last Post: 08-20-2008, 01:25 AM
  5. Replies: 0
    Last Post: 05-13-2008, 07:16 AM

Posting Permissions

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