Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Java ArrayList or for loop problem.

  1. #1
    Junior Member
    Join Date
    Apr 2014
    Posts
    22
    My Mood
    Lonely
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Java ArrayList or for loop problem.

    So I have been busy at some projects lately. One of them having this project
    ill put up the full code and the part i have problems with:

    package relax;
     
    //importeverything
    import java.awt.AWTException;
    import java.awt.Robot;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyEvent;
    import java.util.ArrayList;
     
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
     
    public class Relax {
     
    	//declare some variables for later use.
    	private static boolean Setup = false;
    	private static Robot r = null;
    	private static JFrame f;
    	private static JPanel p;
    	private static JButton b1, b2, b3, b4;
    	private static JLabel l1, l2;
    	private static int Clicks;
    	private static boolean Stop = false;
     
    	//makes a robot
    	private static void MakeRobot()
    	{
     
     
    		try {
    			r = new Robot();
    		} catch (AWTException e) {
    			//if it fails this will happen
    			e.printStackTrace();
    		}
     
    	}	
     
     
     
     
     
    	public static void main(String[] args) 
    	{
    		//initialize everything
    		MakeRobot();
    		MakeFrame();
    		AddActionListeners();
    	}
     
     
    	//adds all the actionlisteners to be added
    	public static void AddActionListeners() {
     
    		b1.addActionListener(new ActionListener(){
     
     
    			public void actionPerformed(ActionEvent e) {
     
     
    				if (Setup) {
    					InitializeBot();
    				}
     
     
    			}
     
    		});
     
    		b2.addActionListener(new ActionListener(){
     
     
    			public void actionPerformed(ActionEvent e) {
    				StartSetup();
     
    			}
     
    		});
     
    		b3.addActionListener(new ActionListener(){
     
     
    			public void actionPerformed(ActionEvent e) {
    				f.dispose();
     
    			}
     
    		});
     
    		b4.addActionListener(new ActionListener(){
     
     
    			public void actionPerformed(ActionEvent e) {
    				Stop = true;
     
    			}
     
    		});
     
     
     
    	}
    	//initialize the bot to click for you
    	public static void InitializeBot() 
    	{
     
    		JOptionPane.showMessageDialog(null, "Enter osu! choose your beatmap and dont start (long intro needed)");	
    		r.delay(10000);
    		r.keyPress(KeyEvent.VK_ENTER);
    		r.delay(10);
    		r.keyRelease(KeyEvent.VK_ENTER);
    		r.delay(4990);
    		r.keyPress(KeyEvent.VK_SPACE);
    		r.delay(10);
    		r.keyRelease(KeyEvent.VK_SPACE);
    		r.delay(3990);
    		int Progress = 0;
     
    		//start the actuall bot
    		while (Stop == false)
    		{
     
    			r.keyPress(KeyEvent.VK_Z);
     
     
    		}
     
    	}
     
     
     
     
    	//makes the frame that we will be using
    	private static void MakeFrame() 
    	{
    		f = new JFrame("Osu!relax");
    		p = new JPanel();
    		b1 = new JButton("Start");
    		b2 = new JButton("Setup");
    		b3 = new JButton("Exit");
    		b4 = new JButton("Stop");
    		l1 = new JLabel("Current Map: ");
    		l2 = new JLabel("None");
     
    		p.add(l1);
    		p.add(l2);
    		p.add(b1);
    		p.add(b4);
    		p.add(b2);
    		p.add(b3);
     
     
    		f.add(p);
    		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		f.setVisible(true);
    		f.setSize(500, 500);
     
     
    	}
     
    	//asks the user some information to start the bot
    	public static void StartSetup() 
    	{
    		Setup = true;
    		l2.setText(JOptionPane.showInputDialog(null, "Song+Difficulty"));
    		Clicks = Integer.parseInt(JOptionPane.showInputDialog(null, "How Much Clicks?"));
    		int TimingPoints = 0;
    		for (int[] Timing = new int[Clicks];TimingPoints != Clicks; Clicks--)
    		{
    			//---> this shows 1 more timing point JOptionPanes instead of the CLICKS amount I want.. then it gives an error. <---------
    			Timing[TimingPoints] = (Integer.parseInt(JOptionPane.showInputDialog(null, "what is timing point ")));
    			TimingPoints++;
    		}
     
     
     
    	}
     
     
     
    }

    And here the part i have problems with:

    //asks the user some information to start the bot
    	public static void StartSetup() 
    	{
    		Setup = true;
    		l2.setText(JOptionPane.showInputDialog(null, "Song+Difficulty"));
    		Clicks = Integer.parseInt(JOptionPane.showInputDialog(null, "How Much Clicks?"));
    		int TimingPoints = 0;
    		for (int[] Timing = new int[Clicks];TimingPoints != Clicks; Clicks--)
    		{
    			//---> this shows 1 more timing point JOptionPanes instead of the CLICKS amount I want.. then it gives an error. <---------
    			Timing[TimingPoints] = (Integer.parseInt(JOptionPane.showInputDialog(null, "what is timing point ")));
    			TimingPoints++;
    		}
     
     
     
    	}
    There is some kind of issue here.
    in the gui it will ask you to give points in time (ms) it wants to click for you, although when for example i put in 3 at "How Much Clicks" it asks 4 times and then exits with an error.
    Here is the error:
    Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 3
    at relax.Relax.StartSetup(Relax.java:175)
    at relax.Relax$2.actionPerformed(Relax.java:78)
    at javax.swing.AbstractButton.fireActionPerformed(Unk nown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed (Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed (Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent( Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(U nknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unkno wn Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$200(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(Unknown Source)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(U nknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

    im probably just dumb like usual but i really dont get this problem :/

    Hope somebody has the time to help!

    Greetings,
    Niels
    Last edited by Niels van Ee; August 10th, 2014 at 02:27 PM.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Java ArrayList or for loop problem.

    Here's the important part of the error/stack trace:

    Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 3
    at relax.Relax.StartSetup(Relax.java:175)

    Find the referenced line and determine why the array index is allowed to go to 3 which is out of bounds. Typically, this is due to a loop that limits the loop variable to <= array.length rather than < array.length or a loop in which the loop variable is incremented in the loop body. If neither of these typical mistakes apply in your case, then please post line 175 and the entire clause in which it exists.

  3. #3
    Junior Member
    Join Date
    Apr 2014
    Posts
    22
    My Mood
    Lonely
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: Java ArrayList or for loop problem.

    Thanks i got it to work! it was that i used the variable 2 times which made it go on forever (the for loop) until the array stops.

    Thanks

Similar Threads

  1. [SOLVED] error with equals in arraylist loop
    By Randor in forum Loops & Control Statements
    Replies: 2
    Last Post: November 27th, 2012, 10:46 AM
  2. java programming loop problem
    By darking123 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 11th, 2012, 05:48 PM
  3. Need help with ArrayList loop
    By mattslagle44 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: June 7th, 2012, 12:31 PM
  4. Perfect Number Java Loop Problem - Please Help!?
    By Th3T3chGuy in forum Loops & Control Statements
    Replies: 2
    Last Post: November 13th, 2011, 06:09 PM
  5. Java Do..while loop problem
    By jwill22 in forum Loops & Control Statements
    Replies: 4
    Last Post: November 13th, 2010, 04:02 AM

Tags for this Thread