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)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.