Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Search Arrays?
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Shiro786




PostPosted: Mon Jun 11, 2007 4:22 pm   Post subject: Search Arrays?

Hey! Sorry to bug everyone again, but it's me, Shiro with some more Novice questions. Razz


Please consider the following (Bill Nye FTW >_>):

code:
        //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] <= 49)
            {
                grade [i] = "F";
            }
            c.clear ();

        }
        //OUTPUT

        //Header
        c.print (" ", 40 - b.length () / 2);
        c.println (b, 40 - "Student Info".length () / 2);
        c.print (" ", 45 - b.length () / 2);
        c.println ("Student Info", 40 - b.length () / 2);
        c.print (" ", 40 - b.length () / 2);
        c.println (b);
        c.println ();
        c.print ("Name", 20);
        c.print ("Age", 20);

        c.print ("Number Grade", 20);
        c.println ("Letter Grade");

        //FOR LOOP FOR OUTPUT
        double avg = 0;

        for (int i = 0 ; i < num ; i++)
        {
            //Output
            c.print (name [i]);
            c.print (age [i], (20 + (a.length () / 2)) - name [i].length ());
            c.print (percent [i], 26, 2);
            c.print (" ", 18 - d.length () / 2);
            //Average
            c.println (grade [i]);
            avg = avg + percent [i];
            //Search: Age
        }
        c.println ();
        c.println ("The following students names listed, are over 16 years old, and have an A or A+ average in the class.");
        c.println ();
        for (int i = 0 ; i < num ; i++)
        {
            if (age [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]
Sponsor
Sponsor
Sponsor
sponsor
Ultrahex




PostPosted: Mon Jun 11, 2007 5:28 pm   Post subject: 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

Java:

public class SearchForName
{
    public static void main (String[] args)
    {
        // Lets Create My Array of names
        String[] names = {"Alex","Jim","Bob"};
       
        // Now Lets Try to find Jim In My Array
       
        // For Each Item in The Array, Check if the Names Are Equal
        for (int i=0;i<names.length;i++) {
            if (names[i].equals("Jim")) { // Check if name at index i is equal to "Jim"
                System.out.println("I Found JIM!");
                break; // Quit For Loop, I've Already Found Jim
            }
        }
    }
}
Shiro786




PostPosted: Mon Jun 11, 2007 6:53 pm   Post subject: 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.

Smile
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 3 Posts ]
Jump to:   


Style:  
Search: