首页 > 解决方案 > 全新的编码和非常困惑为什么我会收到以下错误

问题描述

我正在为我的 CS 课程介绍使用二进制到十进制转换器。我不断收到错误:

“线程“主”java.lang.StringIndexOutOfBoundsException 中的异常:字符串索引超出范围:java.base/java.lang.StringLatin1.charAt(StringLatin1.java:48) 的 java.base/java.lang.String 中的 8。 charAt(String.java:711) at lab05.Lab05.main(Lab05.java:40) C:\Users\anton\OneDrive\Desktop\CS 0007\Lab05\nbproject\build-impl.xml:1384: 以下错误执行此行时发生:C:\Users\anton\OneDrive\Desktop\CS 0007\Lab05\nbproject\build-impl.xml:908:执行此行时发生以下错误:C:\Users\anton\OneDrive\ Desktop\CS 0007\Lab05\nbproject\build-impl.xml:961:Java 返回:1 BUILD FAILED(总时间:10 秒)在尝试运行以下代码时。”

请帮忙

package lab05;

import javax.swing.JOptionPane;

import java.util.Scanner;


public class Lab05 {

// Global Variables    
private static double binaryNum;
private static String userInputBinary;
        
    public static void main(String[] args) {
   
        // requesting input for the desired type of conversion
        String typeOfConversion = JOptionPane.showInputDialog("Type of "
                + "conversion: \n1 for binary to decimal\n" + 
                "2 for decimal to binary");
        
            // converting string into double            
            double conversionType = Double.parseDouble(typeOfConversion);
            
        // requesting input of binary number
        if(conversionType == 1) {
            userInputBinary = JOptionPane.showInputDialog("Enter "
                    + "binary number using 8 bits");
        }
        boolean correctEntry = userInputBinary.length() == 8;
        if(correctEntry) {
            
            double b0 = Double.parseDouble(Character.toString(userInputBinary.charAt(0)));
            double b1 = Double.parseDouble(Character.toString(userInputBinary.charAt(1)));
            double b2 = Double.parseDouble(Character.toString(userInputBinary.charAt(2)));
            double b3 = Double.parseDouble(Character.toString(userInputBinary.charAt(3)));
            double b4 = Double.parseDouble(Character.toString(userInputBinary.charAt(4)));
            double b5 = Double.parseDouble(Character.toString(userInputBinary.charAt(5)));
            double b6 = Double.parseDouble(Character.toString(userInputBinary.charAt(6)));
            double b7 = Double.parseDouble(Character.toString(userInputBinary.charAt(7)));
            double b8 = Double.parseDouble(Character.toString(userInputBinary.charAt(8)));
        
            double decimalOutput = (b0 * Math.pow(2,0) + b1 * 
                Math.pow(2,1) + b2 * Math.pow(2,2) + b3 * Math.pow(2,3) + b4 
                * Math.pow(2,4) + b5 * Math.pow(2,5) + b6 * Math.pow(2,6)
                + b7 * Math.pow(2,7) + b8 * Math.pow(2,8));
            JOptionPane.showMessageDialog(null, "Your binary number is "
                    + "equivalent to:" + decimalOutput , "Conversion", 0);
        } else if (!correctEntry) {
            JOptionPane.showMessageDialog(null, "You did not enter the binary"
                    + " number using 8 bits", "Error", 0);
            System.exit(0);
        }
        // Scanner declaration
        Scanner keyboard = new Scanner(System.in); 
        
        

        
    }
    
}

标签: java

解决方案


推荐阅读