Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

need help on a simple JAVA program

Posted on 2003-02-22
11
Medium Priority
?
164 Views
Last Modified: 2010-03-31
Dear all
 
    I need a simple program that can read two input numbers(type:Double), add each other and print out. I looked many books, but they all focus on Applet or web programming, can't find any sample that I can use. Please help me on this program. Thank you.

CC
0
Comment
Question by:puppyhuang
10 Comments
 
LVL 18

Accepted Solution

by:
bobbit31 earned 100 total points
ID: 8000287
import java.io.*;

public class AddDoubles {
   public static void main(String[] args) {
       double d1, d2;

       BufferedReader in = null;

       try {
            in = new BufferedReader(new InputStreamReader(System.in));
            String strD1, strD2;

            System.out.print("Enter number 1: ");
            strD1 = in.readLine();
            System.out.print("Enter number 2: ");
            strD2 = in.readLine();

            d1 = Double.parseDouble(strD1);
            d2 = Double.parseDouble(strD2);

            System.out.println (d1 + d2);
        } catch (Exception e) {
             e.printStackTrace();
        } finally {
             if (in != null) {
                  try {
                       in.close();
                  } catch (IOException ignored) {}
             }
        }
     }
}
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8000569
That's somebody's homework fixed then...
0
 
LVL 18

Expert Comment

by:bobbit31
ID: 8000604
wow, if that's homework, than our school system is going downhill... sorry, i didn't interpret it as a homework... mods repremand me if you want... but if you look at my profile, i definitely do not make it a habit to answer blatant homework q's
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Expert Comment

by:CEHJ
ID: 8000636
Don't worry bobbit - it's easy to forget sometimes ;-)
0
 

Author Comment

by:puppyhuang
ID: 8001861
Thank you very much. You gave me a very good sample and I made another one for my homework.  Thank you for your help.

CC
0
 
LVL 6

Expert Comment

by:jwenting
ID: 8002124
the school system IS going downhill, dumb people are easier to manipulate so they'll believe the lies in the election campaigns more easily :-)
But remember that classes have to start simple with their lessons, you don't know how far along in his Java classes this questioner is...
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8003217
QED bobbit ;-)

It's certainly much better than the habit of giving beginners ludicrously ambitious tasks, which happens frequently.
0
 

Expert Comment

by:CleanupPing
ID: 9059144
puppyhuang:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
 
LVL 18

Expert Comment

by:bobbit31
ID: 9072061
recommendation: points to bobbit31
0
 
LVL 7

Expert Comment

by:tomboshell
ID: 9582325
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:
Accept bobbit31's comment as answer.

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Thomas Boshell
EE Cleanup Volunteer
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses
Course of the Month14 days, 16 hours left to enroll

577 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question