Results 1 to 3 of 3
  1. #1
    ravjot28 is offline Member
    Join Date
    Jun 2008
    Posts
    87
    Rep Power
    0

    Default Please Help me urgent help please

    I AM MAKING A QUIZ SIMULATOR AND I AM TAKING QUESTIONS FROM A FILE QUES.TXT FILE BUT WHEN I ADD MORE QUES TO QUES.TXT IT GIVES AN EXCEPTION OF ARRAYOUTOFBOUND
    PLEASE HELP ME

    My code is
    [code]
    import java.io.*;

    public class quesbank
    {
    String in[]=new String[5];
    String file="ques.txt";
    static int count=0;
    static int c=0;
    public quesbank(String name,String phone,String[] info)
    {
    in=info;
    String data;
    String q[]=new String[len()];
    String temp=null;
    try
    {
    BufferedReader b=new BufferedReader(new FileReader(file));
    data=b.readLine();
    while(data!=null)
    {
    if((count%5)==0)
    {
    temp=temp+data;
    q[c]=temp;
    c++;
    temp=null;
    count=0;
    }
    else
    {
    if(temp==null)
    {
    temp=data+"#";
    }
    else
    {
    temp=temp+data+"#";
    }


    }
    data=b.readLine();
    count++;
    }
    b.close();
    }catch(Exception e)
    {

    }
    new Frames(q,len(),name,phone,in);
    }

    public int len()
    {
    int l=0;
    String data;
    try
    {
    BufferedReader b=new BufferedReader(new FileReader(file));
    data=b.readLine();
    while(data!=null)
    {
    if(count==4)
    {
    count=0;
    l++;
    }
    else
    {
    count++;
    }
    data=b.readLine();
    }
    }catch(Exception e)
    {

    }
    System.out.println(l);
    return l;
    }

    }


    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.Calendar;
    import java.util.StringTokenizer;
    public class Frames extends JFrame implements ActionListener
    {
       static int flag11=0;
       String in[]=new String[5];
       private JTextField _timeField;
       static boolean checc;
       final int a;
       static int timee=1;
       static int correct=0;
       static int incorrect=0;
       static boolean click1[];
       static boolean click2[];
       static boolean click3[];
       static boolean click4[];
       static JButton lis[];
       final int co;
       final int inco;
       static String n;
       static String ph;
       static int li[];
       static String q[];
       static String aa1[];
       static String aa2[];
       static String aa3[];
       static String aa4[];
       static int sub[];
       static int visible=0;
       JButton prev,next,submit,finish;
       TextArea ques;
       JRadioButton r1,r2,r3,r4;
       JPanel p,p2,p3,a11,a22,a33,a44,listt;
       ButtonGroup bg;
       int flag=0;
       static int cq;
       JFrame ff;
        public Frames(String[] q,int l,String name,String phone,String[] info)
        {
            in=info;
            a=Integer.parseInt(in[2]);
            co=Integer.parseInt(in[3]);
            inco=Integer.parseInt(in[4]);
            checc=true;
            lis=new JButton[a];
            sub=new int[a];
            for(int i=0;i<a;i++)
            {
                lis[i]=new JButton(""+(i+1));
                lis[i].setActionCommand(""+i);
                lis[i].addActionListener(this);
            }
            click1=new boolean[a];
            for(int i=0;i<a;i++)
            {
                click1[i]=false;
            }
            click2=new boolean[a];
            for(int i=0;i<a;i++)
            {
                click2[i]=false;
            }
            click3=new boolean[a];
            for(int i=0;i<a;i++)
            {
                click3[i]=false;
            }
            click4=new boolean[a];
            for(int i=0;i<a;i++)
            {
                click4[i]=false;
            }
            n=name;
            ph=phone;
            for(int i=0;i<a;i++)
            {
                sub[i]=-1;
            }
            String qq[]=new String[l];
            String a1[]=new String[l];
            String a2[]=new String[l];
            String a3[]=new String[l];
            String a4[]=new String[l];
            int c=0;
            for(int i=0;i<l;i++)
            {
                StringTokenizer s=new StringTokenizer(q[i],"#");
                while(s.hasMoreTokens())
                {
                    String temp=s.nextElement().toString();
                    switch(c)
                    {
                        case 0: qq[i]=temp;
                            break;
                        case 1: a1[i]=temp;
                            break;
                        case 2:a2[i]=temp;
                            break;
                        case 3:a3[i]=temp;
                            break;
                        case 4:a4[i]=temp;
                            break;
                    }
                    c++;
                }
                c=0;
            }
            int list[]=new Random1().m(l,a);
            new TextClock1();
            launch(list,qq,a1,a2,a3,a4,0);
        }
    
        public void launch(int[] l,String[] qq,String[] a1,String[] a2,String[] a3,String[] a4,int cq)
        {
            li=l;
            q=qq;
            aa1=a1;
            aa2=a2;
            aa3=a3;
            aa4=a4;
            launch1(cq);
        }
    
        public void launch1(int cq)
        {
             if(flag11==a)
            {
                re();
            }
            else
            {
            if(!checc)
            {
                re();
            }
            if(check())
            {
                re();
            }
             LookAndFeel lf = UIManager.getLookAndFeel();
                   try
                   {
                        UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
                       //UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
                    } catch (Exception e) {
                        }
            ff=new JFrame("Test");
            bg=new ButtonGroup();
             ff.addWindowListener(new WindowAdapter(){public void windowClosing(WindowEvent e) {re();}});
             ff.addKeyListener(new KeyListener()
            {
                public void keyPressed(KeyEvent e)
                { if((e.isAltDown())||(e.isControlDown())||(e.getKeyCode()==KeyEvent.VK_WINDOWS))
                  {
    
                      re();
                  }
                }
    
                public void keyReleased(KeyEvent e) {}
    
                public void keyTyped(KeyEvent e) {}
            });
            ff.setLayout(new BorderLayout());
            p=new JPanel();
            p2=new JPanel(new GridLayout(2,2));
            p3=new JPanel();
            listt=new JPanel(new GridLayout(a,1));
            ques=new TextArea(q[li[cq]],3,50,TextArea.SCROLLBARS_VERTICAL_ONLY);
            ques.setEditable(false);
            ques.addKeyListener(new KeyListener()
            {
                public void keyPressed(KeyEvent e) { if((e.isAltDown())||(e.isControlDown())||(e.getKeyCode()==KeyEvent.VK_WINDOWS)){re();}}
    
                public void keyReleased(KeyEvent e) {}
    
                public void keyTyped(KeyEvent e) {}
            });
            p.addKeyListener(new KeyListener()
            {
                public void keyPressed(KeyEvent e) { if((e.isAltDown())||(e.isControlDown())||(e.getKeyCode()==KeyEvent.VK_WINDOWS)){re();}}
    
                public void keyReleased(KeyEvent e) {}
    
                public void keyTyped(KeyEvent e) {}
            });
            p2.addKeyListener(new KeyListener()
            {
                public void keyPressed(KeyEvent e) { if((e.isAltDown())||(e.isControlDown())||(e.getKeyCode()==KeyEvent.VK_WINDOWS)){re();}}
    
                public void keyReleased(KeyEvent e) {}
    
                public void keyTyped(KeyEvent e) {}
            });
            p3.addKeyListener(new KeyListener()
            {
                public void keyPressed(KeyEvent e) { if((e.isAltDown())||(e.isControlDown())||(e.getKeyCode()==KeyEvent.VK_WINDOWS)){re();}}
    
                public void keyReleased(KeyEvent e) {}
    
                public void keyTyped(KeyEvent e) {}
            });
            prev=new JButton(new ImageIcon("prev.gif"));
            next=new JButton(new ImageIcon("next.gif"));
            submit=new JButton(new ImageIcon("submit.gif"));
            finish=new JButton("Finish");
            finish.addActionListener(this);
            next.setActionCommand("next");
            prev.setActionCommand("prev");
            submit.setActionCommand("submit");
            next.addActionListener(this);
            prev.addActionListener(this);
            submit.addActionListener(this);
            _timeField = new JTextField(4);
            _timeField.setEditable(false);
            _timeField.setFont(new Font("sansserif", Font.PLAIN,16));
            for(int i=0;i<a;i++)
            {
                listt.add(lis[i]);
            }
            if(aa1[li[cq]].charAt(0)=='#')
            {
                String t4=aa1[li[cq]].substring(2);
                r1=new JRadioButton(t4,click1[cq]);
            }
            else
            {
                 r1=new JRadioButton(aa1[li[cq]],click1[cq]);
            }
             if(aa2[li[cq]].charAt(0)=='#')
            {
                String t4=aa2[li[cq]].substring(2);
                r2=new JRadioButton(t4,click2[cq]);
            }
            else
            {
                r2=new JRadioButton(aa2[li[cq]],click2[cq]);
            }
             if(aa3[li[cq]].charAt(0)=='#')
            {
                String t4=aa3[li[cq]].substring(2);
                r3=new JRadioButton(t4,click3[cq]);
            }
            else
            {
                 r3=new JRadioButton(aa3[li[cq]],click3[cq]);
            }
             if(aa4[li[cq]].charAt(0)=='#')
            {
                String t4=aa4[li[cq]].substring(2);
                r4=new JRadioButton(t4,click4[cq]);
            }
            else
            {
                r4=new JRadioButton(aa4[li[cq]],click4[cq]);
            }
            bg.add(r1);
            bg.add(r2);
            bg.add(r3);
            bg.add(r4);
            p2.add(r1);
            p2.add(r2);
            p2.add(r3);
            p2.add(r4);
            p.add(ques);
           // p.add(finish);
            p.add(_timeField);
            p3.add(prev);
            p3.add(submit);
            p3.add(next);
            p3.add(finish);
            ff.add(p,BorderLayout.NORTH);
            ff.add(p2,BorderLayout.CENTER);
            ff.add(listt,BorderLayout.EAST);
            ff.add(p3,BorderLayout.SOUTH);
            Toolkit tk = Toolkit.getDefaultToolkit();
            //ff.setLocation(400,200);
            ff.setSize(((int) tk.getScreenSize().getWidth()), ((int) tk.getScreenSize().getHeight()));
            ff.setUndecorated(true);
           // ff.pack();
            if(sub[cq]==0)
            {
                submit.setEnabled(false);
            }
            if(cq==0)
            {
                prev.setEnabled(false);
            }
            else
                if(cq==(li.length-1))
                {
                    next.setEnabled(false);
                }
            ff.setVisible(true);
            
        }
        }
        public void actionPerformed(ActionEvent e)
        {
            if(e.getActionCommand().equalsIgnoreCase("next"))
            {
                click1[cq]=r1.isSelected();
                click2[cq]=r2.isSelected();
                click3[cq]=r3.isSelected();
                click4[cq]=r4.isSelected();
                cq++;
                //ff.setVisible(false);
                //launch(li,q,aa1,aa2,aa3,aa4,cq);
                ques.setText(q[li[cq]]);
                r1.setText(aa1[li[cq]]);
                r1.setSelected(click1[cq]);
                r2.setText(aa1[li[cq]]);
                r2.setSelected(click2[cq]);
                r3.setText(aa1[li[cq]]);
                r3.setSelected(click3[cq]);
                r4.setText(aa1[li[cq]]);
                r4.setSelected(click4[cq]);
                ff.setVisible(false);
                launch1(cq);
            }
            else
                if(e.getActionCommand().equalsIgnoreCase("prev"))
                {
                    click1[cq]=r1.isSelected();
                    click2[cq]=r2.isSelected();
                    click3[cq]=r3.isSelected();
                    click4[cq]=r4.isSelected();
                    cq--;
                   // ff.setVisible(false);
                    //launch(li,q,aa1,aa2,aa3,aa4,cq);
                ques.setText(q[li[cq]]);
                r1.setText(aa1[li[cq]]);
                r1.setSelected(click1[cq]);
                r2.setText(aa1[li[cq]]);
                r2.setSelected(click2[cq]);
                r3.setText(aa1[li[cq]]);
                r3.setSelected(click3[cq]);
                r4.setText(aa1[li[cq]]);
                r4.setSelected(click4[cq]);
                ff.setVisible(false);
                launch1(cq);
                }
                else
                    if(e.getActionCommand().equalsIgnoreCase("submit"))
                    {
                        flag11++;
                        lis[cq].setEnabled(false);
                        click1[cq]=r1.isSelected();
                        click2[cq]=r2.isSelected();
                        click3[cq]=r3.isSelected();
                        click4[cq]=r4.isSelected();
                        if(aa1[li[cq]].charAt(0)=='#')
                        {
                            if(r1.isSelected())
                            {
                                correct++;
                            }
                            else
                            {
                                incorrect++;
                            }
                        }
                        else
                        {
                            if(aa2[li[cq]].charAt(0)=='#')
                            {
                                if(r2.isSelected())
                                {
                                    correct++;
                                }
                                else
                                {
                                    incorrect++;
                                }
                            }
                            else
                            {
                                 if(aa3[li[cq]].charAt(0)=='#')
                                 {
                                     if(r3.isSelected())
                                     {
                                        correct++;
                                     }
                                     else
                                     {
                                        incorrect++;
                                     }
                                 }
                                 else
                                 {
                                    if(r4.isSelected())
                                    {
                                        correct++;
                                    }
                                    else
                                    {
                                        incorrect++;
                                    }
                                 }
                            }
                        }
                        sub[cq]=0;
                        submit.setEnabled(false);
                        //ff.setVisible(false);
                        //launch(li,q,aa1,aa2,aa3,aa4,cq);
                        ff.setVisible(false);
                launch1(cq);
                    }
                    else
                        if(e.getActionCommand().equalsIgnoreCase("Finish"))
                        {
                            re();
                        }
                        else
                        {
                            click1[cq]=r1.isSelected();
                            click2[cq]=r2.isSelected();
                            click3[cq]=r3.isSelected();
                            click4[cq]=r4.isSelected();
                            cq=Integer.parseInt(e.getActionCommand());
                            ff.setVisible(false);
                            launch1(cq);
    
                        }
    
        }
    
        public boolean check()
        {
            int cp=0;
            for(int i=0;i<sub.length;i++)
            {
                if(sub[i]==0)
                {
                    cp++;
                }
            }
            if(cp==sub.length)
            {
                return true;
            }
            return false;
        }
    
        public void re()
        {
            ff.setVisible(false);
            int result=(correct*co)+(incorrect*inco);
            //String msg="Name:"+n+"\n"+"Phone:"+ph;
            //String sub=""+result;
            //sending ss=new sending(msg,sub);
            //ss.send();
            new Final(flag11,correct,incorrect,result);
        }
    
        class TextClock1
    {
        int ss;
    
        public TextClock1()
        {
            ss =60;
            javax.swing.Timer t = new javax.swing.Timer(1000, new ClockListener());
            t.start();
        }
        class ClockListener implements ActionListener
        {
        	public void actionPerformed(ActionEvent e)
            {
                Calendar now = Calendar.getInstance();
                int m = now.get(Calendar.MINUTE);
                
                ss--;
                if(ss<0)
                {
                    timee--;
                    ss=60;
                }
                if((timee==0)&&(ss==60))
                {
                    JOptionPane.showMessageDialog(null,"You Have Last 1 min left","Alert",JOptionPane.WARNING_MESSAGE);
                }
                if((timee==0)&&(ss==0))
                {
                    re();
                }
                String result;
                if(ss<10)
                {
                    if(timee<10)
                    {
                        result="0"+timee+":"+"0"+ss;
                    }
                    else
                    {
                        result=timee+":"+"0"+ss;
                    }
                }
                else
                {
                    if(timee<10)
                    {
                        result="0"+timee+":"+ss;
                    }
                    else
                    {
                        result=timee+":"+ss;
                    }
                }
    
                _timeField.setText(result);
        	}
        }
    
    }
    }
    help me please

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    Do you mean ArrayIndexOutOfBoundsException?

    If so the message will say on what line of your code the exception is occurring. It occurs whenever you try and read from or assign to an array at some index that doesn't exist either because the index is negative or because it is too large.

    It is quite difficult to read the code you posted. Try renaming variables and methods so that they are descriptive of what they are or do. Remove everything that is static with the single exception of the main() method that launches the application. For instance if you have arrays that are static then they can be accessed from all over the place and that's just an invitation for ArrayIndexOutOfBoundsException (and its friend NullPointerException) to come and play in your program.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    ++ on everything pbrockway2 stated above.

    Let me also add then when you see a class taking up several pages of your screen, you know it's time to refactor your code. I suggest that you try to split the "God-class" above into several smaller class, each with its own job to do. This will make it much easier for you and others to read and understand your code, to be able to extend your code, and mostly to be able to isolate any problems and debug your code. Much luck!

Similar Threads

  1. need urgent help!!
    By ras_pari in forum New To Java
    Replies: 3
    Last Post: 10-30-2009, 08:06 AM
  2. Urgent Help!!
    By Winniee in forum New To Java
    Replies: 5
    Last Post: 02-17-2009, 04:32 AM
  3. very urgent
    By nehaa in forum Networking
    Replies: 12
    Last Post: 01-23-2009, 06:46 PM
  4. Please Help!!!!Its Urgent
    By shru07 in forum AWT / Swing
    Replies: 1
    Last Post: 07-31-2008, 07:50 PM
  5. Help me ...urgent!
    By googgoo in forum New To Java
    Replies: 7
    Last Post: 04-05-2008, 09:46 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
  •