• Post Reply Bookmark Topic Watch Topic
  • New Topic

Basic questions on JOptionsPane  RSS feed

 
Patrick De
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is some code I got from Java for Dummies. I have a few questions about it



1) Where do I get the import javax.swing.JOptionPane; ? I am using Eclipse Eclipse

SDK
Version: 4.2.2
Build id: M20130204-120
I went to this website : http://stackoverflow.com/questions/14181142/eclipse-doesnt-find-javax-swing
and it didn't fix the issue.


2) Why do they "parseInt" in this case? Couldn't they have made 's' an 'int' initially and by-passed that part?

3) If a "class variable" is declared outside the main, it is available to all the methods, correct? If this is so, it is also available in other classes?

 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37181
515
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1) It's part of the Java language. What is the exact error message you are getting? Do other programs that don't use JOptionPane work.

2) No. The input prompt is just getting a String. Java doesn't know that you have an int. That's why the code has to tell it.

3) If it is an instance variable (or private), it is only available to one class. If it is a public, static variable, it is available everywhere.
 
Campbell Ritchie
Sheriff
Posts: 55351
157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't call it a class variable unless it is static. Some people believe there should be no such thing as a static variable and static fields should be reserved for constants.

As Jeanne has already said, the methods of JOptionPane usually return Strings, so they may need parsing to other datatypes, which you can see here with the Integer class.
I suggest you change the last line to read
System.out.println("you entered half of " + x * 2 + "."); // enter 123, output 246
Then you can see you have done arithmetic on the number and that it is not a String any more.
 
Campbell Ritchie
Sheriff
Posts: 55351
157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David De wrote: . . .
I went to this website : http://stackoverflow.com/questions/14181142/eclipse-doesnt-find-javax-swing
and it didn't fix the issue.
. . .
That is not fair on the people on SO; they gave correct answers.
 
Patrick De
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1) Here is the error : "Opens the artifact search dialog to search for 'javax.swing' to add
to your target"

2) Can't the input be an "Int" to start with instead of a string?

3) So as long as it remains public static, it is used in other classes as well?

4) Sorry, yes SO did give a solution that worked for other people, but it did not fix my problem, so I was not fair in saying it did not work, I meant to say it is did work for me because of my inexperience.
 
Campbell Ritchie
Sheriff
Posts: 55351
157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David De wrote:1) Here is the error : "Opens the artifact search dialog to search for 'javax.swing' to add
to your target"
Where does that come from? I didn't see anything like that on the SO thread.

2) Can't the input be an "Int" to start with instead of a string?
Read the documentation for the JOptionPane methods. If it says the return type is XYZ then the return type is XYZ and it cannot be anything else.

3) So as long as it remains public static, it is used in other classes as well?
Don't know. You never know where a method has been used. If a method in another class calls it, then it is used from there, yes.

4) Sorry, yes SO did give a solution that worked for other people, but it did not fix my problem, so I was not fair in saying it did not work, I meant to say it is did work for me because of my inexperience.
Apologoies accepted on behalf of the people on SO
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!