This week's book giveaway is in the General Computing forum. We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line! See this thread for details.
I am trying to compare the input from a JOptionPane to string literal and execute an event based on if the values match or not.
I do understand that .equalsIgnoreCase() is not part of the JOptionPane class, but I dont know any other way to compare string values.
here are my errors:
C:\Documents and Settings\ben\Desktop\park>javac BenHultinProg7.java
.\Prog7.java:90: cannot find symbol
symbol : method equalsIgnoreCase(java.lang.String)
location: class javax.swing.JOptionPane if (inPut1.equalsIgnoreCase("red")) {
^
.\Prog7.java:100: cannot find symbol
symbol : method equalsIgnoreCase(java.lang.String)
location: class javax.swing.JOptionPane if (inPut2.equalsIgnoreCase("white")) {
^
.\Prog7.java:109: cannot find symbol
symbol : method equalsIgnoreCase(java.lang.String)
location: class javax.swing.JOptionPane if (inPut3.equalsIgnoreCase("yellow")) {
^
.\Prog7.java:118: cannot find symbol
symbol : method equalsIgnoreCase(java.lang.String)
location: class javax.swing.JOptionPane if (inPut4.equalsIgnoreCase("green")) {
^
.\Prog7.java:127: cannot find symbol
symbol : method equalsIgnoreCase(java.lang.String)
location: class javax.swing.JOptionPane if (inPut5.equalsIgnoreCase("blue")) {
^
5 errors
But you're not comparing String values. You're comparing a JOptionPane with a String which makes no sense, and so the compiler is right to complain. Just what are you trying to have your program do here?
Ben Hultin
Ranch Hand
Joined: Aug 17, 2009
Posts: 135
posted
0
well I am trying to compare the string value that was inputed by the user from the JOptionPane with another string literal. How would I get the value from the JOptionPane and save it into a variable?
How would I get the value from the JOptionPane and save it into a variable?
What are you talking about you already know how to do this.
How is prompting for a second color any different than prompting for the first color?
Ben Hultin
Ranch Hand
Joined: Aug 17, 2009
Posts: 135
posted
0
Sorry about that, I must have gotten sidetracked. The real issue is the .equalsIgnoreCase() method. The JVM is trying to find it in the JOptionPane class. But I need it to look in the java.lang.String class which I understand is implicit.
The main purpose is to get the String value gathered from the JOptionPane input and compare it with another string literal. I know I cant use the == for comparing strings and .equals() is missing a feature I need. So .equalsIgnoreCase() is the one I need to use, but as mentioned before the JVM is looking for this method in the swing class. How could I fix this?
Thanks for the response, but I am not trying to compare two separate JOptionPane inputs with each other. I am trying to compare if:
inPut1.equalsIgnoreCase("red");
Besides i am not sure if you suggestion would work, its the .equalsIgnoreCase() that is not being found by the JVM. How would I make the .equalsIgnoreCase() method found?
I have used this method several times in the past without a problem, so I dont why its causing a problem now or how to fix it.
pete stein
Bartender
Joined: Feb 23, 2007
Posts: 1561
posted
0
Ben Hultin wrote:Thanks for the response, but I am not trying to compare two separate JOptionPane inputs with each other. I am trying to compare if:
inPut1.equalsIgnoreCase("red");
Besides i am not sure if you suggestion would work, its the .equalsIgnoreCase() that is not being found by the JVM. How would I make the .equalsIgnoreCase() method found?
I have used this method several times in the past without a problem, so I dont why its causing a problem now or how to fix it.
The method can only be called on a String object. Is this how you are trying to call it?
Rather than make us to guess, why not post your latest code effort and let us see for ourselves.
Much luck.
Ben Hultin
Ranch Hand
Joined: Aug 17, 2009
Posts: 135
posted
0
Thanks, I have included the current code I have below for inPut1 JOptionPane and the string literal I am trying to compare it to.
The two lines that really matter are:
the error I am getting about this particular line is:
.\Prog7.java:90: cannot find symbol
symbol : method equalsIgnoreCase(java.lang.String)
location: class javax.swing.JOptionPane if (inPut1.equalsIgnoreCase("red")) {
you need to re-start, at page 1, whatever programming book you have, and try to grasp the basics
here, input1 is local to the if(), and cannot be 'seen' anywhere else
in your original post, you have this
private JOptionPane inPut1;
so, it seems you think, via some magic, the two input1's are linked - they are not
the way to do it
//private JOptionPane inPut1;
private String inPut1;
then
//String inPut1 = JOptionPane.showInputDialog("Enter first color");
inPut1 = JOptionPane.showInputDialog("Enter first color");
JOptionPane.showInputDialog(..) returns a String (someString or empty) or null ('esc' 'X' or 'Cancel'), so null should always be the first check
Ben Hultin
Ranch Hand
Joined: Aug 17, 2009
Posts: 135
posted
0
huh, I thought local variables only applied within a method. Never heard it also applied to if() as well. Thanks for the heads up there. That would explain the problem.