Home > Blockchain >  Build is successful but I kept getting the 'Exception in thread "main" java.lang.Stri
Build is successful but I kept getting the 'Exception in thread "main" java.lang.Stri

Time:10-30

I have 3 classes but I think the problems are occurring between the Main and the Roman Numerals class. . . would also include the class Variables as well. . .

import java.util.Scanner;

public class Main{

public static void main (String[] args){
    System.out.println("Press 1 for RPS, 2 for Flowchart, 3 for Roman Numerals, 4 for Ascendant");
    Scanner Selection = new Scanner(System.in);
    Variables sel = new Variables(); 
    sel.run = Selection.nextLine();
        {
        if (sel.run.equals("1")){
        System.out.println("You have chosen and initialized 1!");
        System.out.println("\nAttempting to Connect to RPS Class\n");
        RPS JackEnPoy = new RPS();
        JackEnPoy.RockPaperScissors();
        }
        else if (sel.run.equals("2")){
        System.out.println("You have chosen and initialized 2!");
        System.out.println("\nAttempting to Connect to Flowchart Class\n");
        Flowchart chart = new Flowchart();
        chart.Flow();
        }
        else if (sel.run.equals("3")){
        System.out.println("You have chosen and initialized 3!");
        System.out.println("\nAttempting to Connect to RomanNumerals Class\n");
        RomanNumerals rn = new RomanNumerals();
        rn.Numeral();
        }
        else if (sel.run.equals("4")){
        System.out.println("You have chosen and initialized 4!");
        System.out.println("\nAttempting to Connect to Ascending Class\n");
        //Ascending aorder = new Ascending();
        //aorder.Ascendant();

        }
        }
    }
}

and the class variables for the other classes

class Variables{

String run;
//RPS
String guide;
String RW = "Rock Smashes Scissors!";
String PW = "Paper Covers Rock!";
String SW = "Scissors Shreds Paper!";
String T = "Impasse!";
//Flowchart
int x;
int y;
int z;
boolean YN;
}

and this is the Roman Numerals that I've been currently trying to work on whilst the RPS(Rock Paper Scissors) class and Flowchart class worked fine

import java.util.Scanner;
import javax.swing.JOptionPane;
public class RomanNumerals{

public static void Numeral()
{
//Declarations
String num = " ", roman = " ";
char sen = num.charAt(0), hachi = num.charAt(1), jyu = num.charAt(2), ichi = num.charAt(3);
int convert;

//String from num to Integer
convert = Integer.parseInt(num);
num = JOptionPane.showInputDialog("Convert from Whole Numbers to Roman Numerals(Maximum:3000)");


//'if' more than 3k then terminate 'else' attempt conversion
if (convert > 3000)
{
//Invalid Message with WARNING_MESSAGE dialog
JOptionPane.showMessageDialog (null, "Input had exceeded the maximum of 3000", "RomanNumerals3000", JOptionPane.WARNING_MESSAGE);
}
else
{
if (ichi == '1')//0001
roman  = "I";
if (ichi == '2')//0002
roman  = "II";
if (ichi == '3')//0003
roman  = "III";
if (ichi == '4')//0004
roman  = "IV";
if (ichi == '5')//0005
roman  = "V";
if (ichi == '6')//0006
roman  = "VI";
if (ichi == '7')//0007
roman  = "VII";
if (ichi == '8')//0008
roman  = "VIII";
if (ichi == '9')//0009
roman  = "IX";
    if (jyu == '1')//0010
    roman  = "X";
    if (jyu == '2')//0020
    roman  = "XX";
    if (jyu == '3')//0030
    roman  = "XXX";
    if (jyu == '4')//0040
    roman  = "XL";
    if (jyu == '5')//0050
    roman  = "L";
    if (jyu == '6')//0060
    roman  = "LX";
    if (jyu == '7')//0070
    roman  = "LXX";
    if (jyu == '8')//0080
    roman  = "LXXX";
    if (jyu == '9')//0090
    roman  = "XC";
        if (hachi == '1')//0100
        roman  = "C";
        if (hachi == '2')//0200
        roman  = "CC";
        if (hachi == '3')//0300
        roman  = "CCC";
        if (hachi == '4')//0400
        roman  = "CD";
        if (hachi == '5')//0500
        roman  = "D";
        if (hachi == '6')//0600
        roman  = "DC";
        if (hachi == '7')//0700
        roman  = "DCC";
        if (hachi == '8')//0800
        roman  = "DCCC";
        if (hachi == '9')//0900
        roman  = "CM";
                if (sen == '1')//1000
                roman  = "M";
                if (sen == '2')//2000
                roman  = "MM";
                if (sen == '3')//3000
                roman  = "MMM";
JOptionPane.showMessageDialog(null, "Whole Number Form = "   num   "\nRoman Numeral Form = "   roman, "Converted!!!", JOptionPane.INFORMATION_MESSAGE,null);
}
System.exit(0);
}
}

and the output went on like this. .

3
You have chosen and initialized 3!

Attempting to Connect to RomanNumerals Class

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 1
    at java.base/java.lang.StringLatin1.charAt(StringLatin1.java:48)
    at java.base/java.lang.String.charAt(String.java:1512)
    at RomanNumerals.Numeral(RomanNumerals.java:9)
    at Main.main(Main.java:27)
Command execution failed.```
I'm still new here and can't figure out on why am I getting this error after initiating 3 as in the RomanNumerals class. . 

CodePudding user response:

char sen = num.charAt(0), hachi = num.charAt(1), jyu = num.charAt(2), ichi = num.charAt(3);

If the string is less than 4 characters long, this will crash because it doesn't know what to return for num.charAt(). You want to rewrite this so it only gets the characters if the length of the string is long enough.

CodePudding user response:

So your problem looks like it's here:

String num = " ", roman = " ";

So at the point above you've initialized num to be a String with a single character, a space, in it.

char sen = num.charAt(0), hachi = num.charAt(1), jyu = num.charAt(2), ichi = num.charAt(3);

In the following line above, you are asking for the first ( char(0) cause string character indexing is zero-based in Java), second (char(1)), third (char(2)) and fourth (char(3)) characters.

This fails cause the string only has the single character.

Also, just because code compiles doesn't mean it will run successfully.

  • Related