GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes Out of Bounds Exception returns  and the eradication thereof. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Out of Bounds Exception returns  and the eradication thereof." Watch "Out of Bounds Exception returns  and the eradication thereof." New topic
Author

Out of Bounds Exception returns and the eradication thereof.

Simon Evans
Ranch Hand

Joined: Mar 27, 2008
Posts: 93
Dear Campell Ritchie, Paul Yule and Michael Dunn
Thank you for your respective advice.
I amended code in accordance with Michael Dunn's advice, I now see that the JPanel creation should occur after the i++ incrementation - therefore in the outer loop - than before it - so
have changed the erroneous:-

to:-

and also declared int i globally, so it'd be accessible in outer loop.
However this still returns an array out of bounds exception (& crash) from compiler, this
time at line 34 that Campbell Ritchie said his version did, ie:


Looking at that line:-

In which I had wanted to get two things, a total amount of letters that were input that'd be returned by the values of i for each of the values stored in usersNames[i] hence in the example given Total would be 15 (3 j's times 5 i's or 3 Names * 5 usersNames[i].length()'s) and that 15 would then be used to create the necessary number of Jlabels after loop in the subsequent line:-

Going back to program Name Layout, using the same loop ie:-

will get :-
[CODE]
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\Documents and Settings\simon.BLUECHIP-KDOCDA>cd c:\backup\Project Tarot Two

C:\backup\Project Tarot Two>set PATH=c:\Sun\AppServer\jdk\bin;%PATH%

C:\backup\Project Tarot Two>javac NameLayout.java

C:\backup\Project Tarot Two>java NameLayout
LabelLabelLabelLabelLabel

LabelLabelLabelLabelLabel

LabelLabelLabelLabelLabel

[CODE]
This means that usersNames[j] got initialised to the length of the names input to the JOptionPane - and using the same syntax as I used in FT3's problem line 34, nonetheless returned/returns what usersNames[i] got initialised to - it works without an array oob exception there - so why doesn't it here in program FT3 ? - is my conundrum.
Not wishing to put the cart before the horse as it were, I will get on to changing the JOptionPane if and when this point is resolved.

Hope you can help,
Thank you for reading,
Yours
Simon.
[ October 11, 2008: Message edited by: Simon Evans ]

Simon
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
you've amended it to this (inner loop):



what are you trying to do?
you need to think about the loop - it's not that hard to do
3 names, Simon Angus Evans
there are only 3 elements in userNames, and the loop will continue until
i >= usersNames[i].length(), so 'i' will generally continue past the 3rd element

there is a simple fix, but the only way you'll understand it is to work it out for yourself.

every for loop you have (nested or not), just write down on paper what
the variable values should be at each loop, then add System.out.println's
to check they match your expectation - if not, find out why, fix, repeat etc
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39044
    
  23
Originally posted by Simon Evans:
line 34 that Campbell Ritchie said his version did, ie:


Please don't go quoting old posts all the time; your posts are getting longer and longer and harder to read, and we can remember what we said before.

I said before to look very carefully at that line and work out what length you are getting, and compare it to what length you want. Michael Dunn has told you something similar; if you print out the different lengths it will give you a hint. When you work out the hint, you will probably be able to get the app to work.

******************************************************************************

I think you are trying to run before you can walk. You are trying a large app without familiarity with the basics of the Java API. You need to look up different things called length. You would do well to re-read the Java Tutorials about arrays, and have a good look at the String class in the Java Tutorials and in the API.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39044
    
  23
I also think you need to read about arrays in the Java Language Specification; there are several links but that one will probably supply the information you require.
Simon Evans
Ranch Hand

Joined: Mar 27, 2008
Posts: 93
Dear Campbell Ritchie, Michael Dunn, Henry Wong, Paul Yule & other programmers

Thank you for your advice regarding debugging my program FT3.
I have got program FT3 to run to some degree, and it presents a gui of JLabels to JPanels or Rows according to the number of Names inputter gives, and a number of columns corresponding to the length of the respective names that are input.

The drawbacks are that firstly if I put in a name of two words or less it returns an array out of bounds exception, and secondly that if I input irregularly lengthed names, the action listeners don't seem to work upon all of the Jlabels that it generates.

If I try a regularly lengthed name - such as my own, being 3 rows by 5 columns, all action listeners seem to work, but if I input say my sister's name:- 'Jane Alison Rebecca Evans' then most of the first name's and some of the second and third name's action listeners don't work.

I have used the same looping code for the creation of the Action Listeners as I have for the creation of the Jlabels, so I was wondering what could be wrong, as using the same loop in program FT2 gets all the labels action listeners to work alright.

I will try to get them to work in the meantime, but I would be glad if you could give me some idea what is going amiss with the program.
I know I have been told to keep the length of posts down, but I am pretty sure this thread is near an end - so I am posting the entire program as it is.

Yours, hoping to hear,

Simon
(code follows:-)



[ October 15, 2008: Message edited by: Simon Evans ]
[ October 15, 2008: Message edited by: Campbell Ritchie ]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39044
    
  23
You still have the problem about which length you are looking for. Have you worked out which length you want and which length you are getting in your for loops? That was the problem before and I am not sure you have sorted that out.

And I shall change your sister's name in your post; I am not convinced it is a good idea to post a third party's full details where anybody might be able to read them.
[ October 15, 2008: Message edited by: Campbell Ritchie ]
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
let's say you have 3 names (irregular lengths)
Simon Joe Simpson

what would you expect 'Total' to be when it finishes the for loops?


run the program, does it match what prints?
System.out.println("This is Total:- "+ Total);
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39044
    
  23
