Calculator program

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hebaa
    New Member
    • Oct 2007
    • 12

    Calculator program

    i want to make a java program (Netbeans) as a calculator that reads from the user 2 numbers and the user choose an operation from + - / * and the results appears with handling exceptions


    Plzz reply me ASAP

    Heba
    Egypt
    Thanksssssss
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    OK, that's very nice; what are you having trouble with? Have you started? Do you have errors you don't understand?

    Please make an attempt to solve your own problem before coming to us with questions.

    Comment

    • hebaa
      New Member
      • Oct 2007
      • 12

      #3
      Originally posted by Ganon11
      OK, that's very nice; what are you having trouble with? Have you started? Do you have errors you don't understand?

      Please make an attempt to solve your own problem before coming to us with questions.
      Yes, I've started but i cant make the program read the 2nd number, the program just ends...


      Would you help?


      [CODE=java]package Assignments;

      import java.io.Buffere dReader;
      import java.io.IOExcep tion;
      //import java.io.InputSt ream;
      import java.io.InputSt reamReader;

      public class Assignment1 {


      public static int sum(int x , int y){
      return x+y;}


      public static int min(int x, int y){
      return x-y;}


      public static int multiply(int x, int y){
      return x*y;}


      public static int divide(int x, int y){
      return x/y;}



      public static void main(String[] args) throws Exception {


      //try{

      BufferedReader in = new BufferedReader( new InputStreamRead er(System.in));
      //String s=in.readLine() ;
      int num1=in.read();

      if (num1==in.read( ))
      System.out.prin tln("Enter a Number");
      else {
      System.out.prin tln("Choose an operation (* + - /)");
      }

      char oper = (char) in.read();
      int num2=in.read();
      if( (num2==in.read( ))&& (oper==(char)in .read())){
      System.out.prin tln(num2); }
      //System.out.prin tln(in.read());



      }
      //else
      //System.out.prin tln("Choose a correct operation");


      //switch(oper){
      //case '+':
      //System.out.prin tln(sum(num1,nu m2));break;
      //case '-':
      //System.out.prin tln(min(num1,nu m2));break;
      //case '*':
      //System.out.prin tln(multiply(nu m1,num2));break ;
      //case '/':
      //System.out.prin tln(divide(num1 ,num2));break;
      //default:
      //System.out.prin tln("Please Enter a valide operation");









      //if(x!=0){

      //}

      //String ss=in.readLine( );
      //if(ss!=null){

      //String sss=in.readLine ();
      //if(sss!=null){
      //System.out.prin tln(s + ss + sss + " = ");
      //System.out.prin tln(sum(s, sss));
      //}




      //}





      //catch (Exception e) {
      //System.out.prin tln("Error");
      //System.out.prin tln();
      //System.out.prin tln("This program is like a calculator for certain operations (+ - * /)");//
      //}



      }[/CODE]
      Last edited by Ganon11; Oct 2 '07, 02:44 AM. Reason: Please use the [CODE] tags provided.

      Comment

      • Ganon11
        Recognized Expert Specialist
        • Oct 2006
        • 3651

        #4
        Originally posted by hebaa
        Yes, I've started but i cant make the program read the 2nd number, the program just ends...


        Would you help?


        [CODE=java]public static void main(String[] args) throws Exception {


        //try{

        BufferedReader in = new BufferedReader( new InputStreamRead er(System.in));
        //String s=in.readLine() ;
        int num1=in.read();

        if (num1==in.read( ))
        System.out.prin tln("Enter a Number");
        else {
        System.out.prin tln("Choose an operation (* + - /)");
        }

        char oper = (char) in.read();
        int num2=in.read();
        if( (num2==in.read( ))&& (oper==(char)in .read())){
        System.out.prin tln(num2); }
        //System.out.prin tln(in.read());



        }
        }
        [/CODE]
        Let's take a look at just your main function. Do you know how to use a BufferedReader to get input? If so, you should know that the .read() function does not return an integer.

        Not only that, but you are calling in.read() twice just for num1 - before you even output a prompt telling the user to enter a number!

        You need to re-think this portion of your code. Write down, step-by-step, what you need to do. Only once you have this figured out should you go back to your code and try to fix this program.

        Comment

        • hsn
          New Member
          • Sep 2007
          • 237

          #5
          you have to orgnize your way of entering the data that you need
          and in JAVA an input will always read it as a string.
          so you need to convert the input to a number.

          GOOD LUCK
          hsn

          Comment

          • hebaa
            New Member
            • Oct 2007
            • 12

            #6
            I dont know how to convert from String to int to be able to use the methos od summation and ...the others.

            String num1=in.readLin e();
            when i try the casting always says; cannot convert from string to int

            Comment

            • hebaa
              New Member
              • Oct 2007
              • 12

              #7
              switch(oper){
              case '+':
              System.out.prin tln(sum(num1,nu m2));break;
              case '-':
              System.out.prin tln(min(num1,nu m2));break;
              case '*':
              System.out.prin tln(multiply(nu m1,num2));break ;
              case '/':
              System.out.prin tln(divide(num1 ,num2));break;
              default:
              System.out.prin tln("Please Enter a valide operation");

              if(s!=readLine( )){
              System.out.prin tln("Enter a Number");
              }




              always says: cannot swtich on string

              Comment

              • JosAH
                Recognized Expert MVP
                • Mar 2007
                • 11453

                #8
                Originally posted by hebaa
                I dont know how to convert from String to int to be able to use the methos od summation and ...the others.

                String num1=in.readLin e();
                when i try the casting always says; cannot convert from string to int
                Note that a cast in Java never changes an object from one type to another type.
                Read the API docs for the Integer or/and Double classes. You have to *convert*
                your String line to an Integer or Double.

                kind regards,

                Jos

                Comment

                • hebaa
                  New Member
                  • Oct 2007
                  • 12

                  #9
                  I still dont know how to convert from string to int to be able to use a method that takes 2 int's as parameters.

                  public static int sum(int num1 , int num2){
                  return num1+num2;}


                  that's the method

                  in da main:
                  i tried:
                  String oper=in.readLin e();
                  String num2=in.readLin e();
                  System.out.prin tln("= ");

                  Integer.getInte ger(oper);
                  Integer.getInte ger(num1);
                  Integer.getInte ger(num2);
                  if( oper==""+"")
                  System.out.prin tln(sum(num1,nu m2));



                  dont know what to do .. please help

                  Comment

                  • hebaa
                    New Member
                    • Oct 2007
                    • 12

                    #10
                    Originally posted by hebaa
                    I still dont know how to convert from string to int to be able to use a method that takes 2 int's as parameters.

                    public static int sum(int num1 , int num2){
                    return num1+num2;}


                    that's the method

                    in da main:
                    i tried:
                    String oper=in.readLin e();
                    String num2=in.readLin e();
                    System.out.prin tln("= ");

                    Integer.getInte ger(oper);
                    Integer.getInte ger(num1);
                    Integer.getInte ger(num2);
                    if( oper==""+"")
                    System.out.prin tln(sum(num1,nu m2));



                    dont know what to do .. please help


                    i've tried
                    int x = Integer.parseIn t(oper);

                    but it doesnt fix my problem

                    Comment

                    • Ganon11
                      Recognized Expert Specialist
                      • Oct 2006
                      • 3651

                      #11
                      Originally posted by hebaa
                      i've tried
                      int x = Integer.parseIn t(oper);

                      but it doesnt fix my problem
                      Well, it's not going to work for oper, because oper is supposed to be '+', '-', '*', or '/'. None of these are numbers! You want to use Integer.parseIn t on num1 and num2.

                      Comment

                      • hebaa
                        New Member
                        • Oct 2007
                        • 12

                        #12
                        String oper=in.readLin e();


                        if(oper!="+"||o per!="-"||oper!="*"||o per!="/")
                        System.out.prin tln("Please Enter a valid operation");

                        the condition does not work...
                        what to do?

                        Comment

                        • Ganon11
                          Recognized Expert Specialist
                          • Oct 2006
                          • 3651

                          #13
                          Google String.java. Look at the very first result of that search. Look for a method calls equalsIgnoreCas e(). Use this instead of comparison operators.

                          Comment

                          • alexJavaRocks
                            New Member
                            • Oct 2007
                            • 4

                            #14
                            For the integer parsing problem, all you need to do is get the String, do
                            Code:
                            int answer = Integer.parseInt(answerString);
                            That should work. If it doesn't tell me and show me your code.

                            Comment

                            • hebaa
                              New Member
                              • Oct 2007
                              • 12

                              #15
                              Thank you everybody iam done with my code.

                              Heba :) thankssss

                              Comment

                              Working...