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.
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.
B4J currently supports two types of applications: UI applications (based on JavaFX) and Non-UI applications. Non-UI applications are standard Java applications. Usually the input to such applications is with command line arguments. You should create a new project and select: The program...
www.b4x.com
You can use Log to print text and ReadLine to read input.
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.
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
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.