This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of Darcy DeClute's Scrum Master Certification Guide: The Definitive Resource for Passing the CSM and PSM Exams and have Darcy DeClute on-line!
See this thread for details.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

How Do I Correct a java.lang.StringIndexOutOfBoundsException: String Index Out of Range 0

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Everyone

I am new to Code Ranch and I am having trouble with a program I am coding. The program is suppose to accept the user's full name and street address using JOptionPane class and construct an ID that consists of the user's initials and numeric portion of their street address. For example, John Bon Lewis and 3245 Maple Street should result in JBM3245 or Ray Simpleton, 306 Foley Road should yield RS306. My program runs smoothly when a three part name is entered, but when a two part name is entered I get a StringIndexOutOfBounds Exception. I think it comes from using substring to extract a word that is not there. I have changed my code around multiple times trying to "fix" it but nothing I change it to works. Could anyone help me out? My code is listed below.


 
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch!

Well, that is too much code. Please tell us which line throws this exception.
Can you post a stack trace?
 
Marshal
Posts: 78698
374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paweł Baczyński wrote:. . .
Well, that is too much code.
. . .

Also too much code for the main method. All that code should be in other methods. Use the split method of the String class to separate the input.

And welcome again
 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch!

The error occurs on line 108:

The problem is that middleName can be empty. Add an if statement around this line to avoid the OOB exception.
 
Marshal
Posts: 8831
631
Mac OS X VI Editor BSD Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Few tinny things from me.

1. I'd change "while" loops to "for". Why? Because for the loop control you're using the same variable "x" in three different loops and after each loop you reset to 0. It is error-prone.
With "for" loop you would eliminate that possible pitfall. As control variable would be unique to each "for" loop.

2. Also, don't write such long comments as you do have on lines 9 and 10 - program becomes more difficult to read.

3. import javax.swing.*; Use explicit imports by specifying exact classes you're going to use, that would improve code readability.

4. Variables initial1, initial2, initial3 in my opinion is a poor choice. Why not to call nameInitial, middleNameInitial, surnameInitial - it is clearer, right?
It is better to have long variable name, than uninformative. Worse than uninformative is only disinformative. These should be avoided at all.
Such example could be a comment on line 121, which sounds "//Adds number to the ID." That is not true, it does something different the line below.
Such example is considered as uninformative "//Prompts user for their street address." It adds the noise to the whole code.

Further on this, look at the code lines:


Second and third lines are disinformative, as these are never used in your code at all, because you're using only x for the loops control (just triple checked, can't find y and z used).
 
Lenise Edwards
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you all for the constructive criticisms, I will try to incorporate these tips in my future assignments. A little background info I probably should have included. My undergrad was accounting and I am now pursuing my masters in computer science. I am only one class into my major right now (currently Java Programming Level 1), so I am unfamiliar with some of the terminologies used.

Campbell Ritchie Wrote:

Also too much code for the main method. All that code should be in other methods. Use the split method of the String class to separate the input.



When you say its too much code in the main method are you suggesting to create a class and create all the methods within that class so that all I have to do is call the methods in the main method? I was following the format the textbook used. So far, the textbook has been placing majority of the code in the main method, but if this isn't efficient I can change that. Also I haven't covered anything on the split method yet but I can try and see if I can figure out how to use it from java API. I do find the API a bit difficult to learn from as I have tried to use it before to learn more about other classes.

Paweł Baczyński wrote:

Please tell us which line throws this exception.
Can you post a stack trace?



For my Java class we are using JGrasp IDE and it didn't pinpoint the exact line that is causing the exception. Also, what is a stack trace and how do I perform one?

Liutauras Vilda: Thank you for your input the unused variables were left there just in case I wanted to use them. I had planned on deleting them once I finished the program. Your other tips I will definitely keep, I didn't realize I was using poor commenting and I will use this information to help improve my programs.

Thank you all for taking the time to help
 
Paweł Baczyński
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Lenise Edwards wrote:For my Java class we are using JGrasp IDE and it didn't pinpoint the exact line that is causing the exception. Also, what is a stack trace and how do I perform one?


When there is an uncaught exception, a stack trace is printed to the console. A stack trace shows you what method were called at which lines and wchich line caused the exception.

For example, running this code:
printsHere, a static method main() called a constructor (listed as <init>) at line 17, the constructor called foo() at line 5, foo() called baz() at line 8, baz() called bar() at line 11 and finally bar() threw an exception at line 14.
 
Campbell Ritchie
Marshal
Posts: 78698
374
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Lenise Edwards wrote:Thank you all for the constructive criticisms . . .
When you say its too much code in the main method are you suggesting to create a class and create all the methods

Yes. Or more likely several classes

. . . I was following the format the textbook used.

Which book?

. . . Also I haven't covered anything on the split method yet but I can try and see if I can figure out how to use it from java API. I do find the API a bit difficult to learn

I am afraid it is not easy to read. It takes a lot of practice. Look here. Try "\\s+" which means any number greater than 0 of whitespace characters, as the “regex”.

. . . Also, what is a stack trace and how do I perform one? . . .

Have you suffered an Exception in your program? If so, you will have seen a stack trace. If not, try this program:-Once you get that running, you will see a stack trace caused by division by zero. I think the first line with the name of the Exception may not constitute part of the stack trace.
 
Lenise Edwards
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paweł Baczyński wrote:

Lenise Edwards wrote:For my Java class we are using JGrasp IDE and it didn't pinpoint the exact line that is causing the exception. Also, what is a stack trace and how do I perform one?


When there is an uncaught exception, a stack trace is printed to the console. A stack trace shows you what method were called at which lines and wchich line caused the exception.

For example, running this code:
printsHere, a static method main() called a constructor (listed as <init>) at line 17, the constructor called foo() at line 5, foo() called baz() at line 8, baz() called bar() at line 11 and finally bar() threw an exception at line 14.



Ah, thanks I didn't know that lol. Here's the stack trace:

at java.lang.String.charAt(String.java:646)
at ConstructID.main(ConstructID.java:108)
 
Lenise Edwards
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote: .

Have you suffered an Exception in your program?

Yes the program I am inquiring about in this post is suffering from an exception. Here's the stack trace:



the full print is:



The textbook we are using in the class is Java Programming, 7th Edition, Joyce Farrell. I have read about methods, objects, and classes. In the book most of the programs are displayed in the main method, it's probably for easier display since I'm learning from you all that this isn't the best way. I have made it to Chapter 8 on arrays so far. I do like that the books tries to help guide you to the Java API and guides you through finding classes, but I still struggle at digesting and applying the information. I really want to become a great programmer though. This is difficult but I am determined to master this. I have a deep interest in programming and would like to eventually work my up to becoming a software developer/engineer. Baby steps
 
Lenise Edwards
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just wanted to say thank you to everyone and update. I figured out how to use the split method and redid the program. It runs perfectly now
 
Campbell Ritchie
Marshal
Posts: 78698
374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well done
 
Why is the word "abbreviation" so long? And this ad is so short?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic