Results 1 to 5 of 5
Thread: Search in the array
- 04-27-2010, 09:50 AM #1
Member
- Join Date
- Jan 2010
- Posts
- 80
- Rep Power
- 0
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); } } }
- 04-27-2010, 10:49 AM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,385
- Blog Entries
- 7
- Rep Power
- 17
- 04-27-2010, 10:53 AM #3
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 10:54 AM. Reason: being slow
Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
The Ubiquitous Newbie Tips
- 04-27-2010, 11:08 PM #4
Member
- Join Date
- Jan 2010
- Posts
- 80
- Rep Power
- 0
I finally solved it.
the first problem was in this line
I exchange obj!=ID[i] with i<ID.length.Java Code:while( i<ID.length && obj!=ID[i]){
the second one
the problem with i ( if i = 3)Java Code:if(ID[i]==obj){
in this case I added
Java Code:if(i==ID.length){ i = i -1; }
- 04-28-2010, 08:02 AM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,385
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
How do I search an array from a different class?
By Psyclone in forum New To JavaReplies: 6Last Post: 02-04-2010, 12:21 AM -
Mostly completed array search program
By Ryujin89 in forum New To JavaReplies: 1Last Post: 11-04-2009, 03:25 AM -
Search a string in a byte array
By 2BOrNot2B in forum New To JavaReplies: 0Last Post: 03-12-2009, 05:52 PM -
Newbie search array question
By CirKuT in forum New To JavaReplies: 19Last Post: 09-14-2008, 06:26 AM -
Array Search Test
By Java Tip in forum java.langReplies: 0Last Post: 04-14-2008, 08:45 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks