-2

I want to create a small console program which convert from Celsius - Fahrenheit and vice versa, this is the code I tried:

import java.util.Scanner;

public class CelsiusFahrenheit {

    private static Scanner s;

    public static void main(String[] args) {

        s = new Scanner(System.in);
        char reponse = 'N', choixConvert;

        do
        {
            do
            {
                System.out.println("Choisie le mode de converstion : ");
                System.out.println("1 - Converstisseur Celesuis - Fahrenheit");
                System.out.println("2 - Converstisseur Fahrenheit - Celesuis");

                do
                {
                    System.out.print("Votre Choix: ");
                    choixConvert = s.next().charAt(0);
                    if(choixConvert != '1' && choixConvert != '2')
                          System.out.println("Mode inconnu, veuillez réitérer votre choix.");
                }
                while(choixConvert!='1' && choixConvert!='2');

                System.out.print("Température à convertir : ");
                double temp = s.nextDouble();
                System.out.print("Resultat est: " + ConvertCelesuisFahrenheit(choixConvert,temp));
                System.out.println("\nSouhaitez-vous convertir une autre température ? (O/N)");
                reponse = Character.toUpperCase(s.nextLine().charAt(0));

            }while(reponse != 'O' && reponse != 'N');
        }while(reponse == 'O');

        System.out.println("Au revoir !");
    }

    private static Double ConvertCelesuisFahrenheit(int choixConvert,double temp )
    {
        if(choixConvert == 1)
            return (9/5)*temp+32;
        else
            return (temp-32)*(5/9);
    }
}

but I had a problem that the function ConvertCelesuisFahrenheit() always return 0.0 and after that it gives me this error:

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

This is the output:

Choisie le mode de converstion : 
1 - Converstisseur Celesuis - Fahrenheit
2 - Converstisseur Fahrenheit - Celesuis
Votre Choix: 2
Température à convertir : 33
Resultat est: 0.0
Souhaitez-vous convertir une autre température ? (O/N)
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0
    at java.lang.String.charAt(Unknown Source)
    at CelsiusFahrenheit.main(CelsiusFahrenheit.java:33)
2
  • 1
    I think it'd be a lot easier to answer your question if it weren't so... messy. You have so many do - while loops, honestly you could probably do it in one loop. If you could just show us line 33, that would be better. Commented Oct 22, 2012 at 23:17
  • this is the line 33: reponse = Character.toUpperCase(s.nextLine().charAt(0)); Commented Oct 22, 2012 at 23:23

3 Answers 3

7

nextDouble() leaves the newline in the input stream, so when you then call

nextLine().charAt(0)

the result of nextLine() is an empty string. Remove the newline from the stream, or use next(), as you did above.

Sign up to request clarification or add additional context in comments.

5 Comments

I tried that but I had this error : Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0 at java.lang.String.charAt(Unknown Source) at CelsiusFahrenheit.main(CelsiusFahrenheit.java:31) The line 31 : s.nextLine().charAt(0);
What did you try? Of course, even if you remove the newline, if the next line the scanner gets is empty, you'll get the same exception.
I did this : double temp = s.nextDouble(); s.nextLine().charAt(0);
That doesn't remove the newline from the stream at all. I strongly advise you to use next() instead of nextLine() there to ensure you don't get an empty line.
Okey my problem has been solved I replaced double s.nextLine(); by s.next();
2

There's several problems here ..

nextLine().charAt(0) gets the empty string because the new line is not consumed by nextDouble()

You can fix this by simply adding a

String continueString = s.next();
response = Character.toUpperCase(continueString.charAt(0));

In addition, the math in your conversion calculation is incorrect. Use 5.0/9.0 instead of 5/9.

Finally, you are reading the choixConvert character as a char , but then passing it as an int into your method, so the else block will always execute.

1 Comment

Okey thanks I have correct all those problems, but the main problem didn't solved
1

Update this: reponse = Character.toUpperCase(s.nextLine().charAt(0)); with

          String line = s.nextLine();
          while(line.length() <1){
             line = s.nextLine();
          }
          reponse = Character.toUpperCase(line.charAt(0));

4 Comments

I did this but I had the same error
@SuSha At same line? It should be coming from different line now.
My problem has been solved the problem was the nextLine() I should use next()
@SuSha Good to know. Hope that you got the differences as well. next reads the next string while nextList reads the entire line. I am hopeful, you can fix you program using nextLine as well.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.