Java方法solve执行报错java.lang.IndexOutOfBoundsException求排查
Hey there! Let's break down why your code is throwing that java.lang.IndexOutOfBoundsException and fix it to meet your goal of finding the largest number in a mixed letter-digit string.
First, the root cause of the IndexOutOfBoundsException
Your second loop has two critical issues:
- Wrong loop condition: You're using
strArr.length(a fixed value) as the loop limit, butarrgets smaller every time you callremove(). Onceiexceeds the current size ofarr, callingarr.remove(i)will trigger the index out-of-bounds error. - Broken check logic:
Character.isLetter(i)is checking if the loop index (an integer like 0,1,2...) is a letter—this makes no sense! You meant to check characters in the string, not the loop counter.
Beyond the exception: Your core logic is misaligned with the goal
Using s.split("\"") to split the string by double quotes has nothing to do with extracting digits from a mixed string. If your input has no quotes, this just gives you the original string as a single element, which doesn't help you separate numbers from letters at all.
Fixed code that works as intended
Let's rewrite the logic to properly extract all continuous digit sequences, convert them to integers, and find the maximum:
import java.util.ArrayList; import java.util.Collections; public class DigitExtractor { public ArrayList<Integer> getIntArray(ArrayList<String> stringArray) { ArrayList<Integer> result = new ArrayList<>(); for (String stringVal : stringArray) { // Guard against non-digit strings to avoid NumberFormatException if (stringVal.matches("\\d+")) { result.add(Integer.parseInt(stringVal)); } } return result; } public int solve(String s) { ArrayList<String> numStrings = new ArrayList<>(); StringBuilder currentNumber = new StringBuilder(); // Traverse each character to collect continuous digit sequences for (char c : s.toCharArray()) { if (Character.isDigit(c)) { currentNumber.append(c); } else { // If we were collecting digits, add the sequence to our list and reset if (currentNumber.length() > 0) { numStrings.add(currentNumber.toString()); currentNumber.setLength(0); } } } // Don't forget to add the last digit sequence if the string ends with digits if (currentNumber.length() > 0) { numStrings.add(currentNumber.toString()); } ArrayList<Integer> intArr = getIntArray(numStrings); // Handle edge case where input has no digits if (intArr.isEmpty()) { throw new IllegalArgumentException("Input string contains no numeric values"); } return Collections.max(intArr); } // Test the code public static void main(String[] args) { DigitExtractor extractor = new DigitExtractor(); System.out.println(extractor.solve("abc123def45xyz789")); // Outputs 789 System.out.println(extractor.solve("hello1world22test333")); // Outputs 333 } }
Key fixes explained
- Proper digit extraction: We iterate through each character in the input string, building up sequences of digits. When we hit a non-digit, we save the current digit sequence (if any) and reset our builder.
- Safe collection handling: We avoid modifying a list while iterating over a fixed length—instead, we build the list of digit strings first, then convert them.
- Error protection: We add checks to avoid converting non-digit strings (preventing
NumberFormatException) and handle the case where the input has no digits at all.
内容的提问来源于stack exchange,提问作者Ayaan Siddiqui
相关产品推荐
相关产品推荐

