Java.lang.StringIndexOutOfBoundsException(Java.lang.StringIndexOutOfBoundsException)

编程入门 行业动态 更新时间:2024-10-26 10:26:43
Java.lang.StringIndexOutOfBoundsException(Java.lang.StringIndexOutOfBoundsException)

到目前为止这是我的代码。 我对java.lang异常感到困惑。 我是编程新手。 我的代码有什么问题?

import javax.swing.*; import java.lang.Character; import java.io.*; public class HWCent { public static void main(String args [])throws IOException { String vince = JOptionPane.showInputDialog("Enter Your File path :"); String c = JOptionPane.showInputDialog("Enter a character"); int NOc = 0; for(int v = 1; v<=c.length(); v++) { char x = c.charAt(v); if(Character.isSpaceChar(x)) { NOc++; } char z = c.charAt(v); if(Character.isLetter(z)) { NOc++; } } File file = new File(vince); if(!file.exists()) { JOptionPane.showMessageDialog(null,"Wrong file path !"); } else { JOptionPane.showMessageDialog(null, "The Number of Characters in "+ c +" is "+ NOc); try { RandomAccessFile gui = new RandomAccessFile(file," "); gui.writeBytes("The number of Characters in "+ c + " is " +NOc); gui.close(); } catch(IOException m) { System.out.print(m.getMessage()); } } } }

This is my code so far. I am confused about that java.lang exception. I'm new to programming. What could be wrong with my code?

import javax.swing.*; import java.lang.Character; import java.io.*; public class HWCent { public static void main(String args [])throws IOException { String vince = JOptionPane.showInputDialog("Enter Your File path :"); String c = JOptionPane.showInputDialog("Enter a character"); int NOc = 0; for(int v = 1; v<=c.length(); v++) { char x = c.charAt(v); if(Character.isSpaceChar(x)) { NOc++; } char z = c.charAt(v); if(Character.isLetter(z)) { NOc++; } } File file = new File(vince); if(!file.exists()) { JOptionPane.showMessageDialog(null,"Wrong file path !"); } else { JOptionPane.showMessageDialog(null, "The Number of Characters in "+ c +" is "+ NOc); try { RandomAccessFile gui = new RandomAccessFile(file," "); gui.writeBytes("The number of Characters in "+ c + " is " +NOc); gui.close(); } catch(IOException m) { System.out.print(m.getMessage()); } } } }

最满意答案

如果你有一个长度为6的字符串,那么你可以访问的最后一个索引是5.当你使用它时

for(int v = 1; v <= c.length(); v++)

您最终尝试访问不存在的索引6。 只需改变

for(int v = 0; v < c.length(); v++) // Notice < instead of <=

另外,请注意我将v = 1更改为v = 0 。 Java字符串从0开始编制索引,因此您需要从那里开始访问字符串的第一个字符。

If you have a string with a length of 6 then the last index you can access is 5. When you used this

for(int v = 1; v <= c.length(); v++)

you end up trying to access an index of 6 which does not exist. Just change to

for(int v = 0; v < c.length(); v++) // Notice < instead of <=

Also, notice I changed v = 1 to v = 0. Java Strings are indexed starting at 0 so you need to start there to access the first character of your string.

更多推荐

本文发布于:2023-07-22 20:49:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1223319.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Java   lang   StringIndexOutOfBoundsException

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!