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.


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

Results 1 to 2 of 2

Thread: How to end a loop when valid entry is made

  1. #1
    Junior Member javaNewbie2014's Avatar
    Join Date
    Sep 2014
    Thanked 1 Time in 1 Post

    Default How to end a loop when valid entry is made

    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");
             JOptionPane.showMessageDialog(null, "You entered " + idNumber + ".\nThe student's name is "
                + studentName + " and the GPA is " + studentGPA);                

  2. The Following User Says Thank You to javaNewbie2014 For This Useful Post:

    Ram Lakshmanan (October 10th, 2018)

  3. #2
    Super Moderator
    Join Date
    Jun 2013
    So. Maryland, USA
    My Mood
    Thanked 698 Times in 680 Posts

    Default Re: How to end a loop when valid entry is made

    Do not use '==' to compare String objects. Use the equals() method instead.

Similar Threads

  1. Valid ticket id
    By Java girl in forum What's Wrong With My Code?
    Replies: 12
    Last Post: April 24th, 2014, 04:24 AM
  2. Valid Ticket
    By Java girl in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 23rd, 2014, 07:56 AM
  3. After a number of pin attempts it will end the loop
    By kulotz in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 3rd, 2013, 05:37 PM
  4. Is this even a valid way of getting Ticks Per Second?
    By vividMario52 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 18th, 2013, 07:25 AM
  5. How to sort a Map.Entry in a for loop?
    By Porknbeans in forum Collections and Generics
    Replies: 1
    Last Post: March 23rd, 2012, 07:02 AM