Android Question [Solved]How to code in JavaCode for using JavaObject?

Theera

Expert
Licensed User
Longtime User
I'm still learning how to use javaobject,but I don't know to recode javacode about return the value.
I need someone to show me the example.
 

Attachments

  • Screenshot_20250207_163053.png
    Screenshot_20250207_163053.png
    273 KB · Views: 39
  • pyramid.zip
    9.3 KB · Views: 38
Solution
You can compare your commented code with mine.
Pay attention to the uppercase and lowercase characters and don't forget the semi-colons ( ; ) in Java.
Use a string to build the pyramid. A newline ("\n") is added for each level.
New assignment for you... can you write code to show the pyramid upside-down (5 asterisks on the first line, 4 on the second line,...)?

B4X:
Sub Button1_Click
'    EditText1.text=CreatePyramid(5)
    EditText1.text = myCreatePyramid(5)
End Sub

'Private Sub CreatePyramid( inputstr As Int) As String
'    Private jo As JavaObject
'    jo.InitializeContext
'    Dim mystring As String=inputstr
'    mystring=jo.RunMethod("PyramidPattern",Array(mystring))
'    Return mystring
'End Sub

'#If Java
'Public string...

PaulMeuris

Well-Known Member
Licensed User
You can compare your commented code with mine.
Pay attention to the uppercase and lowercase characters and don't forget the semi-colons ( ; ) in Java.
Use a string to build the pyramid. A newline ("\n") is added for each level.
New assignment for you... can you write code to show the pyramid upside-down (5 asterisks on the first line, 4 on the second line,...)?

B4X:
Sub Button1_Click
'    EditText1.text=CreatePyramid(5)
    EditText1.text = myCreatePyramid(5)
End Sub

'Private Sub CreatePyramid( inputstr As Int) As String
'    Private jo As JavaObject
'    jo.InitializeContext
'    Dim mystring As String=inputstr
'    mystring=jo.RunMethod("PyramidPattern",Array(mystring))
'    Return mystring
'End Sub

'#If Java
'Public string PyramidPattern (int N) {
'   for (int i=0;i<N;i++){
'     system.out.print(" ".Repeat(N-i-1));
'     system.out.print("*".Repeat(2*i+1));
'   }
'   return ??
'}
'#End If

Private Sub myCreatePyramid(level As Int) As String
    Private jo As JavaObject
    jo.InitializeContext
    Dim mystring As String = ""
    mystring = jo.RunMethod("PyramidPattern",Array(level))
    Return mystring
End Sub

#If Java
public String PyramidPattern(int N) {
    String result = "";
    String space = " ";
    String asterisk = "*";
    for (int i=0; i < N; i++) {
        result += space.repeat(N-i-1);
        result += asterisk.repeat(2*i+1);
        result += "\n";
   }
   return result;
}
#End If
Happy coding!
 
Upvote 0
Solution

Theera

Expert
Licensed User
Longtime User
The result looks unusual.
 

Attachments

  • Screenshot_20250208_132446.png
    Screenshot_20250208_132446.png
    21.8 KB · Views: 37
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
You probably need to use a fixed font so all characters (and spaces) are the same width.
 
Upvote 0

Theera

Expert
Licensed User
Longtime User
You probably need to use a fixed font so all characters (and spaces) are the same width.
The code has only asterisk ,how to fixed font.
 

Attachments

  • pyramid2.zip
    9.5 KB · Views: 35
Upvote 0

PaulMeuris

Well-Known Member
Licensed User
As @Daestrum suggests, you need to use a monospaced font.
1739005350206.png

Set the EditText1 to a monospaced font...
B4X:
    EditText1.As(B4XView).Font = xui.CreateFont(Typeface.MONOSPACE,16)
    EditText1.text = myCreatePyramid(5)
And that gives you the result above.
BTW, did you try to find an answer to my assignment?
 
Upvote 0

Theera

Expert
Licensed User
Longtime User
Many Thanks all of you
 
Upvote 0
Top