What's "Names"; if it isn't exactly the right number the result is an ArrayIndexOutOfBoundsException.

Arrays take care of themselves, if you use them properly. Using that sort of variable (which somebody else has already told you is named incorrectly with a capital N) isn't using arrays correctly. Back to the Java Tutorials links I gave earlier.

And lose the JOptionPane asking how many names you have got. You want a single name entry with "Please enter full name." You write "Simon Angus Evans" and there are methods which will convert that to an array and work out automatically that it has 3 words in.
Simon Evans
Ranch Hand

Joined: Mar 27, 2008
Posts: 93
Dear Campbell Ritchie, Michael Dunn, Henry Wong, Paul Yule & other programmers,

Thank you for your advice regarding the alteration of my program FT3, which I have used to create FT4, as now it only uses one JOptionpane, and the split string method, and the recommended name for the numofNames array as also recommended.

I have changed the Total initialsement, so that it obtains the amount of letters inputter gives.

The program compiles and runs alright, but I still am getting the problem with not all of the ActionListeners functioning upon irregular names - such as 'Jane Alison Rebecca Evans' f'rinstance.

I have made the variable for the amount of names returned from the split string the array numofNames.

I was wondering - having in a sense eliminated those causes, why all the ActionListeners are still not functioning as hoped?

Yours, hoping to hear,
Simon (code follows-)

[ October 17, 2008: Message edited by: Simon Evans ]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39044
    
  23
What ActionListener? You only seem to have a MouseListener with an endearing name. Look very closely where you are adding it, and work out how many of your picture JLabels you have added it to.

I presume you have got rid of the out of bounds Exceptions?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39044
    
  23
I haven't worked out where you are adding the "mousie" but I suggest you go through those for loops with a pencil and paper and a nice short name like "Simon Evans" and work out whether there is any possibility of your adding the MouseListener beyond the end of the array. At least with "Simon Evans" you only have to do the arithmetic 10 times!
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
this bit is wrong


write down a name - determine the various lengths that will be used in the above,
and the number of labels that should be generated, which should equal 'Total'
now write down *exactly* what label is being created in each loop
i.e. what number is this going to produce
[j + (i*numofNames[i].length())]
don't stop until you've finished both for loops, then have a look at what it should be
Simon Evans
Ranch Hand

Joined: Mar 27, 2008
Posts: 93
Dear Campbell Ritchie, and Michael Dunn, and other programmers.

Thank you for your advice.
I have seen the error in the inner loop, and had reckoned on a way to fix it.
Looking for example at the first name I'd give it:-
SIMON

In such a loop would register as:-

i j
0, 1 = (0 + 1*5) = 5
0, 2 = (0 + 2*5) = 10
0, 3 = (0 + 3*5) = 15
0, 4 = (0 + 4*5) = 20
0, 5 = (0 + 5*5) = 25

but if

were instead :

it'd read:

i j
0, 1 = (1 + 5*0) = 1
0, 2 = (2 + 5*0) = 2
0, 3 = (3 + 5*0) = 3
0, 4 = (4 + 5*0) = 4
0, 5 = (5 + 5*0) = 5

then (for ANGUS EVANS ) :-

i j
1, 1 = (1 + 5*1) = 6
1, 2 = (2 + 5*1) = 7
1, 3 = (3 + 5*1) = 8
1, 4 = (4 + 5*1) = 9
1, 5 = (5 + 5*1) = 10

and:-

i j
2 1 = (1 + 5*2) = 11
2 2 = (2 + 5*2) = 12
2 3 = (3 + 5*2) = 13
2 4 = (4 + 5*2) = 14
2 5 = (5 + 5*2) = 15

So that would seem to be the appropriate code.
I noticed that I'd mistranslated one line from program FT3 that read:-

to

whereas it should've read:


I would've liked to see the program now run alright, but now it returns an array exception out of bounds exception, which reads on the compiler as such:-

Line 52 being the one that I have just ammended, ie:

I was wondering where I have gone wrong in my alteration of this part of the code?
I am sorry its dragging on, I'd hoped this last alteration would've fixed things.
I hope you can help.
Thank you for reading.
Yours
Simon
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39044
    
  23
We're making progress at last.

How many arrays have you got? If you are trying to put 5 pictures into an array to match 5 letters in Simon, which array indices do you need?

Try a tiny application which simply puts 5 pictures (on JLabels?) onto a PFrame. It is much much easier to sort problems out if you break the problme into little bits. When you have got the length length() length * i bits all sorted out, you will have a good idea what to do with the bigger application.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> label[j + (i*numofNames[i].length())] = new JLabel(images[0]);
> ...
> Looking for example at the first name I'd give it:- SIMON
> ...
> then (for ANGUS EVANS ) :-

repeat the exercise for SIMON JOE SIMPSON
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
it's probably going to be a lot easier for you if you changed it
to how (I think) you may have been trying to do it earlier

repeat for the mouseListener (which you could be including in the above)
Simon Evans
Ranch Hand

Joined: Mar 27, 2008
Posts: 93
Dear Campbell Ritchie, and particularly Michael Dunn,
Thank you for putting me out of my misery.
My loops are a bit shaky, so in a perverse, twisted sort of way, I thought
I'd create a program that'd rely heavily on this subject.
These are the bits I recoded, though you probably know what they look

like:-

and


Anyhow, thank you for your expert advice.

Hoping to talk again soon.

Yours

Simon.

[ October 20, 2008: Message edited by: Simon Evans ]
[ October 21, 2008: Message edited by: Simon Evans ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Out of Bounds Exception returns and the eradication thereof.