Results 1 to 4 of 4
  1. #1
    jonmackey22 is offline Member
    Join Date
    Jan 2015
    Posts
    1
    Rep Power
    0

    Default If/ else statement with arrays

    In the program below I populated three arrays: student id, name, and GPA. The findStudent() method attempts to match users input with the input stored in the studentID array using an if/ else statement. When I use the if/ else statement the else always executes regardless if the IDs match? I am trying to get JOptionPane.showMessageDialog(null, "Incorrect entry"); to print only if the IDs don't match.

    Java Code:
    //FILE:   StudentIDArray.java
    
    import javax.swing.*;  //Used for the JOption dialog boxes
    import java.util.*;    //Used for Scanner input
    
    public class StudentIDArray
    {
     boolean  nameFound = true;
     final int numOfElements = 2;                 //Final int to control to loop for data
     Scanner  keyboard  = new Scanner(System.in);  //Used to accept user input
     String[] firstName = new String[10];          //Array to hold first names
     String[] studentID = new String[10];          //Array to hold student id
     String[] gradeAvg  = new String[10];          //Array to hold grade point average
     int x;
      
       //Method to input 10 names, 10 student ID numbers, and 10 grade point averages
       public void getInfo()
       {
          for(x = 0; x < numOfElements; ++x)
          { 
            //Populate the firstName[] array
            System.out.print("Enter the students first name: ");
            firstName[x] = keyboard.next();
          
             if(nameFound)
             {
              //Populates the studentID[] array
              System.out.print("Enter their student ID number");
              studentID[x] = keyboard.next();
             }
         
               if(nameFound)
               {
                //Populates the gradeAvg[] array
                System.out.print("Enter their grade point average");
                gradeAvg[x] = keyboard.next();
               }
            System.out.println(""); 
          }
       }
       
       //Method to find a students name and GPA based on a user student ID search
       public void findStudent()
       {
        //Prompts the user for a students ID number
        String id = JOptionPane.showInputDialog(null, "Enter the student's ID number");
        
          for(int x = 0; x < numOfElements; ++x)
          {  
             //If the users input matches the students ID their name is printed with their GPA
             if(id.equals(studentID[x]))
              {
               JOptionPane.showMessageDialog(null, "NAME: " +firstName[x]+ "   GPA: " +gradeAvg[x]+"");
              }
             else 
               JOptionPane.showMessageDialog(null, "Incorrect entry");    
         }
        
       }
       
       //Main method that calls the other methods in the StudentIDArray class
       public static void main(String args[]) 
       {
        StudentIDArray show = new StudentIDArray();
        show.getInfo();
        show.findStudent();
       }
       
    }

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,143
    Rep Power
    15

    Default Re: If/ else statement with arrays

    Time to debug your program. The goal is to figure out exactly what line the program's execution differs from what you expect, and then what the values of every variable on that line are.

    You can do this by stepping through this with a debugger, or by adding print statements, or simply walking through with a piece of paper and a pencil to keep track of variable values.

    I'll give you a hint. Look at this line:

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

    What is the value of id? What is the value of x? What is the value of studentID[x]?
    How to Ask Questions the Smart Way
    Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
    Static Void Games forum - Come say hello!

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: If/ else statement with arrays

    Another observation which probably has nothing to do with your problem. You have a boolean nameFound which is set to true. And then you use it in some if statements. But you never change it, thus it is not necessary. I define this stuff as clutter. And it can make your program hard to read and debug.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    ImTroyMiller is offline Member
    Join Date
    Jan 2014
    Posts
    23
    Rep Power
    0

    Default Re: If/ else statement with arrays

    This is what it is, sorry for the multiple edits...

    } else if (x == numOfElements) {

    ...in other words, you're iterating through the elements(two in your example), and then your not only printing out when you found them, but also printing out when you didn't find them(which is one not found in your example).
    Last edited by ImTroyMiller; 01-29-2015 at 01:23 AM.

Similar Threads

  1. Replies: 4
    Last Post: 06-28-2014, 05:00 PM
  2. Replies: 5
    Last Post: 05-18-2014, 03:41 AM
  3. Comparing arrays if statement Help !
    By steveday77 in forum New To Java
    Replies: 2
    Last Post: 04-30-2013, 10:27 PM
  4. Replies: 3
    Last Post: 11-19-2012, 08:12 PM
  5. Replies: 2
    Last Post: 03-26-2012, 04:27 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •