showInputDialog and System.out.print
Why won't the following print anything out until the cancel button is pressed on showInputDialog?
String input;
System.out.println("Hi!");
while (input !=null)
{
input = JOptionPane.showInputDialog("Enter something");
System.out.println(input);
}
System.out.println("Bye!");
Nothing will get printed out until the Cancel button is pressed (input = null). How can I get around this so stuff will be printed as the program is going through the code?
[703 byte] By [
Lava_Javaa] at [2007-11-27 8:12:47]

Huh?Your program says to print out what is returned from the JOptionPane but you want it to print out something BEFORE it returns anything.
Look at the code and then read my post again. It's not printing anything out. It's not printing "Hi!" or any of the input until input = 0. I want it to print "Hi!", user inputs String, print that String, user inputs another String, print that String too, user hits Cancel, print "Bye!", and we're done. Instead, it won't print anything until the user hits Cancel.
> Look at the code and then read my post again. It's
> not printing anything out. It's not printing "Hi!" or
> any of the input until input = 0. I want it to print
> "Hi!", user inputs String, print that String, user
> inputs another String, print that String too, user
> hits Cancel, print "Bye!", and we're done. Instead,
> it won't print anything until the user hits Cancel.
Try actually compiling and running the code you posted.
> Look at the code and then read my post again. It's
> not printing anything out. It's not printing "Hi!" or
> any of the input until input = 0. I want it to print
> "Hi!", user inputs String, print that String, user
> inputs another String, print that String too, user
> hits Cancel, print "Bye!", and we're done. Instead,
> it won't print anything until the user hits Cancel.
I have doubt in your post!
I'm not sure what's wrong. When I ran your code, it printed "Hi!" first and then showed the dialog box. It basically did everything as I expected it would. Why it would behave differently when you run it, I have no idea.
Actually shouldn't the output be "Hi" "Bye" and not even enter the loop?
Then it seems like there is something wrong with TextPad. Anybody know if there's a setting I have to change or something?
Yes, make sure the code you have has been saved and that you are compiling/running the correct version.
> Then it seems like there is something wrong with> TextPad. Anybody know if there's a setting I have to> change or something?unless I am terribly mistaken (happened once 04/02/2005), textpad should have no effect on how java functions.
flounder, there is a difference between a String that equals "" and a String that equals Null or has not been initalized. They are not the same. And I am compiling the correct version.
EDIT:
In my code, I have String input = "";
This is my code EXACTLY as I'm compiling it:
import javax.swing.*;
public class ShowInputDialog {
public static void main(String[] arg) {
String input = "";
System.out.println("Hi!");
while (input != null)
{
input = JOptionPane.showInputDialog("Enter something");
System.out.println(input);
}
System.out.println("Bye!");
}
}
Message was edited by:
Lava_Java
Message was edited by:
Lava_Java
> > Then it seems like there is something wrong with
> > TextPad. Anybody know if there's a setting I have
> to
> > change or something?
>
> unless I am terribly mistaken (happened once
> 04/02/2005), textpad should have no effect on how
> java functions.
Ah yes. That was a black black day for Java programming. Everyone remembers what they were doing on that day.
> flounder, there is a difference between a String that
> equals "" and a String that equals Null or has not
> been initalized. They are not the same. And I am
> compiling the correct version.
>
> EDIT:
> In my code, I have String input = "";
>
> Message was edited by:
> Lava_Java
And in the code you posted originally you had String input; Hence my comment and reinforces my belief you have mutliple vesions of your code of which you are running the incorrect one.
> flounder, there is a difference between a String that
> equals "" and a String that equals Null or has not
> been initalized. They are not the same.
You're right, but the String in the code you first posted would be null.
> And I am compiling the correct version.
Well then you are doing something else wrong.
Ugh. I already edit my message after realizing there was one difference between the two. I already posted the full version of what I'm currently trying to compile. Even if with String input; and not String input = "", System.out.println("Hi!") should still be printed as soon as the program runs.
I just tried running my program in JCreator, and it works like it should. So like I was saying, there must be a setting in TextPad that needs to be changed.
Post you current code. At the same time delete your old .class file and resave you code. Compile and run again. Make sure you have the correct version!
I created a new java file in a new directory, and put the code in there, and still won't work like it's supposed to in TextPad. But as mentioned before, copying-and-pasting the same code into JCreator works perfectly fine. But TextPad is my main editor, and I'm wondering why it won't work in there.
> I created a new java file in a new directory, and put
> the code in there, and still won't work like it's
> supposed to in TextPad. But as mentioned before,
> copying-and-pasting the same code into JCreator works
> perfectly fine. But TextPad is my main editor, and
> I'm wondering why it won't work in there.
Dang. I'll have to add 06/19/2007 to the list :(
Never used Textpad. Only thing I can think of is you need to configure where it sends its output. It is happening, you are just not seeing it.