Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 14 of 14

Thread: read a double type on the keyboard

  1. #1
    Junior Member
    Join Date
    Jul 2021
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Angry read a double type on the keyboard

    hello
     public static void main(String[] args) {
       double n;
       Scanner sc=new Scanner(System.in );
      n=sc.nextDouble();
     
      }
    when i enter a decimal number on the keyboard it doesn't work for example by entering 4.5

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: read a double type on the keyboard

    it doesn't work
    Please explain what "doesn't work" means.

    The code does not have any print statements to show the results or to prompt the user to enter the number.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    Aba lino (July 18th, 2021)

  4. #3
    Junior Member
    Join Date
    Jul 2021
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: read a double type on the keyboard

     public static void main(String[] args) {
       double n;
       Scanner sc=new Scanner(System.in );
      System.out.println("enter a decimal number") ;
      n=sc.nextDouble();
       System.out.println("you entered" +n);
      }

    it doesn't work when i entered decimal
    Last edited by Mounkaila144; July 16th, 2021 at 11:24 AM.

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: read a double type on the keyboard

    it doesn't work when i entered decimal
    Please explain what you do and what the program does. What is printed out?
    There could be many reasons why code does not work.

    Are decimal points represented by a . or by a , on your computer?
    If you don't understand my answer, don't ignore it, ask a question.

  6. #5
    Junior Member
    Join Date
    Jul 2021
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Lightbulb Re: read a double type on the keyboard

    [QUOTE=Mounkaila144;172218]hello
    [code=Java] public static void main(String[] args) {

    Scanner sc=new Scanner(System.in ); //(import java.util.Scanner before public static void main
    double n=sc.nextDouble();

    System.out.println(n);
    }
    }
    Last edited by parth31; July 18th, 2021 at 06:17 AM.

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: read a double type on the keyboard

    @parth31
    What happens when you compile the code you posted? Are there errors?

    Please test your code before posting it.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #7
    Junior Member
    Join Date
    Jul 2021
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Talking Re: read a double type on the keyboard

    now run the code. there is no errors right now .
    if you are interested in pair programming then E mail me at parthmishra1131@gmail.com

  9. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: read a double type on the keyboard

    Where is the print statement to ask the user to enter a number?

    How is your code different from the code in post#3?
    If you don't understand my answer, don't ignore it, ask a question.

  10. #9
    Junior Member
    Join Date
    Jul 2021
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Red face Re: read a double type on the keyboard

    oh , you want take input from user .
    wait then .

    Scanner sc = new Scanner (System.in);

    System.out.println(" enter your decimal number ");
    double number = sc.nextDouble();

    System.out.println = (number);

    // norm , program #3 is also right . i think i missed to see it .
    // Go with anyone both are kindly similar .

  11. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: read a double type on the keyboard

    Also note that post#3 used code tags which you have not used with your code.
    If you don't understand my answer, don't ignore it, ask a question.

  12. #11
    Junior Member
    Join Date
    Jul 2021
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: read a double type on the keyboard

    like? can you please elaborate...

  13. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: read a double type on the keyboard

    Look at post#3 to see how the code is formatted.
    If you don't understand my answer, don't ignore it, ask a question.

  14. #13
    Junior Member
    Join Date
    Jul 2021
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: read a double type on the keyboard

    now its totally upto you, i write the code in easy way as easy i can write.

  15. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: read a double type on the keyboard

    I don't understand why you are posting on this thread instead of working on the thread that you started.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. trouble with making a simple construct for an array of double type
    By Abadude in forum What's Wrong With My Code?
    Replies: 9
    Last Post: November 29th, 2012, 05:05 PM
  2. How to convert from type double to type int?
    By rph in forum Object Oriented Programming
    Replies: 7
    Last Post: July 25th, 2011, 04:21 AM
  3. [SOLVED] Read double from console without having to read a string and converting it to double.
    By Lord Voldemort in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: June 26th, 2011, 08:08 AM
  4. how to read an integer of DOUBLE datatype with type casting
    By amr in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 14th, 2010, 03:03 PM
  5. Typecasting of double variable to integer
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 2
    Last Post: December 5th, 2010, 03:41 AM