简体   繁体   English

编译Java项目后出错:线程“ main”中的异常java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:0

[英]Error after compiling Java project : Exception in thread “main” java.lang.StringIndexOutOfBoundsException: String index out of range: 0

I am working on a project for school. 我正在为一个学校项目。 The project is a game where the user needs to guess the price of an object. 该项目是一个游戏,用户需要猜测对象的价格。 The price is generated randomly. 价格是随机产生的。 The user has 5 chances to guess it. 用户有5次机会猜测它。 if he doesnt, th game is over and is asked if he wants to replay or not. 如果没有,则游戏结束,并询问他是否要重播。 The name of this game could be known as The Price is Right. 该游戏的名称可能被称为“正确的价格”。 My problem is that when i run the project it crashes just before taking the answer of the user, to know if he wants to replay. 我的问题是,当我运行项目时,它会在获取用户答案之前崩溃,以了解他是否要重播。

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0
at java.lang.String.charAt(String.java:658)
at pkgthepriceisrightv1.ThePriceisRightV1.main(ThePriceisRightV1.java:102)
Java Result: 1
BUILD SUCCESSFUL (total time: 7 seconds)

This is the error. 这是错误。 I dont know what is the problem.... 我不知道这是什么问题。

And here is the code lines where i am taking the information, the yes or no from the user, wich is the line 102, in the error code, i have declared all my variables. 这是我要获取信息的代码行,是来自用户的是还是否,其中第102行是错误代码,我已经声明了所有变量。 valeur_recommencer is a char, and recommencer is a string. valeur_recommencer是一个字符,recommencer是一个字符串。 All my code is in Java. 我所有的代码都使用Java。 Excuse me for the language of the code, its all in french, a requirement for school. 对不起,代码的语言全部为法语,这是学校的要求。

recommencer = lire.nextLine();   
valeur_recommencer = recommencer.charAt(0);

This returns empty string "" . 这将返回空字符串""

recommencer = lire.nextLine();

so when you try to find the first char of empty string, it ends with exception. 因此,当您尝试查找空字符串的第一个字符时,它以异常结尾。

Ok found my mistake. 确定发现了我的错误。 I needed to add an other Scanner. 我需要添加其他扫描仪。 If this error code is shown for any body else, try adding an other scanner. 如果此错误代码在其他任何地方仍然显示,请尝试添加其他扫描仪。 The sacnner could not read too much of answers from the user, so is crashes. sacnner无法从用户那里读取太多答案,因此崩溃也很严重。 I think it could solve the problem 我认为这可以解决问题

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 该程序不断给我一个错误:线程“ main”中的异常java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:4 - This program keeps on giving me an error: Exception in thread “main” java.lang.StringIndexOutOfBoundsException: String index out of range: 4 使用递归错误。 线程“main”中的异常 java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:0 - using recursion error. Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0 线程“main”中的错误异常java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:-1 - Error Exception in thread “main” java.lang.StringIndexOutOfBoundsException: String index out of range: -1 如何修复线程“main”中的异常 java.lang.StringIndexOutOfBoundsException: String index out of range: error - How to fix Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: error anagram的运行时错误-线程“ main”中的异常java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:-1 - Runtime Error for an anagram - Exception in thread “main” java.lang.StringIndexOutOfBoundsException: String index out of range: -1 为什么程序会抛出这个错误??:线程“main”中的异常java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:36 - Why is the program throwing this error??: Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 36 压缩类错误 - 线程“main”中的异常 java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:-1 - Compression class error - Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1 我不断收到此错误:线程“main”中的异常 java.lang.StringIndexOutOfBoundsException: String index out of range: 28 - I keep getting this error : Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 28 我收到如下错误消息:线程“ main”中的异常java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:0 - I get an error message as follows: Exception in thread “main” java.lang.StringIndexOutOfBoundsException: String index out of range: 0 Java:线程“ main”中的异常java.lang.StringIndexOutOfBoundsException:字符串索引超出范围: - Java : Exception in thread “main” java.lang.StringIndexOutOfBoundsException: String index out of range:
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM