简体   繁体   English

数字输入异常线程“main”java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:0

[英]Number input Exception thread “main” java.lang.StringIndexOutOfBoundsException: String index out of range: 0

In my program you're supposed to enter the distance which is assigned to a variable. 在我的程序中,您应该输入分配给变量的距离。 You then select a number that signifies what type of conversion you want to make. 然后,您可以选择一个数字来表示您要进行的转换类型。 For example, if I selected the number 1 and pressed enter, it would convert my meters to kilometers. 例如,如果我选择数字1并按下回车键,它会将我的米转换为千米。

I went to do a test run, entered the meters in and pressed enter. 我去做试运行,进入仪表并按下进入。 I was unable to enter what conversion I wanted to do as an exception was caused like here: 我无法输入我想要做的转换,因为这里引起了异常:

Please enter the distance in meters: 500
Please enter the number of the conversion you want to make: 
1. Convert to Kilometers 
2. Convert to Inches 
3. Convert to Feet 
4. Quit ProgramException in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0
at java.lang.String.charAt(String.java:658)
at ConversionWilson.main(ConversionWilson.java:41)

Not sure what is causing this exception. 不确定导致此异常的原因。

public static void main (String [] args)
{

// Scanner object to read input
Scanner keyboard = new Scanner (System.in);
// Prompt the user for distance and conversion.
System.out.println("Welcome to the Conversion Program.");
System.out.println("With this program, you can enter a distance and convert it to another form of measurement.");
System.out.print("Please enter the distance in meters: ");

if (meters >= 0)
{
   meters = keyboard.nextDouble();
}
else
{
   System.out.println("Meters cannot be a negative number. Please choose a positive number.");
}

System.out.print("Please enter the number of the conversion you want to make: \n" +
                 "1. Convert to Kilometers \n" + "2. Convert to Inches \n" + "3. Convert to Feet \n" +
                 "4. Quit Program");
input = keyboard.nextLine();
conversion = input.charAt(0);

// Deciding what conversion method to call.

switch (conversion)
{
  case '1':
     showKilometers(meters);
     break;

  case '2':
     showInches(meters);
     break;

  case '3':
     showFeet(meters);
     break;

  case '4':
     System.out.println("Beep boop bop. Quitting the program now. Later.");
     break;

  default:
     System.out.println("You did not select a possible choice. Please run the program again and be sure to choose a correct number.");
}         

I tried seeing if I could find a resolve via a search, but most of them seem to have an issue with the character at index 0 being a letter, where my input is a number. 我试着看看我是否可以通过搜索找到解决方案,但是大多数人似乎都遇到了索引0处字符的问题,我的输入是一个数字。

meters = keyboard.nextDouble(); is the issue as it only reads the number and not the entire line. 是问题,因为它只读取数字而不是整行。 You need to add keyboard.nextLine() after it. 你需要在它之后添加keyboard.nextLine()。 As the nextLine() call later is consuming the rest of the line. 因为稍后的nextLine()调用正在消耗该行的其余部分。

Just make your if statement like this, 只要像这样制作你的if语句,

        if (meters >= 0)
        {
           meters = keyboard.nextDouble();
           keyboard.skip("\n"); //This will skip the '\n' you entered after entering the meters.
        }

Because of '\\n' is still there in the buffer the keyboard.nextLine() after that is going to read that and you are getting ArrayIndexOutOfBounds Exception. 因为'\\ n'仍然存在于缓冲区中,所以在此之后的keyboard.nextLine()将会读取它并且您将获得ArrayIndexOutOfBounds Exception。

暂无
暂无

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

相关问题 编译Java项目后出错:线程“ main”中的异常java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:0 - Error after compiling Java project : Exception in thread “main” java.lang.StringIndexOutOfBoundsException: String index out of range: 0 线程“main”中的回文字符串异常 java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:-1 - Palindrome String Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1 Java:线程“ main”中的异常java.lang.StringIndexOutOfBoundsException:字符串索引超出范围: - Java : Exception in thread “main” java.lang.StringIndexOutOfBoundsException: String index out of range: 如何修复 Java 中的“线程“主”中的“异常”java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:5”问题 - How to fix “Exception in thread ”main“ java.lang.StringIndexOutOfBoundsException: String index out of range: 5” problem in Java 线程“main”中的异常 java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:5 - Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 5 该程序不断给我一个错误:线程“ 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中的异常:字符串索引超出范围 - Exception in thread "main java.lang.StringIndexOutOfBoundsException: String index out of range 线程“ main”中的异常java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:4 - Exception in thread “main” java.lang.StringIndexOutOfBoundsException: String index out of range: 4 线程“main”中的错误异常java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:-1 - Error Exception in thread “main” java.lang.StringIndexOutOfBoundsException: String index out of range: -1
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM