Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Program Error,Thanks

 
Jiali Yang
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import java.io.*;
public class MultiplyNumbers{
public static void main(String[] args)
throws java.io.IOException{
String s1;
String s2;
Double num1,num2,product;

InputStreamReader isr=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(isr);

System.out.println("Enter a number: ");
s1=br.readLine();
num1=Double.parseDouble(s1);

System.out.println("Great! Now enter another number: ");
s2=br.readLine();
num2=Double.parseDouble(s2);

product=num1*num2;
System.out.println(num1+" times "+num2+" is "+product);
}
}

以下是错误提示:
MultiplyNumbers.java:14: incompatible types
found : double
required: java.lang.Double
num1=Double.parseDouble(s1);
^
MultiplyNumbers.java:18: incompatible types
found : double
required: java.lang.Double
num2=Double.parseDouble(s2);
^
MultiplyNumbers.java:20: operator * cannot be applied to java.lang.Double,java.lang.Double
product=num1*num2;
^
3 errors
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch!

My guess is that you want num1, num2, and product to be of type double (with a lowercase 'd') instead of type Double (with an uppercase 'D').

(The uppercase Double is a wrapper class.)
 
Kj Reddy
Ranch Hand
Posts: 1704
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Double.parseDouble() method returns primitive data type double and not Double.
So declare num1,num2,product as double instead of Double as:

double num1,num2,product;;
 
Jiali Yang
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your help!
I find JavaRanch yesterday,and I like the website very much.
I'm from China,so my English is poor,but I will work hard to find useful message here.

ychda
Taiyuan,China
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic