It looks like you indented so_far = new too much. Try this:
if guess in word:
print("\nYes!", guess, "is in the word!")
# Create a new variable (so_far) to contain the guess
new = ""
i = 0
for i in range(len(word)):
if guess == word[i]:
new += guess
else:
new += so_far[i]
so_far = new # unindented this
Answer from Rob Wouters on Stack OverflowPython error: "IndexError: string index out of range" - Stack Overflow
How To Fix String Index Out of Range in Python - Stack Overflow
java - How to fix String index out of range: -1 - Stack Overflow
How to fix string index out of range errror
Videos
I'm writing a program that needs to iterate through a string and print the first letter of every word. I have the program working but I can't submit it because of a "String index out of range" error.
An example of what needs to happen is this:
Please type in a sentence: Humpty Dumpty sat on a wall
Output:
H
D
s
o
a
w
Here's my code:
sent = input("Please type in a sentence: ")
index = 0
sent_length = len(sent)
space = " "
print(sent[index])
while index <= sent_length:
index += 1
if sent[index] == space:
print(sent[index + 1])I'm getting the correct output but it says I have a "string index out of range" error on line 8. I can't figure out what's wrong with it
It looks like you indented so_far = new too much. Try this:
if guess in word:
print("\nYes!", guess, "is in the word!")
# Create a new variable (so_far) to contain the guess
new = ""
i = 0
for i in range(len(word)):
if guess == word[i]:
new += guess
else:
new += so_far[i]
so_far = new # unindented this
You are iterating over one string (word), but then using the index into that to look up a character in so_far. There is no guarantee that these two strings have the same length.
Index out of range exception value of -1 means the requested symbol, in this case, S, is not found.
You take phrase.indexOf('S') on a string without checking the return value. If there is no match, the method returns -1. You then use this index as the upper bound of a substring, which crashes the program.
You would want a different algorithm even if you got it correct, if I understand correctly what you want to do. There is no reason both to search the string for each occurrence of the character you want and also to write a loop decrementing the length by 1. Also, avoid copying long arrays and strings if possible.
It is a pity that substring is not implemented in a way that handles short strings โ like in other languages e.g. Python.
Ok, we cannot change that and have to consider this edge case every time we use substr, instead of if-else clauses I would go for this shorter variant:
myText.substring(0, Math.min(6, myText.length()))
I"m guessing i'm getting this error because the string is trying to substring a Null value. But wouldn't the ".length() > 0" part eliminate that issue?
No, calling itemdescription.length() when itemdescription is null would not generate a StringIndexOutOfBoundsException, but rather a NullPointerException since you would essentially be trying to call a method on null.
As others have indicated, StringIndexOutOfBoundsException indicates that itemdescription is not at least 38 characters long. You probably want to handle both conditions (I assuming you want to truncate):
final String value;
if (itemdescription == null || itemdescription.length() <= 0) {
value = "_";
} else if (itemdescription.length() <= 38) {
value = itemdescription;
} else {
value = itemdescription.substring(0, 38);
}
pstmt2.setString(3, value);
Might be a good place for a utility function if you do that a lot...
int i = s1.charAt(s1.length()-1);
This code stores the ASCII code of the character at the index : - s1.length() - 1, that can certainly be greater than the maximum accessible string index.
For e.g, the last character in your current string is 8, whose ASCII code is: - 56, and that would certainly fail.
So, s1.substring(i, i+1) in your if condition after that would fail.
In fact, I don't understand the need of that line at all. Why are you using it?
Also, your if-else block seems buggy to me: -
if (s1.charAt(1) != '-')
{
i = s1.length();
}
else if (s1.charAt(11) != '-')
{
i = s1.length();
}
Why have you assigned same value to i in both the blocks there?
May be you wanted something like this: -
if (s1.charAt(1) != '-' || s1.charAt(11) != '-')
{
break; // Just break if not a valid string
}
You are starting your for loop at zero index.
for (int i = 0; i < s1.length()-1; i++)
Yet your substring goes beyond i in the following code,
.substring(i, i+1).
On an optimizing note, you may use regexp to check for the hyphen at index 1 and 11.
Hello!
I can not figure out why it is coming up as out string out of range. I would love a second pair of eyes.
String numbers = scanObj.nextLine();
// make sure to only use numbers in case user input letters.
int i=0;
numbers.length();
char x =numbers.charAt(i);
if ((x >= 48 && x<=57))
// convert in to int
for (i = numbers.length(); i !=0; --i)
{
int numbers1 = numbers.charAt(i)*10^i;
{
System.out.println(numbers1);
}
}