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
?