B4J Question Simple Console IO in B4J?

chikega

Member
Licensed User
I'm having trouble finding an example of a simple input/output Console example in the tutorial documentation. I've found bits and pieces like reading from a text file. But, essentially, how would one translate this Java example (see pic) to B4J? Thank you.

2021-03-23 12_31_33-IDEAProject – Main.java.png
 

stevel05

Expert
Licensed User
Longtime User
I hadn't tried it before but this is (part) of that code converted.
B4X:
    Dim System As JavaObject
    System.InitializeStatic("java.lang.System")

    Dim Input As JavaObject
    Input.InitializeNewInstance("java.util.Scanner",Array(System.GetField("in")))
   
    Log("Hello from IDEA!")
    Log("What is your name? ")
    Dim fName As String = Input.RunMethod("nextLine",Null)
    Log("Your Name is " & fName)
    Log("How old are you?")
    Dim Age As Int = Input.RunMethod("nextLine",Null)
    Log("You are " & Age & " years old")

It works, but you will need to run the jar file from the command line and I guess only with java 8.
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
You can use Log to print text and ReadLine to read input.
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
ReadLine from which library?
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
OK, I should have followed the link :)
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
B4X:
Sub Process_Globals
    Private reader As TextReader
End Sub

Sub AppStart (Args() As String)
    Dim sys As JavaObject
    sys.InitializeStatic("java.lang.System")
    reader.Initialize(sys.GetField("in"))
    
    Log("Hello from B4J!")   
    Log("What is your name?")
    Dim fName As String = ReadLine   
    Log($"Your name is ${fName}!"$)
    Log("How old are you?")
    Dim age As Int = ReadLine
    Log($"You are ${age} years old."$)
    Log("What is your GPA?")
    Dim gpa As Double = ReadLine
    Log($"And your GPA of ${gpa} is really good!"$)   
End Sub

Sub ReadLine As String
    Return reader.ReadLine
End Sub

Depends on JavaObject library and run in Release mode with Windows CMD.
For e.g.
Bash:
java -jar simpleio.jar
 
Upvote 0

chikega

Member
Licensed User
Thanks for the gift ?.
Hi Aeric,
Is there a way of printing to console without a CRLF (carriage return and line feed) in B4J?? Essentially the equivalent of Java:
Java:
System.out.print();
or C#/VB.Net:
Code:
Console.Write();
Thanks! ?
 
Upvote 0

MicroDrie

Well-Known Member
Licensed User
Longtime User
The forum rule is a new question is a new thread so that others can find your question and the answer on it.
Without further explanation what you want to achieve exactly, I give a possible solution with [B4X] Smart String Literal
smart String Example:
public Sub demo
    Dim Var1 As String = "first"
    Dim Var2 As String = "second"
    Log($"The content is ${Var1} and ${Var2}"$)
End Sub
 
Upvote 0

chikega

Member
Licensed User
The forum rule is a new question is a new thread so that others can find your question and the answer on it.
Without further explanation what you want to achieve exactly, I give a possible solution with [B4X] Smart String Literal
smart String Example:
public Sub demo
    Dim Var1 As String = "first"
    Dim Var2 As String = "second"
    Log($"The content is ${Var1} and ${Var2}"$)
End Sub
Thank you MicroDrie, smart string literal appears to be string interpolation which is cool, but not what I'm looking for. I'm simply looking for the ability to print to a console without a newline. I'll create a new thread.
 
Upvote 0
Top