Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Java
  4. About Calculate....

About Calculate....

Scheduled Pinned Locked Moved Java
helpjava
15 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Khalil Adam
    wrote on last edited by
    #1

    // I need someone to help me fix this codes // it's about CALCULATING in Java, thanks alot...

    import java.util.Scanner; public class Calculate { static Scanner in = new Scanner(System.in); public static void main(String [] args) { String sign; int num1; int num2; int num3; int ans; System.out.print("Enter your Calculations => \n"); sign = in.nextLine(); if(args.Length == 3) { num1 = Integer.parseInt(args[0]); sign = (args [1].equal("1")); num2 = Integer.parseInt(args[2]); num3 = Integer.parseInt(args[3]); ans = num1 + num2; ans = num1 - num2; ans = num1 / num2; System.out.println("Answer = "+ ans); }else if(args[1].equal("+")) { ans = num1 + num2; System.out.println("num1 "+ "num2 " = ans); } else if(args[2].equal("-")) { ans = num1 - num2; System.out.println("num1 "- "num2 "= ans); } else if(args[3].equal("/")) { ans = num1 / num2; System.out.println("num1 "/ "num2 "= ans); } else if("Do it again => Calculate arg1, arg2, arg3"); } } }

    modified on Wednesday, January 19, 2011 9:58 AM

    C 1 Reply Last reply
    0
    • K Khalil Adam

      // I need someone to help me fix this codes // it's about CALCULATING in Java, thanks alot...

      import java.util.Scanner; public class Calculate { static Scanner in = new Scanner(System.in); public static void main(String [] args) { String sign; int num1; int num2; int num3; int ans; System.out.print("Enter your Calculations => \n"); sign = in.nextLine(); if(args.Length == 3) { num1 = Integer.parseInt(args[0]); sign = (args [1].equal("1")); num2 = Integer.parseInt(args[2]); num3 = Integer.parseInt(args[3]); ans = num1 + num2; ans = num1 - num2; ans = num1 / num2; System.out.println("Answer = "+ ans); }else if(args[1].equal("+")) { ans = num1 + num2; System.out.println("num1 "+ "num2 " = ans); } else if(args[2].equal("-")) { ans = num1 - num2; System.out.println("num1 "- "num2 "= ans); } else if(args[3].equal("/")) { ans = num1 / num2; System.out.println("num1 "/ "num2 "= ans); } else if("Do it again => Calculate arg1, arg2, arg3"); } } }

      modified on Wednesday, January 19, 2011 9:58 AM

      C Offline
      C Offline
      Cedric Moonen
      wrote on last edited by
      #2

      Please consider the following points: 1) Put your question inside the body of the message and keep a concise but precise title 2) Use the "code block" button to format your code so that it is readable 3) Provide the information which is required for us to understand the problem (in your case, what you are getting and what you were expecting). How can we guess what you had in mind when writing your code ? Related to your question: there are a couple of points which are wrong in your code. 1) You have three consecutive lines in which you assign a value to ans (num1+num2, num1-num2, num1/num2). Only the last value will be kept, since the other ones will be overriden (so, what's the point of having those lines anyway ?) 2) The sign variable is never used 3) If you don't fall in the first if, then you are using the num1 and num2 variables which are not initialized. 4) In the different else, you check for args[1], then args[2], then args[3] for the type of operation to execute. I would guess that it is always at the same position (args[1]). I suggest that you read some basic tutorial about java because it seems you are confused about some fundamental concepts.

      Cédric Moonen Software developer
      Charting control [v3.0] OpenGL game tutorial in C++

      K 1 Reply Last reply
      0
      • C Cedric Moonen

        Please consider the following points: 1) Put your question inside the body of the message and keep a concise but precise title 2) Use the "code block" button to format your code so that it is readable 3) Provide the information which is required for us to understand the problem (in your case, what you are getting and what you were expecting). How can we guess what you had in mind when writing your code ? Related to your question: there are a couple of points which are wrong in your code. 1) You have three consecutive lines in which you assign a value to ans (num1+num2, num1-num2, num1/num2). Only the last value will be kept, since the other ones will be overriden (so, what's the point of having those lines anyway ?) 2) The sign variable is never used 3) If you don't fall in the first if, then you are using the num1 and num2 variables which are not initialized. 4) In the different else, you check for args[1], then args[2], then args[3] for the type of operation to execute. I would guess that it is always at the same position (args[1]). I suggest that you read some basic tutorial about java because it seems you are confused about some fundamental concepts.

        Cédric Moonen Software developer
        Charting control [v3.0] OpenGL game tutorial in C++

        K Offline
        K Offline
        Khalil Adam
        wrote on last edited by
        #3

        thanks alot Mr. Cedric the reason for this code is that l wonted to do CALCULATING IN JAVA? l understand u completely but for now,how should l make the code work(l mean to initialize it)and not to fall back the way l was before? thanks for ur alert for the tutorials.please help me for this one...

        modified on Wednesday, January 19, 2011 9:57 AM

        C 1 Reply Last reply
        0
        • K Khalil Adam

          thanks alot Mr. Cedric the reason for this code is that l wonted to do CALCULATING IN JAVA? l understand u completely but for now,how should l make the code work(l mean to initialize it)and not to fall back the way l was before? thanks for ur alert for the tutorials.please help me for this one...

          modified on Wednesday, January 19, 2011 9:57 AM

          C Offline
          C Offline
          Cedric Moonen
          wrote on last edited by
          #4

          You can already fix the points I mentionned in my previous message, this was detailed enough for you to understand the problems. You don't expect us to write the program for you, do you ?

          Cédric Moonen Software developer
          Charting control [v3.0] OpenGL game tutorial in C++

          K 2 Replies Last reply
          0
          • C Cedric Moonen

            You can already fix the points I mentionned in my previous message, this was detailed enough for you to understand the problems. You don't expect us to write the program for you, do you ?

            Cédric Moonen Software developer
            Charting control [v3.0] OpenGL game tutorial in C++

            K Offline
            K Offline
            Khalil Adam
            wrote on last edited by
            #5

            thanks again.. l dont wont u to write all the program for for but to guide me Fix the errors is all what l need..but thanks alot for ur time becouse this's my first time of sending a code in this forum...

            modified on Wednesday, January 19, 2011 10:02 AM

            L 1 Reply Last reply
            0
            • K Khalil Adam

              thanks again.. l dont wont u to write all the program for for but to guide me Fix the errors is all what l need..but thanks alot for ur time becouse this's my first time of sending a code in this forum...

              modified on Wednesday, January 19, 2011 10:02 AM

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              Try here[^] for some good tutorials that will help you with your problem.

              I must get a clever new signature for 2011.

              1 Reply Last reply
              0
              • C Cedric Moonen

                You can already fix the points I mentionned in my previous message, this was detailed enough for you to understand the problems. You don't expect us to write the program for you, do you ?

                Cédric Moonen Software developer
                Charting control [v3.0] OpenGL game tutorial in C++

                K Offline
                K Offline
                Khalil Adam
                wrote on last edited by
                #7

                Mr. Cedric please a question? 1- how should make the sign veriable work and? 2- how to initialize those to variables( num1 and num2)?

                C 1 Reply Last reply
                0
                • K Khalil Adam

                  Mr. Cedric please a question? 1- how should make the sign veriable work and? 2- how to initialize those to variables( num1 and num2)?

                  C Offline
                  C Offline
                  Cedric Moonen
                  wrote on last edited by
                  #8

                  Initialize your variables at the beginning of the program by reading ONCE the command line arguments: only do this of course if the number of arguments match, otherwise you exit the program with an error. BTW, you have another problem: you are expecting 3 command line arguments but you retrieve four of them (num3 is initialized with args[3], which is out of bounds).

                  Cédric Moonen Software developer
                  Charting control [v3.0] OpenGL game tutorial in C++

                  K 1 Reply Last reply
                  0
                  • C Cedric Moonen

                    Initialize your variables at the beginning of the program by reading ONCE the command line arguments: only do this of course if the number of arguments match, otherwise you exit the program with an error. BTW, you have another problem: you are expecting 3 command line arguments but you retrieve four of them (num3 is initialized with args[3], which is out of bounds).

                    Cédric Moonen Software developer
                    Charting control [v3.0] OpenGL game tutorial in C++

                    K Offline
                    K Offline
                    Khalil Adam
                    wrote on last edited by
                    #9

                    //l've change the code but still 1 error has been occur public static void main(String [] args) { String sign; int num1; int num2; //int num3; int ans; System.out.print("Enter your Calculations =>: \n"); sign = in.nextLine(); if(args.Length == 3) { num1 = Integer.parseInt(args[0]); sign = (args [1].equal("1")); num2 = Integer.parseInt(args[2]); //num3 = Integer.parseInt(args[3]); System.out.println("Answer = "+ ans); }else if(args[0].equal("+")) { ans1 = num1 + num2; System.out.println("num1 "+ "num2 " = ans); } else if(args[1].equal("-")) { ans2 = num1 - num2; System.out.println("num1 "- "num2 "= ans); } else if(args[2].equal("/")) { ans3 = num1 / num2; System.out.println("num1 "/ "num2 "= ans); } System.err.println("Try Again"); } } }

                    C 1 Reply Last reply
                    0
                    • K Khalil Adam

                      //l've change the code but still 1 error has been occur public static void main(String [] args) { String sign; int num1; int num2; //int num3; int ans; System.out.print("Enter your Calculations =>: \n"); sign = in.nextLine(); if(args.Length == 3) { num1 = Integer.parseInt(args[0]); sign = (args [1].equal("1")); num2 = Integer.parseInt(args[2]); //num3 = Integer.parseInt(args[3]); System.out.println("Answer = "+ ans); }else if(args[0].equal("+")) { ans1 = num1 + num2; System.out.println("num1 "+ "num2 " = ans); } else if(args[1].equal("-")) { ans2 = num1 - num2; System.out.println("num1 "- "num2 "= ans); } else if(args[2].equal("/")) { ans3 = num1 / num2; System.out.println("num1 "/ "num2 "= ans); } System.err.println("Try Again"); } } }

                      C Offline
                      C Offline
                      Cedric Moonen
                      wrote on last edited by
                      #10

                      Please refer to the first part of my initial message (code formating and providing information about your problem). You should put your else conditions inside the first if (and replace the first else condition by an else condition). In your example you are checking if you have 3 arguments and only if that's not the case you will check the sign argument.

                      Cédric Moonen Software developer
                      Charting control [v3.0] OpenGL game tutorial in C++

                      K 2 Replies Last reply
                      0
                      • C Cedric Moonen

                        Please refer to the first part of my initial message (code formating and providing information about your problem). You should put your else conditions inside the first if (and replace the first else condition by an else condition). In your example you are checking if you have 3 arguments and only if that's not the case you will check the sign argument.

                        Cédric Moonen Software developer
                        Charting control [v3.0] OpenGL game tutorial in C++

                        K Offline
                        K Offline
                        Khalil Adam
                        wrote on last edited by
                        #11

                        Mr. Cedric, I think u've tried alot best for me but l did try what u said and l got 3 error. anyway,l appreciate very well for ur help since am a beginner in java and once l cant compile this code those not make me to stop learning?please if u get anytime,try and correct this code for another people to lean from it? remain bless always from God.. I'll go back and learn the tutorials...thanks.

                        L T 2 Replies Last reply
                        0
                        • K Khalil Adam

                          Mr. Cedric, I think u've tried alot best for me but l did try what u said and l got 3 error. anyway,l appreciate very well for ur help since am a beginner in java and once l cant compile this code those not make me to stop learning?please if u get anytime,try and correct this code for another people to lean from it? remain bless always from God.. I'll go back and learn the tutorials...thanks.

                          L Offline
                          L Offline
                          Lost User
                          wrote on last edited by
                          #12

                          The tutorials (see the link I provided) will get you to the answers faster and better than struggling with the above sample. You obviously have some gaps in your knowledge of some of the basic concepts, and should learn and understand those first before you try to write a program like the one above. Starting at the beginning will get you up to speed much faster than trying to start from the middle.

                          I must get a clever new signature for 2011.

                          1 Reply Last reply
                          0
                          • K Khalil Adam

                            Mr. Cedric, I think u've tried alot best for me but l did try what u said and l got 3 error. anyway,l appreciate very well for ur help since am a beginner in java and once l cant compile this code those not make me to stop learning?please if u get anytime,try and correct this code for another people to lean from it? remain bless always from God.. I'll go back and learn the tutorials...thanks.

                            T Offline
                            T Offline
                            TorstenH
                            wrote on last edited by
                            #13

                            please if u get anytime,try and correct this code for another people to lean from it? come on! If you're just searching for the code, go to google. This is a simple code used in training throughout the world, nearly every learner has to do this task at a certain point while learning. How are you trying to compile the code? You should use eclipse or netbeans - those are freeware and should help you a lot along your way. What do those editors say if you try to compile your code? Most times it should be fairly easy to figure out the errors. Even automatic help is often granted! regards Torsten

                            I never finish anyth...

                            K 1 Reply Last reply
                            0
                            • T TorstenH

                              please if u get anytime,try and correct this code for another people to lean from it? come on! If you're just searching for the code, go to google. This is a simple code used in training throughout the world, nearly every learner has to do this task at a certain point while learning. How are you trying to compile the code? You should use eclipse or netbeans - those are freeware and should help you a lot along your way. What do those editors say if you try to compile your code? Most times it should be fairly easy to figure out the errors. Even automatic help is often granted! regards Torsten

                              I never finish anyth...

                              K Offline
                              K Offline
                              Khalil Adam
                              wrote on last edited by
                              #14

                              l've done it the code has compiled?

                              1 Reply Last reply
                              0
                              • C Cedric Moonen

                                Please refer to the first part of my initial message (code formating and providing information about your problem). You should put your else conditions inside the first if (and replace the first else condition by an else condition). In your example you are checking if you have 3 arguments and only if that's not the case you will check the sign argument.

                                Cédric Moonen Software developer
                                Charting control [v3.0] OpenGL game tutorial in C++

                                K Offline
                                K Offline
                                Khalil Adam
                                wrote on last edited by
                                #15

                                Mr. Cedric Moonen :) bellow is the code that l did compile and it works perfectly..thanks alot..

                                public class Calculate { public static void main(String[] args) { double x,y; if (args.length == 3) { x = Double.parseDouble(args[0]); y = Double.parseDouble(args[2]); if(args[1].equals("+")) { System.out.println("Answer = "+ (x + y)); }else if(args[1].equals("-")) { System.out.println("Answer = "+ (x - y)); }else if(args[1].equals("/")) { System.out.println("Answer = "+ (x / y)); }else if(args[1].equals("x")) { System.out.println("Answer = "+ (x * y)); } }else System.out.println("Do it again......"); } }

                                1 Reply Last reply
                                0
                                Reply
                                • Reply as topic
                                Log in to reply
                                • Oldest to Newest
                                • Newest to Oldest
                                • Most Votes


                                • Login

                                • Don't have an account? Register

                                • Login or register to search.
                                • First post
                                  Last post
                                0
                                • Categories
                                • Recent
                                • Tags
                                • Popular
                                • World
                                • Users
                                • Groups