Results 1 to 1 of 1
  1. #1
    fredrik8719 is offline Member
    Join Date
    Apr 2012
    Posts
    1
    Rep Power
    0

    Unhappy Why wont it work???

    Hi i'm having a problem with a piece of code i have written. It compiles and runs and for the most part does as i intent but when it comes to register time part, then it gets a bit confusing.

    The code is seperated into two files
    The file Competition


    Java Code:
    import javax.swing.*;  
    import java.awt.*;  
    import java.awt.event.*;  
    import java.util.*;  
    
    
    class Competition extends JFrame
    {   
        public static ArrayList<Participants> list = new ArrayList<Participants>();
    
    	JRadioButton rb1 = new JRadioButton("Startnr", true); 
    	JRadioButton rb2 = new JRadioButton("Name", false); 
    	JRadioButton rb3 = new JRadioButton("Age", false);
    	JRadioButton rb4 = new JRadioButton("Tome", false);
        JTextArea center = new JTextArea(); 
    
        Competition()
        {
            super ("DSV Kista Marathon");
            setLayout(new BorderLayout());
    
    	JPanel north=new JPanel();
    	north.add(new JLabel("Dsv Kista Marathon"));
    	add(north, BorderLayout.NORTH);
    		
        JPanel east = new JPanel(); 
    	east.setLayout(new BorderLayout()); 
    	JPanel seast = new JPanel(); 
    	seast.setLayout(new BoxLayout(seast, BoxLayout.Y_AXIS)); 
    	seast.add(new JLabel("Sortering")); 
    	
    	seast.add(rb1); 
    	seast.add(rb2); 
    	seast.add(rb3); 
    	seast.add(rb4); 
    
    
    	ButtonGroup grupp = new ButtonGroup(); 
    	grupp.add(rb1); 
    	grupp.add(rb2); 
    	grupp.add(rb3); 
    	grupp.add(rb4); 
    	east.add(seast, BorderLayout.SOUTH);
    	add(east, BorderLayout.EAST); 
    	   
    	   
    	JPanel south=new JPanel();
    	south.setLayout(new BorderLayout());
    	JPanel southw=new JPanel();
    	southw.setLayout(new BoxLayout (southw, BoxLayout.X_AXIS));
    
    
    	JButton b1 = new JButton("Ny");
    	b1.addActionListener(new B1());
    	southw.add(b1);
    	
    	JButton b2 = new JButton("Visa");
    	b2.addActionListener(new B2());
    	southw.add(b2);
    	
    	JButton b3 = new JButton("Tid");
    	b3.addActionListener(new B3());
    	southw.add(b3);
    	
    	south.add(southw, BorderLayout.WEST); 
    	add(south, BorderLayout.SOUTH);
    	
    	center=new JTextArea();    
    	center.setEditable(false);
    	JScrollPane scroll=new JScrollPane(center);
    	scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
    	add(scroll, BorderLayout.CENTER);
    
            //Set
            setLocation(500,200);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            setSize(300,350);
            setVisible(true);
    
        }
    
    
        public int getStartnr()
        {
            int startnr = list.size()+1;
            return startnr;
        }
    
        public static void main(String[] args) 
        {
            new Competition();
        }
    
    
        class B1 implements ActionListener
        {
            public void actionPerformed(ActionEvent ave) { 
                new F1();
            
    		
    		}   
        }
    
    
        class B2 implements ActionListener
        {
    		public void actionPerformed(ActionEvent ave) 
            {
                if (rb1.isSelected()){
                    Collections.sort(list, new Comparator<Object>(){
                   	 
                        public int compare(Object o1, Object o2) {
                        	Participants p1 = (Participants) o1;
                        	Participants p2 = (Participants) o2;
                           return p1.getStartnr() - p2.getStartnr();
                        }
                    });
                    center.setText("");
                	 for(int i=0; i<list.size(); i++) {
                    center.append(list.get(i) + "\n"); 
                	 }
                }
                else if (rb2.isSelected()){
                    Collections.sort(list, new Comparator<Object>(){
                    	 
                        public int compare(Object o1, Object o2) {
                        	Participants p1 = (Participants) o1;
                        	Participants p2 = (Participants) o2;
                           return p1.getName().compareToIgnoreCase(p2.getName());
                        }
                    });
                        center.setText("");                
                    for(int i=0; i<list.size(); i++) {
                        center.append(list.get(i) + "\n");                	
                    }
                }
                else if (rb3.isSelected()){
                    Collections.sort(list, new Comparator<Object>(){
                      	 
                        public int compare(Object o1, Object o2) {
                        	Participants p1 = (Participants) o1;
                        	Participants p2 = (Participants) o2;
                           return p1.getAge() - p2.getAge();
                        }
                    });
                	center.setText("");                
                	for(int i=0; i<list.size(); i++) {
                		center.append(list.get(i) + "\n");                	
                	}
                }
                else if (rb4.isSelected()){
                    Collections.sort(list, new Comparator<Object>(){
    				
    					public int compare (Object o1, Object o2) {
    						Participants p1 = (Participants) o1;
    						Participants p2 = (Participants) o2;
    				return (int) (p2.getTime() - p1.getTime());
    				}
    			});
    				center.setText("");
                    for(int i=0; i<list.size(); ++i) {
                    	center.append(list.get(i) + "\n");                	
                }
                }
            }   
        }
    
    
        class B3 implements ActionListener
        {
        	
            public void actionPerformed(ActionEvent ave) {
                new F2();
            }   
        }
    
    
    
        class F1 implements ActionListener
        {       
            JTextField nfield = new JTextField(12);
            JTextField cfield = new JTextField(12);
            JTextField afield = new JTextField(3);
            String name;
            double time = 0;
    
            F1()
            {
                JPanel form = new JPanel();
                form.add(new JLabel("Startnr "+ getStartnr()));
                form.setLayout(new BoxLayout(form, BoxLayout.Y_AXIS));
    
                JPanel r1 = new JPanel();
                r1.add(new JLabel ("Namn:"));
                r1.add(nfield);
                form.add(r1);
                nfield.addActionListener(this);
    
                JPanel r2 = new JPanel();
                r2.add(new JLabel ("Land:"));
                r2.add(cfield);
                form.add(r2);
                cfield.addActionListener(this);
    
                JPanel r3 = new JPanel();
                r3.add(new JLabel ("Ålder:"));
                r3.add(afield);
                form.add(r3);
                afield.addActionListener(this);
    
    			
    			for(;;){
    				try{
    			
                int choice = JOptionPane.showConfirmDialog(null, form, "New Participant"
                                                   , JOptionPane.OK_CANCEL_OPTION);
                                            
                if (choice == JOptionPane.OK_OPTION)
                
                    name = nfield.getText(); 
                    String country = cfield.getText();
    				
              	if (country.isEmpty() || name.isEmpty())
    			throw new MissingFormatArgumentException("Land or Name is empty");
    
    			int age = Integer.parseInt(afield.getText());
                int startnr = getStartnr();
    
    			if (age < 18 || age > 100) 
    		throw new IllegalArgumentException();	
               
    			else if (choice == JOptionPane.CANCEL_OPTION) 
                {
                    System.out.println("CANCEL OPTION SELECTED, DO SOMETHING NOW");
                }				
    				
             list.add(new Participants(name, country, age, startnr, time));
    			break;	
    		 } 
    				catch (NumberFormatException e){
    					JOptionPane.showMessageDialog(null, "Age must contain a numerical value","An error has occured" , JOptionPane.ERROR_MESSAGE);
    				} 
    				catch (MissingFormatArgumentException e){
    					JOptionPane.showMessageDialog(null, "Name and/or land is missing content","An error has occured" , JOptionPane.ERROR_MESSAGE);
    				}
    				catch (IllegalArgumentException e){
    					JOptionPane.showMessageDialog(null, "Participant must be between 18 and 100 years old.","An error has occured" , JOptionPane.ERROR_MESSAGE);
    				}
    			}
            }
    
            public void actionPerformed(ActionEvent ave) 
            {
                String name = nfield.getText();
                String country = cfield.getText();
                int age = Integer.parseInt(afield.getText());
                int startnr = getStartnr();
    
                list.add(new Participants(name, country, age, startnr, time));       
            }   
        }
    
        //JOptionPane - Tid (Registrera ny)
        class F2 implements ActionListener
        {       
            JTextField field1 = new JTextField(6);
            JTextField field2 = new JTextField(6);
            double time;
            int startnr;
    		
            F2()
            {
                JPanel form = new JPanel();
                form.setLayout(new BoxLayout(form, BoxLayout.Y_AXIS));
    
                JPanel r1 = new JPanel();
                r1.add(new JLabel ("Startnr:"));
                r1.add(field1);
                form.add(r1);
                field1.addActionListener(this);
    
                JPanel r2 = new JPanel();
                r2.add(new JLabel ("Time:"));
                r2.add(field2);
                form.add(r2);
                field2.addActionListener(this);
                Collections.sort(list, new Comparator<Object>(){
                  	 
                    public int compare(Object o1, Object o2) {
                    	Participants p1 = (Participants) o1;
                    	Participants p2 = (Participants) o2;
                       return p1.getStartnr() - p2.getStartnr();
                    }
                });
    
       		
    
    			try{
    			
    			JOptionPane.showConfirmDialog(null, form, "Register time", JOptionPane.OK_CANCEL_OPTION);
    			time = Double.parseDouble(field2.getText());
    			startnr = Integer.parseInt(field1.getText());
    
            
    			if (startnr > list.size() || startnr < 0) {
    			throw new MissingFormatArgumentException("Wrong startnr, no participant match!");
    			
    		}	
    			else {
    				boolean areEqual;
    				for(int i=0; i<list.size(); i++)
    				{
    					if(startnr==(getStartnr())) {
    						areEqual = true;
    					}
    					
    					
    					if(areEqual = true){
    						Participants d = list.get(startnr-1);
    						d.setTime(time);
    					}
    				}
    			}
     
    	}
              	
      
    	catch (NumberFormatException e){
    		JOptionPane.showMessageDialog(null, "An error has occured, please try again!","An error has occured" , JOptionPane.ERROR_MESSAGE);
    				} 
    	
    	catch (MissingFormatArgumentException e){
    		JOptionPane.showMessageDialog(null, "This staringnr doesnt exist.","An error has occured" , JOptionPane.ERROR_MESSAGE);
    				}
    	
    	catch (IllegalArgumentException e){
    		JOptionPane.showMessageDialog(null, "This staringnr doesnt exist.","An error has occured" , JOptionPane.ERROR_MESSAGE);
    	
    			}
    		}
    	
    	
    	
    	public void actionPerformed(ActionEvent ave) 
            { 
            
            int startnr = getStartnr();
    			
            list.get(startnr-1);
    		} 
        }
    }[/I]
        
    	
    [B]And the file Participants[/B]
    
    [I]public class Participants
    {
        public String name,country, doubleString = "--";
        public int age;
        public int startnr;
        public double time;
        
    
        public Participants(String n, String c, int a, int b, double t )
        {
            this.name = n;
            this.country = c;
            this.age = a;   
            this.startnr = b;
            this.time = t;
        }
    
        public void setStartnr(int b)
        {
            startnr = b;
        }
    
        public int getStartnr()
        {
            return startnr; 
        }
        
    
        public String getName()
        {
            return name;    
        }
    
        public String getCountry()
        {
            return country; 
        }
    
        public int getAge()
        {
            return age; 
        }
        
        public void setTime(double t)
        {
        	if (t >= 0 ) {
        		doubleString = Double.toString(t);
        	    time = t;	
        	}
        }
        
        public double getTime()
        {
        	return time;
        }
        
    
        public String toString()
        {
        	return startnr + "" + name + " " + country + " " + age + " " + doubleString;	
        }
    
    }
    The time is registred and sorted correctly at first but after a few shots at registering time it gets a bit strange and starts to sort all wrong. Im wondering why, and if there is some way to make sure it sort it correctly instead.

    //Fredrik
    Last edited by JosAH; 04-08-2012 at 08:23 PM. Reason: changed [i] ... [/i] tags to [code] ... [/code tags

Similar Threads

  1. Why Wont This Work???
    By Billywizz in forum New To Java
    Replies: 11
    Last Post: 03-09-2011, 02:33 AM
  2. why wont the %.2f work here?
    By jjth39347 in forum New To Java
    Replies: 2
    Last Post: 03-06-2011, 05:55 AM
  3. importing JCurses wont work!
    By TheBreadCat in forum New To Java
    Replies: 0
    Last Post: 02-16-2011, 08:46 PM
  4. JApplet buttons wont work
    By Mark101 in forum New To Java
    Replies: 3
    Last Post: 03-16-2010, 06:53 PM
  5. save will work but load wont?!?!
    By Sticks_ll in forum New To Java
    Replies: 1
    Last Post: 06-12-2008, 04:19 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
  •