I am working on a binary to decimal converter for my intro to CS class. I keep getting the error:
"Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 8 at java.base/java.lang.StringLatin1.charAt(StringLatin1.java:48) at java.base/java.lang.String.charAt(String.java:711) at lab05.Lab05.main(Lab05.java:40) C:\Users\anton\OneDrive\Desktop\CS 0007\Lab05\nbproject\build-impl.xml:1384: The following error occurred while executing this line: C:\Users\anton\OneDrive\Desktop\CS 0007\Lab05\nbproject\build-impl.xml:908: The following error occurred while executing this line: C:\Users\anton\OneDrive\Desktop\CS 0007\Lab05\nbproject\build-impl.xml:961: Java returned: 1 BUILD FAILED (total time: 10 seconds)when trying to run the following code."
Please help
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);
}
}