473,419 Members | 1,544 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,419 software developers and data experts.

how do I fix a "string index out of bounds" error

What am I doing wrong. I have been looking over this code for hours and Im sure its a simple fix.
Expand|Select|Wrap|Line Numbers
  1. import java.util.Scanner;
  2.  
  3. public class TremorinKeithA2Q1 {
  4.  
  5. public static void main(String[] args) {
  6.  
  7.   Scanner kbd = new Scanner(System.in);
  8.  
  9.   String esparanto;
  10.   int len =0;
  11.   char lastLetter;
  12.     System.out.println("Please enter a word in Esperanto");
  13.     esparanto = kbd.next();
  14.  
  15.     while (!esparanto.equals("cesi")) {
  16.     len = esparanto.length();
  17.     lastLetter =  esparanto.charAt(len);
  18.     System.out.println(lastLetter);
  19.  
  20.     if(lastLetter == 'a') {
  21.         System.out.println("That number is an adjective");
  22.     }if(lastLetter == 'e'){
  23.         System.out.println("That number is an adverb");
  24.     }
  25.  
  26.  
  27.  
  28.  
  29.  
  30. }
  31. }
  32. }
  33.  
I can compile it but when I run it I get:

Please enter a word in Esperanto
konkopa
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String ind
ex out of range: 7
at java.lang.String.charAt(String.java:686)
at TremorinKeithA2Q1.main(TremorinKeithA2Q1.java:17)
Press any key to continue . . .
Oct 19 '10 #1
1 10953
Nepomuk
3,112 Expert 2GB
The problem is with the line
Expand|Select|Wrap|Line Numbers
  1. lastLetter =  esparanto.charAt(len);
The characters are numbered from 0 to len - 1, so if you change it to
Expand|Select|Wrap|Line Numbers
  1. lastLetter =  esparanto.charAt(len - 1);
it should work as intended.

Greetings,
Nepomuk
Oct 19 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Klaus Petersen | last post by:
Hi ng. I have a varchar field in my table, called Name. I wanna do a selection, which is ordered by whether this field is empty or not. E.g. something like: SELECT
7
by: William Payne | last post by:
Hello, have you seen a recent files menu in a GUI application? In many GUI applications there's a menu the displays the most recent files that has been opened by the program. Say such a menu has...
2
by: srktnc | last post by:
When I run the program, I get a Debug Error saying "This application has requested the Runtime to terminate it in an unusual way. Please contact the application's support team for more...
388
by: maniac | last post by:
Hey guys, I'm new here, just a simple question. I'm learning to Program in C, and I was recommended a book called, "Mastering C Pointers", just asking if any of you have read it, and if it's...
7
by: Dan H. | last post by:
Hello, What is the equivalent "string resource file" in c#? And, is there a way to create a beep in C#? Thanks, Dan
9
by: Jeff Jarrell | last post by:
for some reason I can get a string.index of to recognize a substring like "%s". dim s as string dim i as integer s = "hello %s" i = s.indexof(s) '--- i is zero
4
by: Newsgroups | last post by:
Does anyone know what the VB.NET equivalent to the VB 6.0 String Function is?
6
by: nrperry | last post by:
Hello, I have a question about this error: Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -2 I am doing my java development in IBM Rationale...
2
by: babakandme | last post by:
Hi to every body...:D I'm a novice C++ programmer & I've a question, How can I write a class as follow to a file... Class A { public: // Accessors Methods...
0
by: Knightxtech | last post by:
I keep getting an out of bounds error message when I have selected the last record on the datagrid (by code) then try and move up in the datagrid using the scroll bar. I dont have any code to...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.