1 2 Previous Next 17 Replies Latest reply: Jun 11, 2010 11:50 PM by 807580 RSS

    messing with strings

    807580
      I'm very new to java and am working on my first Android app. I am using the webview demo as a template. I am trying to generate a random integer between 1 and 12 and then call a certain javascript function based on the result. Here's what I have:
      int number = 1 + (int)(Math.random() * ((12 - 1) + 1));
                          number = (int) Math.floor(number);
                       String nextQuote = "javascript:wave" + number + "()";
                       mWebView.loadUrl(nextQuote);
      So
      mWebView.loadUrl(nextQuote)
      will be the same as something like
       mWebView.loadUrl("javascript:wave1()")
      I just want to know if what I have here is correct and will work the way I think it will. The application isn't responding as expected and I suspect this bit of code is the culprit.
        • 1. Re: messing with strings
          807580
          You might want to ask on an Android forum. Android's Java isn't standard Java as owned/run by Oracle/Sun.
          • 2. Re: messing with strings
            807580
            aloishis89 wrote:
            int number = 1 + (int)(Math.random() * ((12 - 1) + 1));
            number = (int) Math.floor(number);
            Change the above to this,
            int number = (int)(Math.random() * 12.0) + 1;
            It will first produce an int between 0 and 11 and then 1 is added.
            • 3. Re: messing with strings
              807580
              double post by mistake
              • 4. Re: messing with strings
                YoungWinston
                wastrel wrote:
                Change the above to this,
                int number = (int)(Math.random() * 12.0) + 1;
                An alternative is to set up a random number generator, viz:
                private static final Random RNG = new Random();
                and then from anywhere in your class you can call:
                int number = RNG.nextInt(12) + 1;
                which saves a lot of casting.
                There are a couple of caveats to the method, so it's probably worth reading the documentation; but it should be fine for your purposes.

                Winston
                • 5. Re: messing with strings
                  Kayaman
                  YoungWinston wrote:
                  An alternative is to set up a random number generator, viz:
                  private static final Random RNG = new Random();
                  Seems he's doing Android development and Android doesn't seem to have the Random() class (just happened to install the dev kit myself yesterday).

                  So he'll have to go the old fashioned way...
                  • 6. Re: messing with strings
                    YoungWinston
                    Kayaman wrote:
                    Seems he's doing Android development and Android doesn't seem to have the Random() class (just happened to install the dev kit myself yesterday).
                    So he'll have to go the old fashioned way...
                    Ooof. No java.util? That's nasty. Or is it one of those mini-versions of Java for mobile phones or something?

                    Anyway, sorry if I misled.

                    Winston
                    • 7. Re: messing with strings
                      Kayaman
                      YoungWinston wrote:
                      Ooof. No java.util? That's nasty. Or is it one of those mini-versions of Java for mobile phones or something?
                      It's not as horrible as J2ME, but it's some sort of subset of apache libraries and java libraries. I haven't managed to go through the whole API yet to decide whether I like it or hate it ;)
                      • 8. Re: messing with strings
                        807580
                        YoungWinston wrote:
                        An alternative is to set up a random number generator, viz:
                        I was about to suggest Random when I saw paulcw's post. I've never heard about Android but I got as much as it's not standard Java. Since Math.random seemed available to the OP I decided to build my reply on that instead.
                        • 9. Re: messing with strings
                          807580
                          Given that games are a standard mobile phone app these days, it seems odd to me that java.util.Random isn't included (or some similar random number generator, more powerful than Math.random). Maybe there's on in the alternative libraries? If you find out I'd like to know...
                          • 10. Re: messing with strings
                            YoungWinston
                            paulcw wrote:
                            Given that games are a standard mobile phone app these days, it seems odd to me that java.util.Random isn't included (or some similar random number generator, more powerful than Math.random). Maybe there's on in the alternative libraries? If you find out I'd like to know...
                            Actually, I just checked the Java 6 SE source, and Math.random() is based on java.util.Random.nextDouble().
                            Which doesn't necessarily prove anything, but clearly Sun felt it was worth doing :-)

                            Winston

                            Edited by: YoungWinston on Jun 14, 2010 2:46 AM
                            I should point out that the source I checked was the openjdk version.
                            • 11. Re: messing with strings
                              807580
                              I don't get your point.
                              • 12. Re: messing with strings
                                YoungWinston
                                paulcw wrote:
                                I don't get your point.
                                If java.lang.Math uses java.util.Random, the class must be there somewhere; it may just be hidden from view. It's also possible that Android's version is non-standard (or that Sun's own version is different from the openjdk one, but I was under the impression that the two worked largely in parallel).

                                Winston
                                • 13. Re: messing with strings
                                  JoachimSauer
                                  YoungWinston wrote:
                                  paulcw wrote:
                                  I don't get your point.
                                  If java.lang.Math uses java.util.Random, the class must be there somewhere; it may just be hidden from view. It's also possible that Android's version is non-standard (or that Sun's own version is different from the openjdk one, but I was under the impression that the two worked largely in parallel).
                                  You're assuming that an implementation of Math.random() must use java.util.Random, and I'm pretty sure that this is not specified. It was probably the simplest thing to do in the Sun JDK (and OpenJDK) to delegate to the existing code, but nothing stops an JRE implementor to build their own implementation of Math.random() as long as it's conforming to the spec.

                                  Anyway: Since Android doesn't use "real Java" (unfortunately) all of this is moot, since they can deviate from the specification in any point they wish.
                                  • 14. Re: messing with strings
                                    807580
                                    JoachimSauer wrote:
                                    You're assuming that an implementation of Math.random() must use java.util.Random, and I'm pretty sure that this is not specified.
                                    That's right. The API doesn't specify java.util.Random must be used. It specifies it should work as if java.util.Random was used.

                                    This gives implementors a choise to use either java.util.Random or something else that works exactly like it.
                                    1 2 Previous Next