WebDivx
11-28-2009, 02:07 PM
I am trying to practice java, but need some help
I want to create a multi_dimensional array. The array has store the first name of a celebrity and the last name of a celebrity.
So what I need the program to do is ask the user to enter 4 names (first and last) and then store them into an array.
Then I need a prompt that asks the user to input the first name, and search the array for the name. When the name is found, it prints the first and the last name.
So an example:
Enter a first name:Oprah
Enter a last name:Winfrey
Enter a first name:Bill
Enter a last name:Gates
Enter a first name:Kobe
Enter a last name:Bryant
Enter a first name:Cheryl
Enter a last name:Cole
Enter a first name to search for: Kobe
Kobe Bryant
Does anybody know how to do this? Help is much appreciated!
Kohrar
11-28-2009, 06:53 PM
Are you trying to make a primitive array of strings? Or are you using a vector or an array list or a collection of strings?
WebDivx
11-28-2009, 09:44 PM
Are you trying to make a primitive array of strings? Or are you using a vector or an array list or a collection of strings?
an array list. do you know how to do it? :rolleyes:
CArmstrong
11-28-2009, 09:55 PM
an array list. do you know how to do it? :rolleyes:
A two-dimensional array (primitive data type) and an ArrayList (object) are two different things. So which are you trying to use?
A two-dimension array would be best. Just create an array:
String[][] myArray = new String[4][2]; // containing 4 rows and 2 columns
Write whatever code you want to fill it up:
myArray[0][0]=firstGuyFirstName; // position row 0, column 0
myArray[0][1]=firstGuyLastName; // position row 0, column 1
When searching to see if a first name exists, start at position [0][0] and index to position [myArray.length-1][0], checking Strings at [x][0]. If it's a match, the last name is contained at [x][1].
Kohrar
11-28-2009, 11:44 PM
an array list. do you know how to do it? :rolleyes:
If you insist on using an array list, you will have to put the first / last name into an object so that it can be placed into the array list.
ArrayList<Person> names = new ArrayList();
Person p = new Person();
p.first = <first name>;
p.last = <last name>;
names.add(p);
Where Person can be defined like:
public static class Person {
public String first, last;
}
But that's not really a multi-dimensional array now is it? ;)
WebDivx
11-29-2009, 03:35 AM
A two-dimensional array (primitive data type) and an ArrayList (object) are two different things. So which are you trying to use?
A two-dimension array would be best. Just create an array:
String[][] myArray = new String[4][2]; // containing 4 rows and 2 columns
Write whatever code you want to fill it up:
myArray[0][0]=firstGuyFirstName; // position row 0, column 0
myArray[0][1]=firstGuyLastName; // position row 0, column 1
When searching to see if a first name exists, start at position [0][0] and index to position [myArray.length-1][0], checking Strings at [x][0]. If it's a match, the last name is contained at [x][1].
actually this is what i wanted to do, lol. can you tell me more?
CArmstrong
11-29-2009, 03:41 AM
actually this is what i wanted to do, lol. can you tell me more?
I already gave you everything you need to implement it.
barry[CoffeeSprout]
11-30-2009, 10:21 AM
actually this is what i wanted to do, lol. can you tell me more?
You learn more if you do your own homework.
You can read more here: http://www.brpreiss.com/books/opus5/html/page9.html
WebDivx
12-04-2009, 12:26 PM
Thanks so much! I was able to figure it out with your guidance guys!