Keyboard input (java)

Solved
Bonjour,

I have this piece of code:

 try { Scanner s = new Scanner(System.in); System.out.println("enter the iteration step"); h = s.nextDouble(); } catch(Exception e){System.out.println("no h :(");} 


Knowing that h is of type:
 public static double h; 


However, it doesn't work when I enter 0.2 for example... if you know why, your help is welcome.

--
Truth belongs to those who seek it and not to those who claim to possess it.
(don't forget to mark resolved if you found your happiness: think of others^^')
Configuration: windows vista, firefox

11 answers

  1. Hello,

    what is not "working"? compilation error or runtime error? is it doing something it shouldn't? is it not doing something it should? if so, what? "With 0.2 for example": does that mean it doesn't work with 0.2, but it works with something else? if so, what? if not, it should be specified.

    Otherwise, I see something strange: System.out.println("no h :(");

    This will not display the value of h....
    0
    1. The try block doesn't work, the program compiles, everything works except for this part, h = s.nextDouble();

      It directly goes into the catch and doesn't display h :( because the try didn't work.

      Basically, how do we record a keyboard input of type 0.x?

      --
      The truth belongs to those who seek it, not to those who claim to possess it.
      (don't forget to mark as resolved if you found your happiness: think of others^^')
      0
      1. In the catch, you can display the exception; the message can be informative.
        0
    2. I'm having trouble putting it in :s I don't know how to do it, it's not working IOException e
      --
      The truth belongs to those who seek it and not to those who claim to hold it.
      (don't forget to mark resolved if you found your happiness: think of others^^')
      0
      1. Hello,

        Please provide us with the part of the code concerned, it will be easier to correct...

        Best regards,

        Dan
        0
        1. I only provided the relevant part and made sure it compiles:

           import java.io.IOException; import java.util.Scanner; class TestLissage { public static int n; public static double y0, x0, Yn, Xn, h; public static int choix; public static void main(String[] args) { while (choix != 6 && choix != 5 && choix != 4 && choix != 3 && choix != 2 && choix != 1 && choix != 7) { System.out.println("please choose the application to solve:"); System.out.println(""); System.out.println("1: y'(x) = x + y(x) solution: y(x) = exp(x)-x-1 with y(0) = 0"); System.out.println("2: y'(x) = -2xy(x) solution: y(x) = exp(-x²) with y(0) = 1"); System.out.println("3: y'(x) = -xy²(x) solution: y(x) = 2/(1+x²) with y(0) = 2"); System.out.println("4: y'(x) = y(x) solution: y(x) = exp(x) with y(0) = 1"); System.out.println("5: y'(x) = "); System.out.println("6: y'(x) = "); System.out.println("7: exit"); try { Scanner s = new Scanner(System.in); System.out.println("enter your choice"); choix = s.nextInt(); } catch(Exception e){} } if (choix == 1 || choix == 2 || choix == 3 || choix == 4 || choix == 5 || choix == 6) { try { Scanner s = new Scanner(System.in); System.out.println("enter the number of iterations"); n = s.nextInt(); } catch(Exception e){} try { Scanner s = new Scanner(System.in); System.out.println("enter the iteration step"); h = s.nextDouble(); } catch(Exception e){} System.out.println(choix); System.out.println(n); System.out.println(h); } } 

          --
          Truth belongs to those who seek it and not to those who claim to possess it.
          (don't forget to mark it solved if you have found your happiness: think of others^^')
          0
          1. Re,

            To get the StackTrace list, you add this to each catch:

             catch(Exception e) { System.err.println("Exception :"); e.printStackTrace(); } 


            Dan
            0
            1. It shows

              Exception :
              java.util.InputMismatchException
              at java.util.Scanner.throwFor(Unknown Source)
              at java.util.Scanner.next(Unknown Source)
              at java.util.Scanner.nextDouble(Unknown Source)
              at TestLissage.main(TestLissage.java:44)

              so it corresponds to this line:

              h = s.nextDouble();
              --
              The truth belongs to those who seek it and not to those who claim to possess it.
              (don't forget to mark resolved if you have found your happiness: think of others^^')
              0
              1. make a test by entering 0.2 >>>> comma

                Dan
                0
                1. You're too strong :)

                  It works, thank you very much
                  --
                  The truth belongs to those who seek it, not to those who claim to possess it.
                  (don't forget to mark resolved if you have found your happiness: think of others^^')
                  0
                  1. I think you understood why: the locale...

                    Dan
                    0