Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Need help using JOptionPane/entering valid id#

  1. #1
    Junior Member javaNewbie2014's Avatar
    Join Date
    Sep 2014
    Location
    Illinois
    Posts
    6
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Need help using JOptionPane/entering valid id#

    I am working on allowing a user to enter an ID # using JOptionPane. If the ID is valid, then it will display the ID#, student name, and GPA. If it is not valid, then the window should pop up again to allow the user to enter a valid ID. I have managed to get that far.

    I am struggling with once a valid ID # is entered, how do I get the loop to stop? I have tried several different ways, but the code below is the most recent one, and it is still not working! Any help/suggestions to try would be greatly appreciated.

    import javax.swing.*;
    public class StudentIDArray
    {
       public static void main(String[] args)
       {
          String enteredID;
          int idNumber;
          int x = 0; 
          String studentName = "";
          double studentGPA = 0.0; 
          boolean validID = false;
     
          int[] id = {1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008, 1009, 1010};
          String[] names = {"Bob", "Joe", "Fred", "Suzie", "Jane", "Megan", "Frank", "Jill", "Tom", "Judy"};
          double[] gpa = {3.5, 3.32, 3.7, 4.0, 3.72, 3.93, 2.6, 1.95, 2.18, 2.83};
     
          enteredID = JOptionPane.showInputDialog(null, "Enter a four-digit student ID number");
          idNumber = Integer.parseInt(enteredID);
     
          for(x = 0; x < id.length; ++x)
          {
             if(idNumber == id[x])
             {
                validID = true;
                studentName = names[x];
                studentGPA = gpa[x];
             }
          }
     
          if(validID == false)
             JOptionPane.showMessageDialog(null, "You entered an invalid ID number");
          else                   
             JOptionPane.showMessageDialog(null, "You entered " + idNumber + ".\nThe student's name is "
                + studentName + " and the GPA is " + studentGPA);                
       }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Need help using JOptionPane/entering valid id#

    how do I get the loop to stop?
    What loop is giving you problems? The for loop should end ok.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member javaNewbie2014's Avatar
    Join Date
    Sep 2014
    Location
    Illinois
    Posts
    6
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Need help using JOptionPane/entering valid id#

    Quote Originally Posted by Norm View Post
    What loop is giving you problems? The for loop should end ok.
    It would help if I checked that I posted the correct code that shows the issue!

    Once an invalid ID # is entered, I can't figure out how to get it to switch to validID = true, and move on with displaying the student's name and GPA. For example, when I enter 1234, it shows an error and asks me to enter another ID number. I enter 1005 (which is on the list of Student ID #'s), it still shows as an error and want me to enter another number. It keeps doing that until I close it.

    while(validID == false)
          {
             enteredID = JOptionPane.showInputDialog(null,"You entered an invalid ID number.  Please enter a student ID #");
             idNumber = Integer.parseInt(enteredID);
             for(x = 0; x < id.length; ++x)
             {
                if(idNumber == id[x])
                {
                   validID = true;
                   studentName = names[x];
                   studentGPA = gpa[x];
                }
                else
                   validID = false;
             }
          }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Need help using JOptionPane/entering valid id#

    Is it possible the value of validID can be set true and then later in the loop it can be set false?
    Once a true is found, the code should exit the loop.
    A couple of ways to do that:
    use the break statement
    include a test of the value of validID in the for loops termination condition.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member javaNewbie2014's Avatar
    Join Date
    Sep 2014
    Location
    Illinois
    Posts
    6
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Need help using JOptionPane/entering valid id#

    Thanks for the feedback. I tried a couple different ways like you mentioned, but I'm wondering if I am not putting the information in the correct place.

    When I used just the break statement, it let me enter a new ID # the second time, but regardless of what I entered, it kicked it out and left the name & GPA as the default values. I tried putting the break statement in a couple different places, but none of them were making it work the way I want it to work. I'm just confused on what would be causing the validID to change back to false after I enter a valid ID #, and cause the loop to continue to execute.

    for(x = 0; x < id.length && validID == false; ++x)
          {
             if(idNumber != id[x])
             {
                validID = false;
                enteredID = JOptionPane.showInputDialog(null,"You entered an invalid ID number.  Please enter a student ID #");
                idNumber = Integer.parseInt(enteredID);
             }
             else
             {
                studentName = names[x];
                studentGPA = gpa[x];
                validID = true;
                break;
             }
          }

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Need help using JOptionPane/entering valid id#

    Do you need to tell the user something for every item in the array that you look at that does not match?
    Would it be better to wait until all the elements in the array had been looked at before telling the user that his entry was not in the array? See post#3
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member javaNewbie2014's Avatar
    Join Date
    Sep 2014
    Location
    Illinois
    Posts
    6
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Need help using JOptionPane/entering valid id#

    I changed it back to the way I had it in post #3. I made the while statement separate and searched through the array, and it worked! Thanks so much for your help!!

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Need help using JOptionPane/entering valid id#

    Glad you got it working.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [SOLVED] why is the program not entering the if statement?
    By nickans in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 5th, 2014, 01:07 AM
  2. Upadating Jtable while entering the data
    By kindk12 in forum AWT / Java Swing
    Replies: 3
    Last Post: April 14th, 2014, 01:28 PM
  3. Entering and running a Program
    By Bijaysadhok in forum Java Theory & Questions
    Replies: 1
    Last Post: February 23rd, 2012, 07:15 AM
  4. Entering objects from a file into an array
    By lookalive34 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 28th, 2011, 09:33 PM
  5. Not entering while loop
    By Kakashi in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 6th, 2011, 02:15 PM