Bug? Inline Java works well?

Discussion in 'Bugs & wishlist' started by LucaMs, May 22, 2015.

  1. LucaMs

    LucaMs Expert Licensed User

    This code:
    Code:
    #If JAVA
    import java.util.Locale;
    public Locale getNewLocale(String Language, String Country) {
    return new Locale(language, country);
    }
    #End If

    and this:
    Code:
    #If JAVA
    import java.text.NumberFormat;
    public Double NumberParse(String Value) {
        return NumberFormat.getInstance().parse(Value).doubleValue();
    }
    #End If

    and this:
    Code:
    #If JAVA
    import java.text.NumberFormat;
    public Integer NumParse() {
    NumberFormat nf = NumberFormat.getInstance();
    Number myNumber = nf.parse("1.5");
    return 0;
    }
    #End If

    do not work.

    Since I'm not a Java expert, I do not know if all the examples are wrong or if inline java has a bug.


    Thanks
     
    Last edited: Aug 15, 2015
  2. DonManfred

    DonManfred Expert Licensed User

    does not work is not a error-description!
    Did you get any error? Did you get wrong results? Which?
    What is the code you are using; upload a project which shows the problem.

    Just with the info "does not work" we cant help. At least i cant
     
  3. LucaMs

    LucaMs Expert Licensed User

    You're right.

    Now I don't remember all error messages, but very probably it is "cannot find symbol" for all "examples", as here.
     
  4. DonManfred

    DonManfred Expert Licensed User

    then you maybe forgot to add import declarations into the inline java?
     
  5. LucaMs

    LucaMs Expert Licensed User

    I prepare a simple test project and attach it.
     
  6. DonManfred

    DonManfred Expert Licensed User

    Code:
    public Locale getNewLocale(String Language, String Country) {
            
    return new Locale(language, country);
        
    }
    for example this cannot work!

    Methodparameters are casesensitive. language and country are unknown! Use them like you have DECLARED them in the method signature

    Language
    and
    Country
     
  7. LucaMs

    LucaMs Expert Licensed User

    Yes, I'll try it, thank you.
     
  8. LucaMs

    LucaMs Expert Licensed User

    The attached project tests the third example (the method is not useful but I wanted to test it without parameters and return value)

    Code:
    #If JAVA
    import java.text.NumberFormat;
    public Integer NumParse() {
    NumberFormat nf = NumberFormat.getInstance();
    Number myNumber = nf.parse("1.5");
    return 0;
    }
    #End If
     

    Attached Files:

  9. LucaMs

    LucaMs Expert Licensed User


    This now works, Speedy, thanks:
    Code:
    import java.util.Locale;
    public Locale getNewLocale(String Language, String Country) {
            
    return new Locale(Language, Country);
    }
     
  10. LucaMs

    LucaMs Expert Licensed User

    The attached project tests the second example:
    Code:
    #If JAVA
    import java.text.NumberFormat;
    public Double NumberParse(String Value) {
        return NumberFormat.getInstance().parse(Value).doubleValue();
    }
    #End If

    At compile time I get:

    upload_2015-5-22_13-33-47.png
     

    Attached Files:

  11. DonManfred

    DonManfred Expert Licensed User

    Code:
    Log("nativeMe.RunMethod(NumParse) -> "&nativeMe.RunMethod("NumParse"Null))
    Code:
    #If JAVA
    // import java.lang.Object; unneeded
    // import java.text.Format; unneeded
    import java.text.NumberFormat;
    import java.text.ParseException;

    public Integer NumParse()  throws ParseException {
    NumberFormat nf = NumberFormat.getInstance();
    Number myNumber = nf.parse("1,5");
    return 0;
    }
    #End If
    1.5 is not a parseable number. 1,5 is
     
  12. LucaMs

    LucaMs Expert Licensed User

    no, you're talking about the third example. I attached a project for the second.

    Also, "1.5" is a parseable number, it is 3 / 2 :)
     
  13. Erel

    Erel Administrator Staff Member Licensed User

    Inline Java works well. If you need any help then it is better to start a new thread in the questions forum.

    Do note that there are no advantages (only disadvantages) to implement things that can be done in B4A with this feature.
     
  14. LucaMs

    LucaMs Expert Licensed User

    Well, since I have the doubt that inline java was the problem, I wrote here the question, of course.

    I know, it is better to use the reflection or java object, when b4a is not enough, but having java code available...!

    But are you sure that Inline Java works well? Have you tried the example #10?
     
  15. JordiCP

    JordiCP Well-Known Member Licensed User

    I think DonManfred's answer works ok for example #10

    Also, if you want no exceptions (and return, for instance 0 if the input argument is not valid), you can do

    Code:
    #If JAVA
    import java.text.NumberFormat;
    import java.text.ParseException;

    //No exceptions
    public double NumberParse(String Value) {

        try{
            double d = NumberFormat.getInstance().parse(Value).doubleValue();
            return(d);
        }
        catch(Exception e){
            return (0.0);  // don't know why, before it worked with 0 and now the compiler complains
        }
    }
    // Throws exception if the input string is not parseable according to "local" settings.
    public double NumParse(String Value)  throws ParseException {

        //double d =
        return (NumberFormat.getInstance().parse(Value).doubleValue());
    }
    #End If

    I suppose it must depend on regional settings, but in my case "12,53" is parsed correctly, whilst it does not accept "12.53"

    --EDIT--
    Please also note that "Double" is a class and "double" is a primitive type (does not work with the former)
     
    Last edited: May 22, 2015
  16. LucaMs

    LucaMs Expert Licensed User

    Thank you @JordiCP.

    (I should ask you how to handle the exception, but not here ;)).

    Surely the problem is not the string format passed, given I get the error at compile time, not a run time.

    I'll try replacing the code with your, anyway.

    Thanks
     
  17. LucaMs

    LucaMs Expert Licensed User

    @JordiCP your code works.

    I added my code before yours:
    Code:
    #If JAVA

    import java.text.NumberFormat;
    import java.text.ParseException;

    // My code
    public double MyNumberParse(String Value) {
        double d = NumberFormat.getInstance().parse(Value).doubleValue();
        return(d);
    }

    // JordiCP code
    //No exceptions
    public double NumberParse(String Value) {

        try{
            double d = NumberFormat.getInstance().parse(Value).doubleValue();
            return(d);
        }
        catch(Exception e){
            return (0);
        }
    }

    #End If
    and I get the error when I compile it :eek:
     
  18. JordiCP

    JordiCP Well-Known Member Licensed User

    As "parse()" method can throw exceptions, any code calling it must provide a way to deal with them. In the first example in #15, it is catched in the same function. In the second, the exception is raised directly.

    if you add "throws ParseException" to your code (as second example in #15), it will compile .

    Going back to the example, in B4A, these 4 lines will give different results:
    Code:
    If FirstTime=True Then
            NativeMe.InitializeContext
        
    End If

        
    Log(NativeMe.RunMethod("NumberParse",Array As String("12,43")))  ' accepted by parse(), will give "12.43"
        Log(NativeMe.RunMethod("NumberParse",Array As String("13.43")))  ' not accepted, but we catched the exception, will give "0"
        Log(NativeMe.RunMethod("NumParse",Array As String("14,43")))      ' accepted, will give "14.43"
        Log(NativeMe.RunMethod("NumParse",Array As String("15.43")))      ' not accepted, will throw an exception and crash
    (edited post #15 so that first function returns 0.0 instead of 0. Strange, since before it compiled ok and now it complained about it o_O)
     
    LucaMs likes this.
  19. LucaMs

    LucaMs Expert Licensed User

    JordiCP likes this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice