This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
Hi out there, Okay I'm slowly figuring things out, my biggest problem was the "isUpperCase()" method, and I don't know if I've fully fixed it. My program does compile now but when I enter a string into the command line for example NoOn, I expect the output to be: "N appears 1 time" "O appears 1 time" But instead I get the following error as an output:
Exception in thread "main" java.main.ArrayIndex.OutOfBoundsException: -19 at Exercise7_7.countLetters (Exercise7_7.java:123) at Exercise7_7.main (Exercise7_7.java:102) My updated code is as follows:
If anyone can give me some insite as to what the problem is, I would appreciate it. Thank you in advance Stacey
Hi Stacey, well, 'A' i at position 65, whereas 'a' is at postion 97. So if you run your programm with the Parameter NoOn, the following happened at the line
word.charAt(i) returns 78, the position of 'N'. 78-97=-19 and that causes the Exception, because negative array indices are not allowed. So, you have to replace the 'a' with and 'A', further the curly brace for the if-statement should go 2 lines below, that this works correctly. that's all. Nils
Hi Stacey, A couple of things I've noticed. First off, take a look at this if statement inside your countLetters method:
If you fix your indentation to line up nicely, you will probably see it better:
What code is the if statement ececuting right now? Is that what you want to happen? I think probably not Using proper indentation will helps you (and any programmer) a lot in spotting these type of errors. Take a look at that a minute, and I have some other comments for you that I'll post in a few moments...
Stacey, Something that will help you as you progress in your studies is to understand what an Exception means so you know what to look at in your code. It will also tell you (basically) where to look for the problem in your code. A java.main.ArrayIndex.OutOfBoundsException says that your code is trying to access an array element at an index, but that index is "out of bounds" � that is it's either too small (i.e. less than 0 since arrays start at index 0) or too large (i.e. greater than array.length � 1 since arrays are 0 based the largest index value is one less than its length) Therefore, if I have an array with three elements at indexes 0, 1, and 2; the bounds of my array are 0 and 2. So if my code ever tries to access an element outside those bounds (like array or array or array[-1]) that piece of code will throw an OutOfBoundsException. The message will tell you what the index value was that you tried to access: Exception in thread "main" java.main.ArrayIndex.OutOfBoundsException: -19 The "-19" tells us that your code tried to access an index of -19 in an array. The next lines are what is called a Stack Trace � these lines trace through the Stack of the JVM to show where the error occurred. In this case, it is a nice short Stack Trace: at Exercise7_7.countLetters (Exercise7_7.java:123) at Exercise7_7.main (Exercise7_7.java:102) It tells us that in the Class Excersice7_7, the countLetters method threw the exception. In particular the code on line 123 caused the exception. The countLetters method was called by line 102 of the main method of the Exercise7_7. Using a Stack Trace can help you trace through your code to figure out where the error occurred. The problem will not always be right at the line of code that the Stack Trace lists. The problem could be in previous code that sets a variable or a condition that manifests itself latter and causes the exception. For example, in this code:
So this tells me that line 5 of the main method of the FindIt class called The doSomethingMethod, and line 16 of the doSomething method tried to access index 10000 of an array, but that 10000 was outside the array's bounds. So line 16 of my code is causing the exception to be thrown, but the real "problem" is that the someMethodThatReturnsAnInt() method is returning a value that is unrealistic for my array. Therefore, I either need to fix that method, or catch and handle the exception in my doSomething method (I am not sure if you have gotten to Exception Handling and try/catch blocks yet in your studies, so I will not mention anything further at this point.) I hope that all that will help you learn how to track down the problems in your code. Nils pointed out the specific issue as to why you are getting the exception with your code, but I think it will help you find problems if you understand how to read Exception messages. You also have one other problem with your code. See my next post... [ February 14, 2004: Message edited by: Mark Vender ]
Ok, one last problem your program... Try running your program without a word at the command line. In other words, try running it as: java Exercise7_7 You�ll notice you are getting another IndexOutOfBounds exception thrown. See if you can figure out why. I�ll give you a big hint You do not need to add or remove any code to fix it. In fact, previous versions of your program did not have this issue. Just think about what your code is doing and when it is doing it. See if you can figure it out... If not, reply to this post and I or someone else can help you find it.
Joined: Jan 11, 2004
Thanks you all so much for your help. As special thanks to Mark for not giving me the answer right out but making me think about it. I appreciate it. This beginner Java course is alot of work but I'm slowly catching on. This is a great sight for help. I don't know what I would of done if I didn't know about it. Again thanks so much for your help, it's kept me sane!! I'm going to switch hear for a few hours and turn to my operating systems course. Stacey
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com