PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, August 9, 2022

[FIXED] how to input two decimal number

 August 09, 2022     decimal, floating-point, java     No comments   

Issue

import java.text.DecimalFormat;
import java.util.Scanner;


public class DecimalFormatExample
{
public static void main(String[] args)
{
    Scanner input=new Scanner(System.in);

    double number1;
    double number2;
    double number3;

    System.out.print("Insert first number:");
    number1=input.nextInt();

    System.out.print("Insert second number:");
    number2=input.nextInt();

    System.out.print("Insert third number:");
    number3=input.nextInt();

    DecimalFormat df = new DecimalFormat("0.00");

    System.out.println(df.format(number1));
    System.out.println(df.format(number2));
    System.out.println(df.format(number3));
}}

when i insert the first number for example:

Insert first number:1

it will print out 1.00

if i insert the number Insert first number:1.00, i get error. How can i put insert the number with decimal point?


Solution

You wrote number1=input.nextInt(); but "1.00" is not an int; it is a double. Try number1=input.nextDouble(); instead.



Answered By - Andrew
Answer Checked By - Terry (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing