Please help a beginner java programmer!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Hazza
    New Member
    • Oct 2007
    • 22

    #1

    Please help a beginner java programmer!

    Hello,
    I am teaching myself java and am completely stuck. I am using the java development kit and vista command prompt to compile and interpret programmes. I am getting error codes which don't make sense because they refer to a code that seems all right to me. Here is the code:
    [CODE=java]
    class calculator {
    public static void main(String[] arguments) {
    float numbera = 100;
    float numberb = 200;

    if (arguments.leng th > 0) {
    numbera = Float.parseFloa t(arguments[0]);
    sign = Char.parseChar (arguments[1]);
    numberb = Float.parseFloa t(arguments[2]);
    }

    if (sign == +)


    System.out.prin tln(numbera
    + "added to "
    + numberb
    + " is "
    + (numbera + numberb));

    if (sign == -)


    System.out.prin tln(numbera
    + "subtract "
    + numberb
    + " is "
    + (numbera - numberb));

    if (sign == *)


    System.out.prin tln(numbera
    + "multiplied by "
    + numberb
    + " is "
    + (numbera * numberb));

    if (sign == /)


    System.out.prin tln(numbera
    + "divided by "
    + numberb
    + " is "
    + (numbera / numberb));

    }
    }

    [/CODE]

    I am getting 6 error codes, all saying "illegal start of expression". They are at:

    The last bracket of [CODE=java] if (sign == +) [/CODE]
    The last bracket of [CODE=java] if (sign == -) [/CODE]
    The multiplication sign of [CODE=java] if (sign == *) [/CODE]
    The last bracket of [CODE=java] if (sign == *) [/CODE]
    The division sign of [CODE=java] if (sign == /) [/CODE]
    The last bracket of [CODE=java] if (sign == /) [/CODE]

    I have been trying for a long time to find a solution but to no avail. Please could someone help me?.
    Thank you
    Last edited by Ganon11; Oct 20 '07, 05:24 PM. Reason: Fixing [CODE] tags.
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    The +, -, *, and / characters alone are arithmetic operators. If you want to compare sign to the characters, use '+', '-', '*', or '/', which are the characters, not the operators.

    Comment

    • Hazza
      New Member
      • Oct 2007
      • 22

      #3
      I realised that I should have used if else statements. Here is the revised code
      [CODE=java]
      class calculator {
      public static void main(String[] arguments) {
      float numbera = 100;
      float numberb = 200;

      if (arguments.leng th > 0) {
      numbera = Float.parseFloa t(arguments[0]);
      sign = Char.parseChar (arguments[1]);
      numberb = Float.parseFloa t(arguments[2]);
      }



      if (sign == +) {


      System.out.prin tln(numbera
      + "added to "
      + numberb
      + " is "
      + (numbera + numberb));}

      else if (sign == -){


      System.out.prin tln(numbera
      + "subtract "
      + numberb
      + " is "
      + (numbera - numberb));}

      else if (sign == *){


      System.out.prin tln(numbera
      + "multiplied by "
      + numberb
      + " is "
      + (numbera * numberb));}

      else if (sign == /){


      System.out.prin tln(numbera
      + "divided by "
      + numberb
      + " is "
      + (numbera / numberb));}

      }
      }

      [/CODE]

      I am still getting six error messages for "illegal start of expression at for the last bracket ( this type of bracket )) of the if ( concerning addition) statement,the last bracket ( this type of bracket )) of the first else if ( concerning subtraction) statement,the last bracket ( this type of bracket )) and multiplication sign (*) of the second else if ( concerning multiplication) statement, and the last bracket ( this type of bracket )) and division sign of the last else if ( concerning division) statement,
      When I say last bracket (of this type)) I meant on the line with the statement,
      if (sign == +) {,
      not after the rest of it. Thank you in advance to anyone who helps

      Comment

      • Hazza
        New Member
        • Oct 2007
        • 22

        #4
        It still doesn't work

        Sorry Ganon I posted the second thing before I saw your reply. here is the code i am now using [CODE=java]

        class calculator {
        public static void main(String[] arguments) {
        float numbera = 100;
        float numberb = 200;

        if (arguments.leng th > 0) {
        numbera = Float.parseFloa t(arguments[0]);
        sign = Char.parseChar (arguments[1]);
        numberb = Float.parseFloa t(arguments[2]);
        }



        if (sign == '+')


        System.out.prin tln(numbera
        + "added to "
        + numberb
        + " is "
        + (numbera + numberb));

        else if (sign == '-')


        System.out.prin tln(numbera
        + "subtract "
        + numberb
        + " is "
        + (numbera - numberb));

        else if (sign == '*')


        System.out.prin tln(numbera
        + "multiplied by "
        + numberb
        + " is "
        + (numbera * numberb));

        else if (sign == '/')


        System.out.prin tln(numbera
        + "divided by "
        + numberb
        + " is "
        + (numbera / numberb));

        }
        }


        [/CODE]
        now the errors i am getting are 6 cannot find symbol errors.
        (
        cannot find symbol
        symbol : variable (depends)
        location: class calculator
        They look like this.
        1. Its the sign in the
        Code:
         sign = Char.parseChar (arguments[1]);
        statement
        2. The first Char in the
        Code:
         sign = Char.parseChar (arguments[1]);
        statement
        3. And all the signs in the if and else if statements (example
        Code:
         else if (sign == '-')

        Comment

        • Hazza
          New Member
          • Oct 2007
          • 22

          #5
          I got rid of all but one of the errors by declaring the "sign" variable at the beginning as a char variable so the first few lines look like this:
          [CODE=java]

          class calculator {
          public static void main(String[] arguments) {
          float numbera = 100;
          float numberb = 200;
          char sign = '+';
          [/CODE]

          I also changed Char to Character.parse Char
          The only error that remains is;
          Cannot find symbol
          Symbol = method parseChar(java. lang.String)
          location = class java.lang.Strin g)
          sign = Character.parse Char (arguments[1]);
          with the little hat under the full stop in the last line (vista comman prompt)
          Thanks in advance

          Comment

          • Ganon11
            Recognized Expert Specialist
            • Oct 2006
            • 3651

            #6
            If you know that arguments[1] has the sign, why not use the .charAt() function to grab the first character of that String? This function returns a character, so there's no need to use parseChar.

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #7
              Originally posted by Ganon11
              If you know that arguments[1] has the sign, why not use the .charAt() function to grab the first character of that String? This function returns a character, so there's no need to use parseChar.
              ... or alternatively make variable sign a String and do this:

              [code=java]
              String sign= arguments[1];

              if (sign.equals("+ ")) { ... }
              if (sign.equals("-")) { ... }
              if (sign.equals("* ")) { ... }
              if (sign.equals("/")) { ... }
              [/code]

              kind regards,

              Jos

              Comment

              • Hazza
                New Member
                • Oct 2007
                • 22

                #8
                Thanks Ganon - now everything works except multiplication.
                I am mystified to why this is, because the code is exactly the same to the other mathematical functions.
                Here is my full code [CODE]

                class calculator {
                public static void main(String[] arguments) {
                float numbera = 100;
                float numberb = 200;
                char sign = '*';

                numbera = Float.parseFloa t(arguments[0]);
                sign = arguments[1].charAt(0);
                numberb = Float.parseFloa t(arguments[2]);





                if (sign == '+') {


                System.out.prin tln(numbera
                + " added to "
                + numberb
                + " is "
                + (numbera + numberb));}

                else if (sign == '-'){


                System.out.prin tln(numbera
                + " subtract "
                + numberb
                + " is "
                + (numbera - numberb));}

                else if (sign == '*'){


                System.out.prin tln(numbera
                + " multiplied by "
                + numberb
                + " is "
                + (numbera * numberb));}


                else if (sign == '/'){


                System.out.prin tln(numbera
                + " divided by "
                + numberb
                + " is "
                + (numbera / numberb));}

                }
                }

                [CODE]

                I can type in java calculator 6 + 8 or 8 / 5 or 78 - 5 but not anything with a multiplication sign in it like 8 * 7.
                The error message looks like this.
                Exception in thread "main" java.lang.Numbe rFormatExceptin : For input string "addition.j ava"
                at sun.misc.Floati ngDecimal.readJ avaFormatString (Unknown Source)
                at java.lang.Float .parseFloat(Unk nown source)
                at calculator.main (test.java:10)

                It says "addtion.ja va" in the error message, and I do have a file in the folder of this progrm, but there is no reference to either to either program. In case it should help, here is the code for it, which is a prototype for calculator:
                [CODE]
                class addition {
                public static void main(String[] arguments) {
                float numbera = 100;
                float numberb = 200;
                if (arguments.leng th > 0) {
                numbera = Float.parseFloa t(arguments[0]);
                numberb = Float.parseFloa t(arguments[1]);

                }
                System.out.prin tln(numbera
                + " added to "
                + numberb
                + " is "
                + (numbera + numberb) );
                }
                }
                [CODE]
                Thanks

                Comment

                • JosAH
                  Recognized Expert MVP
                  • Mar 2007
                  • 11453

                  #9
                  Originally posted by Hazza
                  Thanks Ganon - now everything works except multiplication.
                  I am mystified to why this is, because the code is exactly the same to the other mathematical functions.

                  <code snipped>

                  I can type in java calculator 6 + 8 or 8 / 5 or 78 - 5 but not anything with a multiplication sign in it like 8 * 7.
                  The error message looks like this.
                  Exception in thread "main" java.lang.Numbe rFormatExceptin : For input string "addition.j ava"
                  at sun.misc.Floati ngDecimal.readJ avaFormatString (Unknown Source)
                  Add the following statement at the beginning of your main method and you'll
                  see why a single '*' on the command line won't work as you expected:

                  [code=java]
                  for (int i= 0; i < argument.length ; i++)
                  System.out.prin tln("argument["+i"]="+argument[i]);
                  [/code]

                  kind regards,

                  Jos
                  Last edited by JosAH; Oct 21 '07, 08:15 AM. Reason: changed new style loop to old style loop

                  Comment

                  • Hazza
                    New Member
                    • Oct 2007
                    • 22

                    #10
                    Sorry, I don't quite understand - where do i put that statement in my code and what does it do?

                    If i put your code directly after the public static void main(String[] arguments) {
                    I get 3 error messages saying that there is a ; expected (with the hat under the last ) of your code), that it is not a statement with the hat under the last put one square bracket, and that a normal bracket is expected, with the hat under the thrid ".
                    Thanks for your help

                    Comment

                    • JosAH
                      Recognized Expert MVP
                      • Mar 2007
                      • 11453

                      #11
                      Originally posted by Hazza
                      Sorry, I don't quite understand - where do i put that statement in my code and what does it do?

                      If i put your code directly after the public static void main(String[] arguments) {
                      I get 3 error messages saying that there is a ; expected (with the hat under the last ) of your code), that it is not a statement with the hat under the last put one square bracket, and that a normal bracket is expected, with the hat under the thrid ".
                      Thanks for your help
                      Do this:

                      [code=java]
                      public static void main(String[] argument) {
                      for (int i= 0; i < argument.length ; i++)
                      System.out.prin tln("argument["+i"]="+argument[i]);

                      // your code goes here
                      // ...
                      [/code]

                      kind regards,

                      Jos

                      Comment

                      • Hazza
                        New Member
                        • Oct 2007
                        • 22

                        #12
                        I did that and got the same error messages.

                        Here is my complete code (with yours included)
                        [CODE]

                        class calculator {
                        public static void main(String[] arguments) {


                        for (int i= 0; i < argument.length ; i++)
                        System.out.prin tln("argument["+i"]="+argument[i]);



                        float numbera = 100;
                        float numberb = 200;
                        char sign = '+';

                        numbera = Float.parseFloa t(arguments[0]);
                        sign = arguments[1].charAt(0);
                        numberb = Float.parseFloa t(arguments[2]);

                        if (sign == '+') {


                        System.out.prin tln(numbera
                        + " added to "
                        + numberb
                        + " is "
                        + (numbera + numberb));}

                        else if (sign == '-'){


                        System.out.prin tln(numbera
                        + " subtract "
                        + numberb
                        + " is "
                        + (numbera - numberb));}

                        else if (sign == '*'){



                        System.out.prin tln(numbera
                        + " multiplied by "
                        + numberb
                        + " is "
                        + (numbera * numberb));}


                        else if (sign == '/'){


                        System.out.prin tln(numbera
                        + " divided by "
                        + numberb
                        + " is "
                        + (numbera / numberb));}

                        }
                        }

                        [CODE]
                        Thanks for your help

                        Comment

                        • JosAH
                          Recognized Expert MVP
                          • Mar 2007
                          • 11453

                          #13
                          Originally posted by Hazza
                          I did that and got the same error messages.

                          Here is my complete code (with yours included)
                          [CODE=java]
                          public static void main(String[] arguments) {


                          for (int i= 0; i < argument.length ; i++)
                          System.out.prin tln("argument["+i"]="+argument[i]);
                          [/CODE]
                          Thanks for your help
                          Duh, my mistake: you named your command line arguments 'arguments', not
                          'argument' as I did; make that code snippet like this:

                          [code=java]
                          public static void main(String[] arguments) {


                          for (int i= 0; i < arguments.lengt h; i++)
                          System.out.prin tln("arguments["+i"]="+arguments[i]);
                          [/CODE]

                          kind regards,

                          Jos

                          Comment

                          • Hazza
                            New Member
                            • Oct 2007
                            • 22

                            #14
                            i updated my code and i am still getting the same error messages
                            thanks

                            Comment

                            • JosAH
                              Recognized Expert MVP
                              • Mar 2007
                              • 11453

                              #15
                              Originally posted by Hazza
                              i updated my code and i am still getting the same error messages
                              thanks
                              Found it; silly me: I should be just lazy on a Sunday morning; not posting wrong
                              code; change that println statement to this:

                              [code=java]
                              System.out.prin tln("arguments["+i+"]="+arguments[i]);
                              [/code]

                              Note that extra '+' sign I forgot in the previous (incorrect) versions.

                              kind regards,

                              Jos

                              Comment

                              Working...