New To Java - Newbie - Can't get method to trigger...

Hi there, I cannot figure out how to get the method Person in the Class Person here to run..... someone has told me that its because the array is dynamic and needs to be static but im not so sure.. Could someone please explain where I've gone wrong... It would be really appreciated...

public class HandlePersonalData {

/*

hello and welcome back to your assignment.

finish generating random name by joining strings at the end of the forloop then check its all working

by running a printstatement of datavalues in array from main method

After that proceed to write this **** to a file. bling bling ***** u just passed

NEED TO USE A VECTOR SO THAT THE USER CAN DECIDE THE LENGTH OF THE ARRAY OTHERWISE IT WONT WORK

Vector <Person> newPersonArray = new Vector <Person> ();

*/

public static int choiceArrayLengthInt;

public static int menuChoiceInt;

public static int userWantsNewArray;

public static int bugCheckerPerson1;

public static int randomIncomeRoundedOne;

public static int randomIncomeRoundedTwo;

public static int randomIncomeRoundedThree;

public static String theNextLetter = new String("");

public static String[] storeEachLetter = new String[0];

public static int lengthOfNameCalculator;

public static int checkDataValuesOne;

public static void main(String args[]) {

System.out.println("Welcome to the Person-Maker 5000");// Welcomes user and create menu

System.out.println("--");

System.out.println("");

System.out.println("");

System.out.println("Please specify what you would like to do");

System.out.println("");

System.out.println("Type: "+"\"1\"" +" to create an array of people");

System.out.println("Type: "+"\"2\"" +" to touch youself");

System.out.println("Type: "+"\"3\"" +" to be sexy with someone");

String menuChoiceString = Keyboard.readInput();

menuChoiceInt = Integer.parseInt(menuChoiceString);

System.out.println("");

System.out.println("");

// Set of ifs which controls what functions to do

if(menuChoiceInt == 1) { // User wants to create array

System.out.println("Please specify how many people you would like to create");

System.out.println("> E.G type: " +"\"4\"" +" to make an array of length four");

String choiceArrayLengthString = Keyboard.readInput();

choiceArrayLengthInt = Integer.parseInt(choiceArrayLengthString);

Person[] newPersonArray = new Person[choiceArrayLengthInt];

userWantsNewArray = 1;

//Person.Person(String name, int age, int income);

System.out.println("");

System.out.println("");

/**if(bugCheckerPerson1 == 1) {

for(int i=0;i<newPersonArray.length -1; i++) {

System.out.println(Person.newPersonArray.name);

System.out.print(Person.newPersonArray.age);

System.out.print(Person.newPersonArray.income);

}

}

**/

}

}

public class Person {

private String name;

private int age;

private int income;

public Person(String name, int age, int income) { // personarray.add(new Person(a,b,c,whatever))

this.name = name;

this.age = age;

this.income = income;

if(userWantsNewArray == 1) { // Then create the array

//Vector <Person> newPersonArray = new Vector <Person> ();

Person[] newPersonArray = new Person[50];

for(int i=0;i<newPersonArray.length -1; i++) {

int randomAge = (int)(Math.random()*61) + 18; //check these make the correct random number

//newPersonArray.age = randomAge;

//newPersonArray = new Person(name, age, income);

int randomIncome = (int)(Math.random()*990001) + 10000; //Has to round to thousands convert to dble bk agen

if(randomIncome < 100000) {

int randomIncomeDecrease = randomIncome / 1000;

double randomIncomeConvert = randomIncomeDecrease;

int randomIncomeBackToInt = (int)randomIncomeConvert;

randomIncomeRoundedOne = randomIncomeBackToInt * 1000; // Rounds values under 100000

newPersonArray.income = randomIncomeRoundedOne;

}

if(randomIncome > 99999 && randomIncome <1000000) {

int randomIncomeDecreaseTwo = randomIncome / 10000;

double randomIncomeConvertTwo = randomIncomeDecreaseTwo;

int randomIncomeBackToIntTwo = (int)randomIncomeConvertTwo;

randomIncomeRoundedTwo = randomIncomeBackToIntTwo * 10000; // Rounds values over 99999 and under 1 million

newPersonArray.income = randomIncomeRoundedOne;

}

if(randomIncome == 1000000) {

randomIncomeRoundedThree = 1000000;

newPersonArray.income = randomIncomeRoundedThree;

}

} // end of name forloop

String formNewName = new String("");

for(int l=0; l<lengthOfNameCalculator-1; l++) {

formNewName = formNewName.concat(storeEachLetter[l]);

}

newPersonArray.name = formNewName;

} // end of array length forloop

bugCheckerPerson1 = 1;

userWantsNewArray = 0;

System.out.println("lol");

}

System.out.println("lola");

} // end of public Person

//public Vector(String name, int age, int income) {

//}

} // end of public class Person

} // end of HandlePersonalData

[5683 byte] By [NocturnalManNza] at [2007-11-26 23:18:04]
# 1

Looks like you need to go back and read the basics about objects. Firstly the "method" Person in Person class is called the constructor not a method. You create objects by invoking the new operator which will call the constructor of the class.

