Question regarding Arrays

Just to give you a heads up on what I'm doing I am currently building a phone searching program that will search a set of parallel arrays and return information to the user based on the id number they enter. I have figured out how to create 3 parallel arrays (I have initialized those arrays to one containing the id numbers, the names of the people, and the phone numbers).

I am confused about the parse method. I used the JOptionPane to ask the user to enter an id number. I am use to the user entering an int or a double and I understand how to parse those data types but I don抰 understand when it comes to a string what exactly to do. Do I parse it to a char using the charAt(0)?

When the user enters the ID number I have a for loop that searches to make sure that the ID number is valid How do I then take that ID number and output the parallel information like for example:

if(deptIdFound)

JOptionPane.showMessageDialog(null,

"Id Number is " + idNum +"/nName is " +

name +"/nPhone Number is " + phoneNum);

I am probably making this seem more difficult than it needs to me 0r over analyzing. Any help would be appreciated.

Thanks,

-Ian-

[1317 byte] By [Ian_Hilla] at [2007-10-2 23:36:45]
# 1
can you post some codes so we can see whats going on your app
jgalacambraa at 2007-7-14 16:19:10 > top of Java-index,Java Essentials,New To Java...
# 2

I just realized I did not declare my array. Im sure this has a lot to do with my problem. This is what I have:

import javax.swing.*;

public class SearchList

{

public static void main(String[] args)

{

String[] directory = new String[5];

String[] idNum = {"101", "107", "109", "112", "217"};

String[] name = {"B. B.", "B. H.", "S. H.",

"B. K.", "J. J."};

String[] phoneNum = {"601-555-5555", "478-777-7777", "561-999-9999",

"912-555-0000, "202-777-1111"};

String id;

int x;

boolean deptIdFound = false;

id = JOptionPane.showInputDialog(null,

"Enter an Id Number");

char aChar = id.charAt(0);

for(x=0; x<idNum.length; ++x)

if(id.equals(idNum[x]))

deptIdFound = true;

if(deptIdFound)

JOptionPane.showMessageDialog(null,

"Id Number is " + idNum + "/nName is " +

name + "/nPhone Number is " + phoneNum);

else

JOptionPane.showMessageDialog(null, id +

" was not found in the list");

System.exit(0);

}

}

>

Ian_Hilla at 2007-7-14 16:19:10 > top of Java-index,Java Essentials,New To Java...
# 3

First, always use braces for 'if', 'else', 'else if', 'for', 'while', etc., even if you currently have only one line associated with them. Your code will be easier to read, and you won't have to remember to add the braces later if you need a second line of code (as you need now).

In your 'for' loop, you need to record the value of 'x' where the ID number matched. Then, in the println, you can return idNum[x], name[x], phoneNum[x].

MLRona at 2007-7-14 16:19:10 > top of Java-index,Java Essentials,New To Java...
# 4

i remodified your code and add an arrayIndex:

import javax.swing.*;

public class SearchList

{

public static void main(String[] args)

{

String[] directory = new String[5];

String[] idNum = {"101", "107", "109", "112", "217"};

String[] name = {"B. B.", "B. H.", "S. H.",

"B. K.", "J. J."};

String[] phoneNum = {"601-555-5555", "478-777-7777", "561-999-9999",

"912-555-0000, "202-777-1111"};

String id;

int arrayIndex = 0;

int x;

boolean deptIdFound = false;

id = JOptionPane.showInputDialog(null,

"Enter an Id Number");

char aChar = id.charAt(0);

for(x=0; x<idNum.length; ++x){

if(id.equals(idNum[x])){

deptIdFound = true;

arrayIndex = x;

}

}

if(deptIdFound)

JOptionPane.showMessageDialog(null,

"Id Number is " + idNum[arrayIndex] + "/nName is " +

name[arrayIndex]+ "/nPhone Number is " + phoneNum[arrayIndex]);

else

JOptionPane.showMessageDialog(null, id +

" was not found in the list");

System.exit(0);

}

}

please tell me if you god some errors, i only modified it on text editor and not try to make it run.. thank you>

jgalacambraa at 2007-7-14 16:19:10 > top of Java-index,Java Essentials,New To Java...
# 5
Is the use of the three arrays a requirement? If not why don't you create a class to hold the three values then create objects and store them in an ArrayList.
floundera at 2007-7-14 16:19:10 > top of Java-index,Java Essentials,New To Java...
# 6
In the assignment we had specific instructions to create a public class with a main method and create 3 parallel arrays of the type string. So far I'm at least on the right track correct?
Ian_Hilla at 2007-7-14 16:19:10 > top of Java-index,Java Essentials,New To Java...
# 7
> In the assignment we had specific instructions to create a public class with a main method and create 3 parallel arrays of the type string.In other words, the instructor has demanded that you use a mackerel to hang drywall. :o)~
yawmarka at 2007-7-14 16:19:10 > top of Java-index,Java Essentials,New To Java...
# 8
I guess so :)
Ian_Hilla at 2007-7-14 16:19:10 > top of Java-index,Java Essentials,New To Java...
# 9

> First, always use braces for 'if', 'else', 'else if', for', 'while', etc., even if

> you currently have only one line associated with them.

Sissie; real manly programmers *never* need more than one line to express themselves clearly.

kind regards,

Jos ( real manly programmer ;-)

JosAHa at 2007-7-14 16:19:10 > top of Java-index,Java Essentials,New To Java...
# 10
Thanks jgalacambra you have been a tremendous help!-Ian-
Ian_Hilla at 2007-7-14 16:19:10 > top of Java-index,Java Essentials,New To Java...