开发者

Using a string on all the program in java?

Actually the title of the question is not properly right, what I want in my program is that whenever i run my program, I take an input from the user through input dialog and store it in a string. I have made a method for this and whenever i want to use this string in my program i just call this method but my problem is that whenever i call this method, it pops up the 开发者_开发技巧input dialog which I dont want. I want the input dialog to come once when the program is run and then i could use that input anywhere in my program. Please help me. Thanks


Add a static boolean variable in the method where you are calling the InputDialog. After calling the dialog for the first time, set the boolean var's value to true. And then add a check which will determine whether to show the dialog or not. Consider the following pseudo;

static boolean isNotFirstRun;

if (isNotFirstRun == false) {
// Show InputDialog

isNotFirstRun = true;
}

// Perform other operations.

Hope you got the idea.


Make the dialog conditional based on the value of your string.

public String myPreciousString = null;   

public String getInput() { 
   if(myPreciousString == null){
      //show dialog
      myPreciousString  = dialog.getText();
   }
   return myPreciousString ; 
} 


Create a variable to store the string in your program. When you need get the string, check the variable first. If it contains a value, return it and don't prompt the user. If it does not contain a value, prompt the user and store what they type in in the variable before returning it.


Write some code that can infer the program state. So first time it will be empty state which will trigger the dialog. If it is not empty, it will just use the state value. Hope this helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