Results 1 to 4 of 4
Thread: People class and Printlns
- 04-28-2009, 09:32 PM #1
Member
- Join Date
- Apr 2009
- Posts
- 8
- Rep Power
- 0
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
Person ClassJava 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); } }
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; } }
-
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!
- 05-02-2009, 01:13 AM #3
Member
- Join Date
- Apr 2009
- Posts
- 8
- Rep Power
- 0
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.
-
Similar Threads
-
For the people who love Calculus
By tim in forum Forum LobbyReplies: 9Last Post: 12-01-2009, 12:53 PM -
problem in accessing array values of one class in to jframe class
By cenafu in forum AWT / SwingReplies: 8Last Post: 03-21-2009, 09:34 AM -
Wats up, Java people
By littlebrat in forum IntroductionsReplies: 0Last Post: 02-12-2009, 05:48 PM -
Calling a method on original class from created class
By kpedersen in forum Advanced JavaReplies: 4Last Post: 08-20-2008, 12:25 AM -
Able to find class file in WEB-INF/classes but not after add sub folders in class dir
By vitalstrike82 in forum Web FrameworksReplies: 0Last Post: 05-13-2008, 06:16 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks