Java method to check variable (counter) max length

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #16
    Originally posted by javaalien
    Need an assistance on how to put this kind of alert in my program...
    Lat's say I assign a counter which suppose to count how many word 'A' in a text file. So how am I supposed to do to alert me when it's already achieve max limit? I can't wait until the execution to finish and returns -ve value or false value.
    So when it reach int limit (2^31-1), it suppose stop the execution (or at least pop up an alert or etc). Is this possible to do? Thank you in advance.

    note: Stick to Integer type.
    Use a long counter then; if a certain word occurs more than n times; the word
    has to present in the text for at least 2^63-1 times. Note that if everybody on the
    world owns more than a couple of thousand books than for every book (~ 200 pages)
    the counter won't overflow if it occurred more than tens of times on every page
    on every book.

    kind regards,

    Jos

    Comment

    • r035198x
      MVP
      • Sep 2006
      • 13225

      #17
      Originally posted by javaalien
      Need an assistance on how to put this kind of alert in my program...
      Lat's say I assign a counter which suppose to count how many word 'A' in a text file. So how am I supposed to do to alert me when it's already achieve max limit? I can't wait until the execution to finish and returns -ve value or false value.
      So when it reach int limit (2^31-1), it suppose stop the execution (or at least pop up an alert or etc). Is this possible to do? Thank you in advance.

      note: Stick to Integer type.
      Have a look at the JOPtionPane class .

      Comment

      • javaalien
        New Member
        • May 2007
        • 34

        #18
        Originally posted by JosAH
        Use a long counter then; if a certain word occurs more than n times; the word
        has to present in the text for at least 2^63-1 times. Note that if everybody on the
        world owns more than a couple of thousand books than for every book (~ 200 pages)
        the counter won't overflow if it occurred more than tens of times on every page
        on every book.

        kind regards,

        Jos
        Thank you.
        If my rewrite my question to long type, how am I going to solve it then?
        It just that I can't wait for it to keep counting after exceed limit , better I stop the execution or give some kind of alert.

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #19
          Originally posted by javaalien
          Thank you.
          If my rewrite my question to long type, how am I going to solve it then?
          It just that I can't wait for it to keep counting after exceed limit , better I stop the execution or give some kind of alert.
          You do realize that with a maximum numer as big as 2^63-1 you will be able to
          count every thousands of a millimetre for a journey starting at the Sun and
          ending at Pluto do you?

          kind regards,

          Jos

          Comment

          • javaalien
            New Member
            • May 2007
            • 34

            #20
            Originally posted by JosAH
            You do realize that with a maximum numer as big as 2^63-1 you will be able to
            count every thousands of a millimetre for a journey starting at the Sun and
            ending at Pluto do you?

            kind regards,

            Jos
            Ok Jos, It's a caution matter to me, I do need to put something that if the thing is going to happen..not more than that.

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #21
              Originally posted by javaalien
              Ok Jos, It's a caution matter to me, I do need to put something that if the thing is going to happen..not more than that.
              Be realistic and do your maths; files that don't exist while they're supposed to;
              sockets that won't connect while the server should be up and running; headless
              machines running your application; some loonie user switching your computer
              off in the middle of some sort of transaction; spilling cola all over your keyboard
              while some critical code is running; power failures while new batteries fail too
              and a UPS burn out at the same time; half of your RAM memory failing because
              of a bad contact caused by one single pin; your mother in law burning the one
              and only source code listing with her cigar; sending all backup disks into orbit
              (including your workdisk); eclipse wiping out all your sources because of a cvs
              failure; a nuclear bomb on your office; EMP into your network connection cable;
              the sky falling down of everybody's head.

              *Everything* is more likely to make your application fail than an overflow in your
              long counter. Don't even implement a JOptionPane message for that.

              kind regards,

              Jos

              Comment

              • r035198x
                MVP
                • Sep 2006
                • 13225

                #22
                Originally posted by JosAH
                You do realize that with a maximum numer as big as 2^63-1 you will be able to
                count every thousands of a millimetre for a journey starting at the Sun and
                ending at Pluto do you?

                kind regards,

                Jos
                If you don't mind to please show your working and assumed distances.

                Comment

                • JosAH
                  Recognized Expert MVP
                  • Mar 2007
                  • 11453

                  #23
                  Originally posted by r035198x
                  If you don't mind to please show your working and assumed distances.
                  Sure, I'd be glad to:

                  |Sun, Pluto| ~= 6e12 metres

                  6e12/(2^63) ~= 6.5e-7 metres ~= 6.5e-4 millimetres < 1/1000 millimetres.

                  kind regards,

                  Jos

                  Comment

                  • r035198x
                    MVP
                    • Sep 2006
                    • 13225

                    #24
                    Originally posted by JosAH
                    Be realistic and do your maths; files that don't exist while they're supposed to;
                    sockets that won't connect while the server should be up and running; headless
                    machines running your application; some loonie user switching your computer
                    off in the middle of some sort of transaction; spilling cola all over your keyboard
                    while some critical code is running; power failures while new batteries fail too
                    and a UPS burn out at the same time; half of your RAM memory failing because
                    of a bad contact caused by one single pin; your mother in law burning the one
                    and only source code listing with her cigar; sending all backup disks into orbit
                    (including your workdisk); eclipse wiping out all your sources because of a cvs
                    failure; a nuclear bomb on your office; EMP into your network connection cable;
                    the sky falling down of everybody's head.

                    *Everything* is more likely to make your application fail than an overflow in your
                    long counter. Don't even implement a JOptionPane message for that.

                    kind regards,

                    Jos
                    Jos. Please step away from the keyboard slowly and go to bed.

                    @OP: Jos is right of course. It's very good of you to be thinking of overflows in your programs and all that but (2^63) - 1 is very big. Try to write it down and see.

                    Comment

                    • JosAH
                      Recognized Expert MVP
                      • Mar 2007
                      • 11453

                      #25
                      Originally posted by r035198x
                      Jos. Please step away from the keyboard slowly and go to bed.
                      I still have to have dinner and a bit of leisure time until I go to bed!

                      kind regards,

                      Jos ;-)

                      Comment

                      • blazedaces
                        Contributor
                        • May 2007
                        • 284

                        #26
                        Originally posted by JosAH
                        Sure, I'd be glad to:

                        |Sun, Pluto| ~= 6e12 metres

                        6e12/(2^63) ~= 6.5e-7 metres ~= 6.5e-4 millimetres < 1/1000 millimetres.

                        kind regards,

                        Jos
                        r035198x, I'm not trying to be insulting, I just think this was funny as ...

                        YOU HAVE BEEN
                        PWNED!!!!!!!!!

                        :P

                        -blazed

                        P.S. I still can not understand why you still have a problem. You keep asking how to spit out a warning if your counter gets too high. If you really want to do it, just do it, it's just an if-statement every time (waste of processing speed, but very very very little depending on how often you do it) asking if the counter == integer.maxvalu e or whatever they wrote up top.

                        Comment

                        • r035198x
                          MVP
                          • Sep 2006
                          • 13225

                          #27
                          Originally posted by blazedaces
                          ...(waste of processing speed, but very very very little depending on how often you do it) asking if the counter == integer.maxvalu e or whatever they wrote up top.
                          and the work done / the functionality gained is disappointing considering that the condition is most likely never going to be satisfied.

                          Comment

                          • blazedaces
                            Contributor
                            • May 2007
                            • 284

                            #28
                            Originally posted by r035198x
                            and the work done / the functionality gained is disappointing considering that the condition is most likely never going to be satisfied.
                            Yes, I concur, but he kept insisting, so I wanted to point out the obvious choice of doing whatever you want regardless of the advice from people with more knowledge and experience (not talking about myself here).

                            -blazed

                            Comment

                            • javaalien
                              New Member
                              • May 2007
                              • 34

                              #29
                              Thank you very much for all the advice.
                              Of course I take note all of your precious advice to me...
                              I am here to consult from you guys that must be very experienced and already be there before me....So by asking some sort of silly questions (to you guys but not for me, because I really don't know) so I will get some feedback, am I doing right or maybe that's not even possible..that' s it.
                              I am learning and sorry for making any trouble here...thank you

                              Comment

                              • blazedaces
                                Contributor
                                • May 2007
                                • 284

                                #30
                                Originally posted by javaalien
                                Thank you very much for all the advice.
                                Of course I take note all of your precious advice to me...
                                I am here to consult from you guys that must be very experienced and already be there before me....So by asking some sort of silly questions (to you guys but not for me, because I really don't know) so I will get some feedback, am I doing right or maybe that's not even possible..that' s it.
                                I am learning and sorry for making any trouble here...thank you
                                I apologize, I am often too harsh. Please don't ever hesitate to ask whatever question you would like.

                                The only stupid question is the one you don't ask.

                                Good luck in your future endeavors,

                                -blazed

                                Comment

                                Working...