simple java program

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Aug 2009
Posts: 50
Reputation: coud_ren_26 has a little shameless behaviour in the past 
Solved Threads: 0
coud_ren_26's Avatar
coud_ren_26 coud_ren_26 is offline Offline
Junior Poster in Training

simple java program

 
-1
  #1
Oct 2nd, 2009
I want first to input the first number then the second number. After that input sub or add. If sub, 1st num-2nd num. If add, 1st num+second num. Then print the sum/difference. I started a code that will input 2 numbers then add. Please revise this one i can't revise it. Thank you!





  1. import java.io.*;
  2. public class reviewExer1 {
  3. public static int addNum(int xVar, int yVar){
  4. int result = 0;
  5. result = xVar + yVar;
  6. return result;
  7. }
  8.  
  9. public static void main (String[]args){
  10. int xVal = 0;
  11. int yVal = 0;
  12.  
  13.  
  14.  
  15. BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in));
  16. String xinput = "0";
  17. String yinput = "0";
  18. System.out.print("Input x");
  19.  
  20. try {
  21. xinput = dataIn.readLine();
  22. }
  23. catch (IOException e) {
  24. System.out.println("Error!");
  25. }
  26.  
  27. xVal = Integer.parseInt(xinput);
  28.  
  29. System.out.print("Input y:");
  30. try {
  31. yinput = dataIn.readLine();
  32. }
  33. catch (IOException e) {
  34. System.out.println("Error!");
  35. }
  36.  
  37. yVal = Integer.parseInt(yinput);
  38.  
  39. int resultOutput = 0;
  40. resultOutput = addNum(xVal,yVal);
  41.  
  42. System.out.print("The sum is " + resultOutput);
  43. }}
Last edited by John A; Oct 3rd, 2009 at 5:42 pm. Reason: added code tags
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,467
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 267
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: simple java program

 
0
  #2
Oct 2nd, 2009
You need to take a third input, right? So start with that, then maybe you can figure out how to continue.
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 50
Reputation: coud_ren_26 has a little shameless behaviour in the past 
Solved Threads: 0
coud_ren_26's Avatar
coud_ren_26 coud_ren_26 is offline Offline
Junior Poster in Training

Re: simple java program

 
0
  #3
Oct 2nd, 2009
Yes thats what I'm planning to do but I do not know how to do the decision.
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,467
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 267
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: simple java program

 
0
  #4
Oct 2nd, 2009
An if statement? Using the equals method of String?
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 50
Reputation: coud_ren_26 has a little shameless behaviour in the past 
Solved Threads: 0
coud_ren_26's Avatar
coud_ren_26 coud_ren_26 is offline Offline
Junior Poster in Training

Re: simple java program

 
0
  #5
Oct 2nd, 2009
How will I do that?Please give me even a single line of code
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,467
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 267
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: simple java program

 
0
  #6
Oct 2nd, 2009
No. I am not going to do your homework for you. You already read lines from the user in that code and I can guarantee you've already used an if statement at somepoint in your course. You can at least make an attempt at doing your own hoemwork. Then again, I can pretty much guarantee that this isn't your code either, and so you are lost in the woods. Well, you've been given enough hints to at least be able to find the edges of those woods on your own, and you are going to have to do that before I give you anything more.
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 50
Reputation: coud_ren_26 has a little shameless behaviour in the past 
Solved Threads: 0
coud_ren_26's Avatar
coud_ren_26 coud_ren_26 is offline Offline
Junior Poster in Training

Re: simple java program

 
0
  #7
Oct 2nd, 2009
Thank You very much. Now I solved my problem. Thanks 'til next time
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC