-3

Hi I just started learning Java, I'm trying to convert from Celsius to Fahrenheit and vice-versa, but my code below throws the following exception:

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: 
String index out of range: 0
    at java.lang.String.charAt(Unknown Source)
    at hello.main(hello.java:33)

This is my program:

public class hello {

    public static void main(String[] args) {
        System.out.println("CONVERTISSEUR DEGRES CELSIUS ET DEGRES FAHRENHEIT");
        System.out.println("-------------------------------------------------");
        Scanner sc = new Scanner(System.in);
        byte a = sc.nextByte();
        char test = 'O';
        while(test == 'O') {
            switch(a) {
                case 1: {
                    System.out.println("Température a convertir :");
                    float temp=sc.nextFloat();
                    float nvtemp=temp * 9/5 + 32;
                    System.out.println(temp + " °C correspond a : " + nvtemp + " °F");
                    }break;
                case 2: {
                    System.out.println("Température a convertir :");
                    float temp = sc.nextFloat();
                    float nvtemp = (temp - 32) * 5/9;
                    System.out.println(temp + " °F correspond a : " + nvtemp + " °C");
                    }break;
                default:
                    System.out.println("Stp entrer 1 ou 2  !!");
            }

            test = ' ';
            while(test != 'O' && test != 'N') {
               System.out.println("Souhaitez-vous convertir une autre température ?(O/N)");
               test = sc.nextLine().charAt(0);
            }
        }
        System.out.println("Goodbye !!");       
    }
}

Can anyone help me understand why my code is throwing StringIndexOutOfBoundsException?

Jonny Henly
  • 4,023
  • 4
  • 26
  • 43
jest.bou
  • 27
  • 8
  • 1
    The first call of `sc.nextLine()` returns an empty String (for `case 1` and `case 2`) and calling `.charAt(0)` on an empty String causes this exception. Check the mentioned duplicate question to know why it returns an empty String. – Tom Nov 16 '15 at 05:24

1 Answers1

0

In the second while loop you are calling test = sc.nextLine().charAt(0);. sc.nextLine() is used for reading any remaining characters which are left by previous nextXXX() method calls. So instead of using test = sc.nextLine().charAt(0); use test = sc.next().charAt(0); in the second while loop.

i.e. the below code works

public class hello {

public static void main(String[] args) {
    System.out.println("CONVERTISSEUR DEGRES CELSIUS ET DEGRES FAHRENHEIT");
    System.out.println("-------------------------------------------------");
    System.out
            .println("Choisir le mode de conversion :\n1 - Convertisseur Celsius - Fahrenheit\n2 - Convertisseur Fahrenheit - Celsius");
    Scanner sc = new Scanner(System.in);
    byte a = sc.nextByte();
    char test = 'O';
    while (test == 'O') {
        switch (a) {
        case 1: {
            System.out.println("Température a convertir :");
            float temp = sc.nextFloat();
            float nvtemp = temp * 9 / 5 + 32;
            System.out.println(temp + " °C correspond a : " + nvtemp
                    + " °F");
        }
            break;
        case 2: {
            System.out.println("Température a convertir :");
            float temp = sc.nextFloat();
            float nvtemp = (temp - 32) * 5 / 9;
            System.out.println(temp + " °F correspond a : " + nvtemp
                    + " °C");
        }
            break;
        default:
            System.out.println("Stp entrer 1 ou 2  !!");
        }
        test = ' ';
        while (test != 'O' && test != 'N') {
            System.out
                    .println("Souhaitez-vous convertir une autre température ?(O/N)");
            test = sc.next().charAt(0);
        }
    }
    System.out.println("Goodbye !!");

}
}
Jagadeesh
  • 862
  • 7
  • 23