Person p = new Person(......);

Where ...... is a list of parameters.

floundera at 2007-7-10 14:19:50 > top of Java-index,Java Essentials,New To Java...
# 2

Hello,

Here in the code mistake you made is you were trying to access array of object with out using index variable for that Array. I have modified bit so compare the code with yours one and let me know if it is now ok for you....

public class HandlePersonalData {

/*

hello and welcome back to your assignment.

finish generating random name by joining strings at the end of the forloop then check its all working

by running a printstatement of datavalues in array from main method

After that proceed to write this **** to a file. bling bling ***** u just passed

NEED TO USE A VECTOR SO THAT THE USER CAN DECIDE THE LENGTH OF THE ARRAY OTHERWISE IT WONT WORK

Vector <Person> newPersonArray = new Vector <Person> ();

*/

public static int choiceArrayLengthInt;

public static int menuChoiceInt;

public static int userWantsNewArray;

public static int bugCheckerPerson1;

public static int randomIncomeRoundedOne;

public static int randomIncomeRoundedTwo;

public static int randomIncomeRoundedThree;

public static String theNextLetter = new String("");

public static String[] storeEachLetter = new String[0];

public static int lengthOfNameCalculator;

public static int checkDataValuesOne;

public static void main(String args[]) {

System.out.println("Welcome to the Person-Maker 5000"); // Welcomes user and create menu

System.out.println("--");

System.out.println("");

System.out.println("");

System.out.println("Please specify what you would like to do");

System.out.println("");

System.out.println("Type: "+"\"1\"" +" to create an array of people");

System.out.println("Type: "+"\"2\"" +" to touch youself");

System.out.println("Type: "+"\"3\"" +" to be sexy with someone");

String menuChoiceString = "1";//Keyboard.readInput();

menuChoiceInt = Integer.parseInt(menuChoiceString);

System.out.println("");

System.out.println("");

// Set of ifs which controls what functions to do

if(menuChoiceInt == 1) { // User wants to create array

System.out.println("Please specify how many people you would like to create");

System.out.println("> E.G type: " +"\"4\"" +" to make an array of length four");

String choiceArrayLengthString = "2";//Keyboard.readInput();

choiceArrayLengthInt = Integer.parseInt(choiceArrayLengthString);

Person[] newPersonArray = new Person[choiceArrayLengthInt];

userWantsNewArray = 1;

//Person.Person(String name, int age, int income);

System.out.println("");

System.out.println("");

/**if(bugCheckerPerson1 == 1) {

for(int i=0;i<newPersonArray.length -1; i++) {

System.out.println(Person.newPersonArray.name);

System.out.print(Person.newPersonArray.age);

System.out.print(Person.newPersonArray.income);

}

}

**/

}

}

public class Person {

private String name;

private int age;

private int income;

public Person(String name, int age, int income) { // personarray.add(new Person(a,b,c,whatever))

this.name = name;

this.age = age;

this.income = income;

if(userWantsNewArray == 1) { // Then create the array

//Vector ><Person> newPersonArray = new Vector <Person> ();

Person[] newPersonArray = new Person[50];

for(int i=0;i<newPersonArray.length -1; i++) {

int randomAge = (int)(Math.random()*61) + 18; //check these make the correct random number

//newPersonArray.age = randomAge;

//newPersonArray = new Person(name, age, income);

int randomIncome = (int)(Math.random()*990001) + 10000; //Has to round to thousands convert to dble bk agen

if(randomIncome > 100000) {

int randomIncomeDecrease = randomIncome / 1000;

double randomIncomeConvert = randomIncomeDecrease;

int randomIncomeBackToInt = (int)randomIncomeConvert;

randomIncomeRoundedOne = randomIncomeBackToInt * 1000; // Rounds values under 100000

newPersonArray<i>.income = randomIncomeRoundedOne;

}

if(randomIncome > 99999 && randomIncome <1000000) {

int randomIncomeDecreaseTwo = randomIncome / 10000;

double randomIncomeConvertTwo = randomIncomeDecreaseTwo;

int randomIncomeBackToIntTwo = (int)randomIncomeConvertTwo;

randomIncomeRoundedTwo = randomIncomeBackToIntTwo * 10000; // Rounds values over 99999 and under 1 million

newPersonArray<i>.income = randomIncomeRoundedOne;

}

if(randomIncome == 1000000) {

randomIncomeRoundedThree = 1000000;

newPersonArray<i>.income = randomIncomeRoundedThree;

}

} // end of name forloop

String formNewName = new String("");

for(int l=0; l<lengthOfNameCalculator-1; l++) {

formNewName = formNewName.concat(storeEachLetter[l]);

}

} // end of array length forloop

bugCheckerPerson1 = 1;

userWantsNewArray = 0;

System.out.println("lol");

}

//System.out.println("lola");

} // end of public Person

//public Vector(String name, int age, int income) {

//}

} // end of public class Person

//} // end of HandlePersonalData>


Vimal

V-I-K-E-Na at 2007-7-10 14:19:50 > top of Java-index,Java Essentials,New To Java...