Please help a beginner java programmer!

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

    #16
    Originally posted by JosAH
    Can you show me the *exact* code snippet again? There's must've been some
    bit rot happening when you copied/pasted my code. All I wanted you to see is
    what the values of the individual argument elements are; no more no less.

    kind regards,

    Jos
    Here is my code [CODE]
    class calculator {
    public static void main(String[] arguments) {

    for (int i= 0; i < arguments.lengt h; i++)
    System.out.prin tln("arguments["+i"]="+arguments[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

      #17
      See my previous reply; I goofed in my code snippet; above you can see the
      corrected version; sorry for the inconvenience.

      kind regards,

      Jos

      Comment

      • Hazza
        New Member
        • Oct 2007
        • 22

        #18
        Originally posted by JosAH
        See my previous reply; I goofed in my code snippet; above you can see the
        corrected version; sorry for the inconvenience.

        kind regards,

        Jos
        Sorry I didn't see that, it now compiles correctly. All the functions except * work, and I get lists of he arguments used for those. When I use * I get 13 arguments,with [0] as the first number i typed in, [13] as the last number, and all the ones in between as random .java and .class files in the folder of calculator. I am new so i'm probably asking stupid questions but - Why is this? and How do i get it to work?
        Thanks for your help

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #19
          Originally posted by Hazza
          Sorry I didn't see that, it now compiles correctly. All the functions except * work, and I get lists of he arguments used for those. When I use * I get 13 arguments,with [0] as the first number i typed in, [13] as the last number, and all the ones in between as random .java and .class files in the folder of calculator. I am new so i'm probably asking stupid questions but - Why is this? and How do i get it to work?
          Thanks for your help
          Open up a command (cmd) window and type 'dir *'. What do you see? What did
          the star '*' do?

          kind regards,

          Jos

          Comment

          • Hazza
            New Member
            • Oct 2007
            • 22

            #20
            Originally posted by JosAH
            Open up a command (cmd) window and type 'dir *'. What do you see? What did
            the star '*' do?

            kind regards,

            Jos
            Oh- it runs through my files?
            Ok - so i cannot use that operator in this way?

            Comment

            • Hazza
              New Member
              • Oct 2007
              • 22

              #21
              Originally posted by Hazza
              Oh- it runs through my files?
              Ok - so i cannot use that operator in this way?
              Sorry I cannot respond for about 20 mins

              Comment

              • JosAH
                Recognized Expert MVP
                • Mar 2007
                • 11453

                #22
                Originally posted by Hazza
                Sorry I cannot respond for about 20 mins
                No need to worry; this is not a chat box (although it looks like it sometimes ;-)
                For now the easiest way out use a lower case 'x' character for the multiplication.
                I think you don't want to do it the hard way now.

                kind regards,

                Jos

                Comment

                • Hazza
                  New Member
                  • Oct 2007
                  • 22

                  #23
                  Originally posted by JosAH
                  No need to worry; this is not a chat box (although it looks like it sometimes ;-)
                  For now the easiest way out use a lower case 'x' character for the multiplication.
                  I think you don't want to do it the hard way now.

                  kind regards,

                  Jos
                  Thanks a lot!!!! :-)

                  Comment

                  • Hazza
                    New Member
                    • Oct 2007
                    • 22

                    #24
                    Sorry but I have another query about the same program.
                    I have now refined it, and want to use switch programs to give an error message if the user doesn't type in the exact things required.
                    Here is the code
                    [CODE]


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


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

                    switch (numbera) {

                    case float:{

                    switch (numberb) {

                    case float:{

                    switch (sign) {

                    case '+':{

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

                    case '-':{

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

                    case 'x':{

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


                    case '/':{

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

                    default:


                    System.out.prin tln("Please enter two numbers with a mathematical operator between them, seperated by spaces");


                    }}

                    default:


                    System.out.prin tln("Please enter two numbers with a mathematical operator between them, seperated by spaces");

                    }}

                    default:


                    System.out.prin tln("Please enter two numbers with a mathematical operator between them, seperated by spaces");

                    }

                    }
                    }



                    [CODE]


                    There are two error messages for the two case float:{ (the command prompt says '.class' expected) but I am not sure how to make it so that it is in the case of numbera and numberb not being floats run the default statement.
                    Thanks to anyone who helps

                    Comment

                    • JosAH
                      Recognized Expert MVP
                      • Mar 2007
                      • 11453

                      #25
                      [QUOTE=Hazza]Sorry but I have another query about the same program.
                      I have now refined it, and want to use switch programs to give an error message
                      if the user doesn't type in the exact things required.
                      Here is the code

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


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

                      switch (numbera) {

                      case float:{

                      switch (numberb) {

                      case float:{

                      switch (sign) {

                      [ ... ]
                      [/code]

                      I don't know what you're trying to do there.
                      The parseFloat() method throws an Exception when the String isn't a valid representation
                      of a floating point number. If it doesn't do that, you can be sure that your numbera
                      (and numberb) contain valid floating point values. If it does throw an exception you
                      can be sure that the user didn't supply a valid expression; you can 'catch' the
                      exception and tell the user so. You can even tell the user that s/he didn't enter
                      a valid operator.

                      Don't try to squeeze everything into that single main method. Add your own methods
                      to do part of the work. Especially getting a floating point number begs for its own
                      little method:

                      [code=java]
                      private float getFloat(String string) {

                      try {
                      return ... ; // parse the string and return the float result
                      }
                      catch (NumberFormatEx ception nfe) {
                      // tell the user /she goofed and:
                      System.exit(1);
                      }
                      }
                      [/code]

                      Your turn now ...

                      kind regards,

                      Jos

                      Comment

                      • Hazza
                        New Member
                        • Oct 2007
                        • 22

                        #26
                        Thanks for your help

                        Comment

                        • JosAH
                          Recognized Expert MVP
                          • Mar 2007
                          • 11453

                          #27
                          Originally posted by Hazza
                          Thanks for your help
                          You're welcome of course. Can you manage from here? Remember: better define
                          another (simple) method instead of trying to cram more and more complicated
                          control flow logic in one method (such as the main() method).

                          kind regards,

                          Jos

                          Comment

                          Working...