• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Program Error,Thanks

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.)
 
Ranch Hand
Posts: 1704
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Clowns were never meant to be THAT big! We must destroy it with this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic