Results 1 to 5 of 5
  1. #1
    Join Date
    Jan 2010
    Posts
    80
    Rep Power
    0

    Default Search in the array

    Hi

    The following code is a simple example for using GUI to enter the ID number then determines whether the number is right or not.

    When I enter one of the three numbers that I wrote in the array, it gives me the correct message but if I enter any wrong number it will stop the program.

    Java Code:
    import javax.swing.*;
    
    import java.awt.*;
    import java.awt.event.*;
    import java.text.DateFormat;
    import java.util.Date;
    import java.util.Locale;
    
    public class Management extends JApplet implements ActionListener{
    	
    	private JLabel label;
    	private JLabel labelDate;
    	private JButton button;
    	private static Locale alocal;
    	private static Date today;
    	private static String dateOut;
    	private static DateFormat dateFormatter;
    	private int[] ID={1213,3343,7604};
    	
    	public boolean binarySearch(int obj){
    		int i=0;
    		boolean val;
    	
    			
    		while(obj!=ID[i] && i<ID.length){
    			i++;
    	}
    			
    			if(obj==ID[i]){
    			val = true;
    			}else{
    				
    				val = false;
    			}
    		return val;
    	}
    	
    	public void init(){
    		
    		alocal  = new Locale("CANADA");
    		dateFormatter = DateFormat.getDateInstance(DateFormat.DEFAULT,
    				alocal);
    		today = new Date();
    		dateOut = dateFormatter.format(today);
    		
    		label = new JLabel("Welcome in Management Programe");
    		button = new JButton("Entrance Panel");
    		labelDate = new JLabel("");
    		labelDate.setText(dateOut);
    		
    		button.addActionListener(this);
    		
    		
    		Container contentPane = getContentPane();
    		contentPane.setLayout(new FlowLayout());
    		contentPane.add(label);
    		contentPane.add(labelDate);
    		contentPane.add(button);
    		
    		
    			
    			
    	}
    	
    	public void actionPerformed(ActionEvent eve){
    		
    		String str = JOptionPane.showInputDialog(getContentPane(), "Enter your ID number");
    		int str1 = Integer.parseInt(str);
    		int i=0;
    		
    		if(binarySearch(str1)==true){
    			JOptionPane.showMessageDialog(getContentPane(), "Welcome Mr.NsNs");
    			i++;
    		}else{
    			JOptionPane.showMessageDialog(getContentPane(), "Error");
    			//JOptionPane.showMessageDialog(getContentPane(), "Invalid Name !!","Error" ,JOptionPane.ERROR_MESSAGE);
    		}
    	}
    }

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,421
    Blog Entries
    7
    Rep Power
    26

    Default

    Quote Originally Posted by ŖΫ ỏƒ Ңόρę View Post
    Java Code:
    	public boolean binarySearch(int obj){
    		int i=0;
    		boolean val;
    	
    			
    		while(obj!=ID[i] && i<ID.length){
    			i++;
    	}
    			
    			if(obj==ID[i]){
    			val = true;
    			}else{
    				
    				val = false;
    			}
    		return val;
    	}
    Your an my definition for 'binary search' are quite different. Hint: what happens when ID[0] is the wanted ID? Your seach method won't find it and throws an ArrayIndexOutOfBounds exception ... IOW that method stinks.

    kind regards,

    Jos

  3. #3
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,429
    Rep Power
    10

    Default

    It doesn't stop the program, you get an exception. Your binarySearch method is seriously borked.

    EDIT: what Jos said.
    Last edited by PhHein; 04-27-2010 at 11:54 AM. Reason: being slow
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  4. #4
    Join Date
    Jan 2010
    Posts
    80
    Rep Power
    0

    Default

    I finally solved it.

    the first problem was in this line

    Java Code:
    while( i<ID.length &&  obj!=ID[i]){
    I exchange obj!=ID[i] with i<ID.length.

    the second one

    Java Code:
    if(ID[i]==obj){
    the problem with i ( if i = 3)
    in this case I added

    Java Code:
    if(i==ID.length){
    				i = i -1;
    			}

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,421
    Blog Entries
    7
    Rep Power
    26

    Default

    Quote Originally Posted by ŖΫ ỏ Ңόρę View Post
    I finally solved it.

    the first problem was in this line

    Java Code:
    while( i<ID.length &&  obj!=ID[i]){
    I exchange obj!=ID[i] with i<ID.length.

    the second one

    Java Code:
    if(ID[i]==obj){
    the problem with i ( if i = 3)
    in this case I added

    Java Code:
    if(i==ID.length){
    				i = i -1;
    			}
    That's a very elaborate way to say just this:

    Java Code:
    for (int i= 0; i < ID.length; i++)
       if (ID[i] == obj)
          return i;
    return -1; // not found value
    kind regards,

    Jos

Similar Threads

  1. How do I search an array from a different class?
    By Psyclone in forum New To Java
    Replies: 6
    Last Post: 02-04-2010, 01:21 AM
  2. Mostly completed array search program
    By Ryujin89 in forum New To Java
    Replies: 1
    Last Post: 11-04-2009, 04:25 AM
  3. Search a string in a byte array
    By 2BOrNot2B in forum New To Java
    Replies: 0
    Last Post: 03-12-2009, 06:52 PM
  4. Newbie search array question
    By CirKuT in forum New To Java
    Replies: 19
    Last Post: 09-14-2008, 07:26 AM
  5. Array Search Test
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-14-2008, 09:45 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
  •