cant figure out what is the error
Please copy and paste here the full text of the error message.
When comparing the contents of String objects you should use the equals() method not the == operator.
Why is this commented out:
/*if (a.equalsIgnoreCase("B"))*/
NormR1
Posting Expert
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
String a =(String)(JOptionPane.showInputDialog(null, "Automatic Teller Machine\n [B] Balance Inquiry\n [D]Deposit\n [W] Withdrawal\n [Q] Quit\n Select you Options: ","Automatic Teller Machine",JOptionPane.PLAIN_MESSAGE));
also, you don't need this (String) cast, since showInputDialog will give you a String object as result.
stultuske
Posting Sensei
3,137 posts since Jan 2007
Reputation Points: 1,114
Solved Threads: 433
well for starters you don need this:
String a =(String)(JOptionPane.showInputDialog(null, "Automatic Teller Machine\n [B] Balance Inquiry\n [D]Deposit\n [W] Withdrawal\n [Q] Quit\n Select you Options: ","Automatic Teller Machine",JOptionPane.PLAIN_MESSAGE));
it can just be:
String a =JOptionPane.showInputDialog(null, "Automatic Teller Machine\n [B] Balance Inquiry\n [D]Deposit\n [W] Withdrawal\n [Q] Quit\n Select you Options: ","Automatic Teller Machine",JOptionPane.PLAIN_MESSAGE);
this is because the method showInputDialog() returns a String.
and also when comparing strings use:
if(a.equals(b)) {}
DavidKroukamp
Practically a Master Poster
693 posts since Dec 2011
Reputation Points: 282
Solved Threads: 169
program just, closes, what i mean is that, itdoes not continue
That is exactly what the code as posted does. There is nothing in the code that will make it continue.
What do you expect the code to do that it is not doing?
NormR1
Posting Expert
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
there is no particular error, its just that when i run the program, the automatic teller machine comes out, but when i select option, and hit enter, the program just, closes, what i mean is that, itdoes not continue
well yes because that
if(a==b) {}
will never execute and thats why you dont see the message box and it just exits.
change your code like we instructed and try again. if you enter nothing into the forst message box and press ok you should then see you the other message box pop up
DavidKroukamp
Practically a Master Poster
693 posts since Dec 2011
Reputation Points: 282
Solved Threads: 169
really? i did not know that, because in our lecture, our teacher said that we should put (String) in there!
no a string will not compare correctly if you use '=='.The == operator compares references of (String) objects and under normal circumstances equal strings don't automatically have the same reference, i.e. they are different objects. (Unless both are internalized, but normally you shouldn't consider it). Edit your code as instructed and it should work
DavidKroukamp
Practically a Master Poster
693 posts since Dec 2011
Reputation Points: 282
Solved Threads: 169
if(a==b) {}
if i will not use "==" then what should i use? i thought of using it, because if the letter that is inputted is "b" then the second window should come out, ...thats what im thinking about.. :(
read up i told you:
if(a.equals(b)) {
}
also note your string 'b' is nothing? its just ""? so the only time your message box will show is if you have no input and press ok
DavidKroukamp
Practically a Master Poster
693 posts since Dec 2011
Reputation Points: 282
Solved Threads: 169
Why is this statement commented out?
/*if (a.equalsIgnoreCase("B"))*/
NormR1
Posting Expert
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
so ..thanks i got it right now, so does that mean that, i dont need do create a String= b; anymore?? because it says here that variable "b" is not in use?
well it depends what is this code supposed to do:
if (a.equals(b)) {}
if it does nothing then yes you can but then you must remove that entire if statement and instead use this:
if (a.equalsIgnoreCase("b")) {}
DavidKroukamp
Practically a Master Poster
693 posts since Dec 2011
Reputation Points: 282
Solved Threads: 169
Do you want to compare the contents of the variable: a
to the contents of variable: b
or to the letter "B"?
BTW The variable names: a and b should be changed to names with meanings.
For example: userChoice would be better than a
NormR1
Posting Expert
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
that's perfect and yes you would do the same if thats what is required of you.
btw remove
string b="";
you dont need it.
also listen to what NormR1 said about variable naming you will lose marks! if thread is solved mark it as such
DavidKroukamp
Practically a Master Poster
693 posts since Dec 2011
Reputation Points: 282
Solved Threads: 169