We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello everyone who read this Question. Yesterday we were told at school to make a program that read a string from keyboard and than print the string. The library I have imported and "Inputs.readString("Enter your age");" help me to add the string from my keyboard creating a box where I can acutally type what string x will get. The box should pop up every time i close is or not leave it black. The problem is that even if I give to x a string, the box still pop up.. unsless i stop the program. Is this a bug into the library or is just me coding it wrong? There you can get the library: https://github.com/aerrity/Inputs
import iadt.creative.Inputs;
String x;
do
{
x=Inputs.readString("Enter your age");
println(x);
}while(x!=null);
Answers
It's going to be pretty hard to help you, since we don't have access to the
Inputs.readString()
function.However, what you described is exactly what I would expect this loop to do. Notice that you're looping while x is not null, which will always be true when the user enters some text.
The Inputs.readString() function is on the link i give. The problem is, when I do while(x==null) or whille(x.equals(null)) and close the box, it wont show the box again to add the string. Somehow it exits the do/while loop.
What exactly does the
Inputs.readString()
return? Does it return null, or does it return an empty String ""?Class Inputs is a collection of
static
utility functions which wrap up Java's JOptionPane functionality:This is readString() btW if some1 is too lazy to check that out:
That is a good question, when you try to print the string x, for example, in this case prints nothing. ! help The function itself is helping me to attribute the value/string to the variable.
@GoToLoop: I'm not lazy. I'm trying to help OP work through this.
@portarul23: Try using print statements to check the value of the String. What happens if you do println(x.equals(""))?
true if i close the box, false if i type something into the box
Okay I figured out. It seems to work with this code
Thanks a lot for you help. And sorry for keeping you too long on this thing.
Glad you made it! :D
Here's my own attempt after copy & paste some of "Inputs.java" and modify it a bit: :ar!
@GoToLoop: Want to do my homework next?
what happens if you escape or cancel the dialogue box? do you still get an empty string?
put a delimiter around your output, catches a lot of errors (including trailing whitespace)
println("[" + x + "]");
doesn't help when you've got a string containing "null" though (not a null string, a string containing the word "null")
A more elaborate version for the "Inputs.java" library: :bz
"Keyboard_Input_Library.pde"
"Inputs.java"
@KevinWorkman The OP would have gained a lot from your input. Not only did he manage to reach a solution he will have gained knowledge of finding the solution. Nice one! =D>
Wow. So you decompiled what my teacher made so I can understand better how the Input work. Thanks a lot for that. I will take a look and try to figure out those things there. I have no words to thank you for this.
There is no "decompiling" necessary. The entire code is available at the link you gave us: https://github.com/aerrity/Inputs/blob/master/src/Inputs.java
Here is the entirety of the
readString()
function you're using:You can also use the Java API to figure out what this will return if the user cancels or doesn't enter any text. The component is a
JTextField
, so itsgetText() function will return an empty String instead of
null`.I have no idea what GoToLoop is trying to help you learn, all I can see is that he's needlessly complicating your teacher's code. I wouldn't recommend using it as a learning tool, but it's really up to you.
Now I figured out what my lecturer made for us.. so now I am able to make an Input box without importing my teacher library. Thanks so much to all of you. Not only that you helped me solve my exercise, but you also helped me to figure out how he made the library and how it is working. I am new on this program and I have never used Java. I have only used C++ for like 4 years, so for me, everything looks like C++ combined to Jave...
Keep in mind that your teacher might want you to use his libraries.