Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

IllegalArgumentExcetion fix

801485Jan 23 2009 — edited Jan 23 2009
I am trying to make a hangman game and the game was two JTextFields. The first field contains the sentence that the person is trying to guess and the secound JTextField is always being compared to the first field to see if the person got the sentence right. Well I have 26 Jbuttons (a-z) and each checks the first field for the specific letter and the specific position of the letter (like where the caret is). Well for the most part it works but say lets say that the sentence (the first textfield) says "A monkey with a cane". Then you click the Jbutton that is labled A. Well this is the code to see wither "A" or "a" is in the first field:
	public void runtesta(){
		if(ranonce == false){
			times = 0;
			ranonce = true;
		}
		if(HangMan.words.getText().charAt(times) == 'A'||HangMan.words.getText().charAt(times) == 'a'){
			HangMan.compare.setCaretPosition(times);
			try {
				HangMan.compare.setText(HangMan.compare.getText(0, times) + "a");
			} catch (BadLocationException e) {
				e.printStackTrace();
			}
			times++;
		}else{
			times++;
		}
		if(times == HangMan.words.getText().length()){
			System.out.println(HangMan.compare.getText());
			ranonce = false;
		}else{
			runtesta();
		}	
	}
The code does find the first A but then when it gets to the second A it fails. I know why it fails and it is because the caret moves to where the second a is but the code fails because there is nothing inbetween the first a and the 2nd a and it causes it to fail. Here is the fail stuff:
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: bad position: 5
	at javax.swing.text.JTextComponent.setCaretPosition(JTextComponent.java:1650)
	at TestInSentence.runtesta(TestInSentence.java:257)
	at TestInSentence.runtesta(TestInSentence.java:271)
	at TestInSentence.runtesta(TestInSentence.java:271)
	at TestInSentence.runtesta(TestInSentence.java:271)
	at TestInSentence.runtesta(TestInSentence.java:271)
	at TestInSentence.runtesta(TestInSentence.java:271)
	at TestInSentence.actionPerformed(TestInSentence.java:18)
	at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
	at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
	at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
	at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
	at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
	at java.awt.Component.processMouseEvent(Component.java:6134)
	at javax.swing.JComponent.processMouseEvent(JComponent.java:3265)
	at java.awt.Component.processEvent(Component.java:5899)
	at java.awt.Container.processEvent(Container.java:2023)
	at java.awt.Component.dispatchEventImpl(Component.java:4501)
	at java.awt.Container.dispatchEventImpl(Container.java:2081)
	at java.awt.Component.dispatchEvent(Component.java:4331)
	at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4301)
	at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3965)
	at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3895)
	at java.awt.Container.dispatchEventImpl(Container.java:2067)
	at java.awt.Window.dispatchEventImpl(Window.java:2458)
	at java.awt.Component.dispatchEvent(Component.java:4331)
	at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
	at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
What I want to know is there anyway around this to make it work or is there something special I need to do or whatever?
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 20 2009
Added on Jan 23 2009
4 comments
214 views