Results 1 to 4 of 4
Thread: Call a public string variable
- 11-22-2011, 06:49 PM #1
Member
- Join Date
- Nov 2011
- Location
- Lithuania
- Posts
- 2
- Rep Power
- 0
Call a public string variable
I took a sample code for a drop-down menu for my applet and placed it in a InputDialogWithDropdownListbox class.
Here is where I draw the drop-down menu:Java Code:public class InputDialogWithDropdownListbox{ public String input; public void drop(Valstybe G[], int n) { String[] ListZemynai = new String[10]; for(int i=0;i<n;i++){ ListZemynai[i]=G[i].gautiZemyna(); } final List<String> lst = Arrays.asList(ListZemynai); final HashSet<String> set = new HashSet<String>(lst); String[] unique = (String[]) set.toArray(new String[set.size()]); input = (String) JOptionPane.showInputDialog(null, "Pasirinkite zemyna", "Pasirinkimas", JOptionPane.QUESTION_MESSAGE, null, unique, unique[1] // ); } }
I have invoked String type public variable input located inside drop method. And it did print out the string value which I chose in the dialog.Java Code:class SurastiDialogas extends JDialog { SurastiDialogas(){ InputDialogWithDropdownListbox menii = new InputDialogWithDropdownListbox(); menii.drop(A,n); JButton ok = new JButton("OK"); System.out.println(menii.input ); //returns a value } }
The problem is, I cannot use such string variable in another method which I need to compare to another list of strings. Here is the code:
System.out.println(ZemynaiOption.input); here returns null while in method SurastiDialogas menii.input returns a string value. What am I missing here to return input value inside Valstybe method?Java Code:public Valstybe[] surasti (Valstybe G[], int n){ Valstybe A[] = new Valstybe[10]; InputDialogWithDropdownListbox ZemynaiOption = new InputDialogWithDropdownListbox(); System.out.println("The value of input located in another class: " + "\n"); [B]System.out.println(ZemynaiOption.input); [/B] //returns null String[] valstybes = new String[10]; String[] zemynai = new String[10]; int i=0; for (int a=0;a<n;a++){ valstybes[a]=G[a].gautiPav(); //we get a value from a file (skipped code) and then parse to array zemynai[a]=G[a].gautiZemyna(); // ---------- "" ---------- if(ZemynaiOption.equals(zemynai[a])) { //checking where zemynai[a] equals input string from drop-down menu A[i]=G[a]; //if equals, we form a new array i++; } } return A; //returns filtered array }Last edited by onstock; 11-22-2011 at 06:57 PM.
- 11-22-2011, 09:04 PM #2
Re: Call a public string variable
The println method does not return any value. It is defined as void. Can you explain what you mean by "returns"?System.out.println(ZemynaiOption.input); here returns null
Are you saying that the above println prints out a null for the value of the input variable in the ZemynaiOption object?
Does that variable have a value other than null? Where and when is the variable: input assigned a value?
- 11-23-2011, 09:45 AM #3
Member
- Join Date
- Nov 2011
- Location
- Lithuania
- Posts
- 2
- Rep Power
- 0
Re: Call a public string variable
Exactly.
Here is where variable: input gets assigned with a value.Java Code:input = (String) JOptionPane.showInputDialog(null, "Pasirinkite zemyna", "Pasirinkimas", JOptionPane.QUESTION_MESSAGE, null, unique, unique[1] // );
For instance, I have a list in drop-down menu with some values (values are taken from a text file):
After selecting some value (Europe, for example) and clicking "OK" button, the codeJava Code:Europe Asia Africa
prints out value: "Europe", whileJava Code:System.out.println(menii.input ); //returns a value
prints out value: "null".Java Code:System.out.println(ZemynaiOption.input);
- 11-23-2011, 12:25 PM #4
Similar Threads
-
Is it bad practice to make attributes public so other classes can call their methods?
By enerj in forum New To JavaReplies: 4Last Post: 10-11-2011, 11:48 PM -
This is an error? public static void main(String args[])
By Jackount in forum New To JavaReplies: 10Last Post: 07-10-2011, 08:37 AM -
Making an array public or any variable to use across classes and Java files?
By Jonatan10 in forum New To JavaReplies: 12Last Post: 12-12-2010, 06:04 PM -
Public static void main (String args[])
By arefeh in forum New To JavaReplies: 12Last Post: 01-28-2010, 11:58 AM -
Public class variable
By Java Tip in forum Java TipReplies: 0Last Post: 12-03-2007, 09:58 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks