Problems with While loop and GUI


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Problems with While loop and GUI

  1. #1
    Join Date
    Jun 2006
    Posts
    1

    Problems with While loop and GUI

    I'm heading in the right direction, however it still isn't functioning the way it should.

    Lets say I enter 202 (which is invalid) it does take me to the second GUI that informs the user that ID number isn't valid if they enter a number that IS valid the information isn't displayed it pops right back into the first GUI "enter an id number"

    I have tried to move the while around. I've tried to add to the error GUI. They don't show you this stuff in the book. So I'm pretty much stumped.

    Here is my code I'm sure it will prove helpful
    Code:
    import javax.swing.*;
    public class SearchList
    {
    	public static void main(String[] args)
    	{
    		String[] directory = new String[5];
    		String[] idNum = {"101", "107", "109", "112", "217"};
    		String[] name = {"B. B.", "B. H.", "S. H.", "B. K.", "J. J."};
    		String[] phoneNum = {"601-555-5555", "478-777-7777", "561-999-9999","912-555-0000", "202-777-1111"};
    				
    		String id;
    		int x;
    		int arrayIndex = 0;
    		boolean deptIdFound = false;
    		
    				
    		while(!deptIdFound)
    		{
    				
    			id = JOptionPane.showInputDialog(null, 
    				"Enter an Id Number");
    			char aChar = id.charAt(0);
    		
    			
    		
    			for(x=0; x<idNum.length; ++x)
    			{
    				
    				if(id.equals(idNum[x]))
    				{
    					deptIdFound = true;
    					arrayIndex = x;
    				}	
    			}
    			
    			
    	
    			if(deptIdFound)
    			{
    				JOptionPane.showMessageDialog(null, 
    					"Id Number is " + idNum[arrayIndex] + "\nName is " +
    					name[arrayIndex] + "\nPhone Number is " + phoneNum[arrayIndex]);
    			}
    			
    			else 
    			{
    				id = JOptionPane.showInputDialog(null, id +
    			 		" was not found in the list, please enter correct Id Number");
    			
    			}
    			
    		}	
    		
    		System.exit(0);
    	}
    }

    Anyone know my issue here?? I've tried everything

  2. #2
    Join Date
    Apr 2006
    Posts
    28
    Change a little bit

    if(deptIdFound)
    {
    JOptionPane.showMessageDialog(null,
    "Id Number is " + idNum[arrayIndex] + "\nName is " +
    name[arrayIndex] + "\nPhone Number is " + phoneNum[arrayIndex]);
    }

    else
    {
    JOptionPane.showMessageDialog(null, id+" was not foud in the list, Try again ","Error ",JOptionPane.ERROR_MESSAGE);
    }

Similar Threads

  1. Replies: 2
    Last Post: 03-16-2006, 06:39 PM
  2. while loop: keep all values
    By Aramis in forum Java
    Replies: 7
    Last Post: 04-23-2005, 04:43 PM
  3. Replies: 0
    Last Post: 12-09-2001, 11:45 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center