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]
# 1
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.
floundera at 2007-7-12 19:57:12 > top of Java-index,Java Essentials,New To Java...
# 2

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.

Lava_Javaa at 2007-7-12 19:57:12 > top of Java-index,Java Essentials,New To Java...
# 3

> 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.

cotton.ma at 2007-7-12 19:57:12 > top of Java-index,Java Essentials,New To Java...
# 4

> 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!

floundera at 2007-7-12 19:57:12 > top of Java-index,Java Essentials,New To Java...
# 5
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.
petes1234a at 2007-7-12 19:57:12 > top of Java-index,Java Essentials,New To Java...
# 6
Actually shouldn't the output be "Hi" "Bye" and not even enter the loop?
floundera at 2007-7-12 19:57:12 > top of Java-index,Java Essentials,New To Java...
# 7
Then it seems like there is something wrong with TextPad. Anybody know if there's a setting I have to change or something?
Lava_Javaa at 2007-7-12 19:57:12 > top of Java-index,Java Essentials,New To Java...
# 8
Yes, make sure the code you have has been saved and that you are compiling/running the correct version.
floundera at 2007-7-12 19:57:12 > top of Java-index,Java Essentials,New To Java...
# 9
> 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.
petes1234a at 2007-7-12 19:57:12 > top of Java-index,Java Essentials,New To Java...
# 10

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

Lava_Javaa at 2007-7-12 19:57:12 > top of Java-index,Java Essentials,New To Java...
# 11

> > 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.

floundera at 2007-7-12 19:57:12 > top of Java-index,Java Essentials,New To Java...
# 12

> 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.

floundera at 2007-7-12 19:57:12 > top of Java-index,Java Essentials,New To Java...
# 13

> 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.

CaptainMorgan08a at 2007-7-12 19:57:12 > top of Java-index,Java Essentials,New To Java...
# 14

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.

Lava_Javaa at 2007-7-12 19:57:12 > top of Java-index,Java Essentials,New To Java...
# 15
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.
Lava_Javaa at 2007-7-21 22:32:38 > top of Java-index,Java Essentials,New To Java...
# 16
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!
floundera at 2007-7-21 22:32:38 > top of Java-index,Java Essentials,New To Java...
# 17

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.

Lava_Javaa at 2007-7-21 22:32:38 > top of Java-index,Java Essentials,New To Java...
# 18

> 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 :(

petes1234a at 2007-7-21 22:32:38 > top of Java-index,Java Essentials,New To Java...
# 19
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.
floundera at 2007-7-21 22:32:38 > top of Java-index,Java Essentials,New To Java...