Bug? Inline Java works well?

LucaMs

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


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


and this:
B4X:
#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:

DonManfred

Expert
Licensed User
Longtime User
does not work.
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
 

LucaMs

Expert
Licensed User
Longtime 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

You're right.

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

DonManfred

Expert
Licensed User
Longtime User
B4X:
    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
 

LucaMs

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

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

Attachments

  • Inline Java test.zip
    11.3 KB · Views: 292

LucaMs

Expert
Licensed User
Longtime User
B4X:
    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


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

LucaMs

Expert
Licensed User
Longtime User
The attached project tests the second example:
B4X:
#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
 

Attachments

  • Inline Java test 2.zip
    11.4 KB · Views: 277

DonManfred

Expert
Licensed User
Longtime User
The attached project tests the second example:

B4X:
Log("nativeMe.RunMethod(NumParse) -> "&nativeMe.RunMethod("NumParse", Null))

B4X:
#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
 

LucaMs

Expert
Licensed User
Longtime User
B4X:
Log("nativeMe.RunMethod(NumParse) -> "&nativeMe.RunMethod("NumParse", Null))

B4X:
#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

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 :)
 

LucaMs

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

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

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

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?
 

JordiCP

Expert
Licensed User
Longtime 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

B4X:
#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:

LucaMs

Expert
Licensed User
Longtime 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
 

LucaMs

Expert
Licensed User
Longtime User
@JordiCP your code works.

I added my code before yours:
B4X:
#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:
 

JordiCP

Expert
Licensed User
Longtime 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:
B4X:
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)
 
Top