
-----------------------------------
Shiro786
Mon Jun 11, 2007 4:22 pm

Search Arrays?
-----------------------------------
Hey! Sorry to bug everyone again, but it's me, Shiro with some more Novice questions. :P


Please consider the following (Bill Nye FTW >_>):

        //BEGIN ARRAY
        c.println ("How many students names in the class do you want to enter?");
        int num = c.readInt ();
        c.println ();

        //DECLARE VARIABLES
        String name[] = new String [num];
        int age[] = new int [num];
        String store[] = new String [num];
        double percent[] = new double [num];
        String grade[] = new String [num];
        String a = ("Name");
        String b = ("/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\");
        String d = ("A+");

        //FOR LOOP ENTER VALUES
        for (int i = 0 ; i < num ; i++)
        {
            //Ask questions for entering values
            c.println ("What is your full name?");
            name [i] = c.readLine ();
            c.println ("What is your age?");
            age [i] = c.readInt ();

            c.println ("What is your number grade? (in percentage)");
            percent [i] = c.readDouble ();

            //Programming Grade
            if (percent [i] > 101)
            {
                grade [i] = "ERROR";
            }
            else if (percent [i] >= 90)
            {
                grade [i] = "A+";
            }
            else if (percent [i] >= 80)
            {
                grade [i] = "A";
            }
            else if (percent [i] >= 70)
            {
                grade [i] = "B";
            }
            else if (percent [i] > 60)
            {
                grade [i] = "C";
            }
            else if (percent [i] >= 50)
            {
                grade [i] = "D";
            }
            else if (percent [i]  16)
            {
                if (percent [i] > 79)
                {
                    c.println (name [i]);
                }
            }
        }
        avg = (avg / num);
        c.println ();
        c.print ("The class average is ");
        c.print (avg, 0, 2);
        c.println ("%");


(Think of c.println as System.out or whatever, I was taught this type of Java differently cause my teacher is a stoner.)

And so, how am I to search through the arrays for a student name? I would like to print it after as it is required to by same said teacher. 

Thanks in advance![/quote]

-----------------------------------
Ultrahex
Mon Jun 11, 2007 5:28 pm

Re: Search Arrays?
-----------------------------------
The easiest way is to loop through each name in the array, and check whether it is equal to the name you are looking for

for example this code looks for the name Jim in the names String Array


public class SearchForName
{
    public static void main (String

-----------------------------------
Shiro786
Mon Jun 11, 2007 6:53 pm

Re: Search Arrays?
-----------------------------------
Wish I saw your post 2 hours ago. Fortunately, I found out how to do it in the end!

I totally forgot that == just compares strings. Thanks for your help man. And thanks to everyone else who have helped me thus far. 

:)
