954,545 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

PLease help me with this new assignment that will be pass tommorow!

So here it is, this is my new problem, we were assigned to create a simple ATM Machine:

Automatic Teller Machine
[B] Balance Machine
[D] Deposit
[W] Withdrawal
[Q] Quit
Select you option:
(input letter here)

when i choose balance this window should pop out:

Your Balance is 0.00
OK ( <--that "ok" is in showConfirmDialog)


this is the code that i did but cant figure out what is the error in there!
this program should accept either small or big letter

public static void main(String[] args) {
		// TODO Auto-generated method stub

    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));
    String b = "";
    
    if(a==b ){
  
    	/*if (a.equalsIgnoreCase("B"))*/
    	
    	JOptionPane.showConfirmDialog(null,"Your Balance is 0.00","Balance Inquiry",JOptionPane.PLAIN_MESSAGE );
    
    }
        
	
	}

}


thanks for the help!

Ms New to Java
Junior Poster
135 posts since Jan 2012
Reputation Points: 10
Solved Threads: 1
 
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
Moderator
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
Team Colleague
693 posts since Dec 2011
Reputation Points: 282
Solved Threads: 169
 

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"))*/

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

Ms New to Java
Junior Poster
135 posts since Jan 2012
Reputation Points: 10
Solved Threads: 1
 
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
Moderator
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
Team Colleague
693 posts since Dec 2011
Reputation Points: 282
Solved Threads: 169
 
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?
if(a==b ){

/*if (a.equalsIgnoreCase("B"))*/

JOptionPane.showConfirmDialog(null,"Your Balance is 0.00","Balance Inquiry",JOptionPane.PLAIN_MESSAGE );

}

there, that part tight there is the thing that i want to come out :(

Ms New to Java
Junior Poster
135 posts since Jan 2012
Reputation Points: 10
Solved Threads: 1
 
also, you don't need this (String) cast, since showInputDialog will give you a String object as result.


really? i did not know that, because in our lecture, our teacher said that we should put (String) in there!

Ms New to Java
Junior Poster
135 posts since Jan 2012
Reputation Points: 10
Solved Threads: 1
 
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
Team Colleague
693 posts since Dec 2011
Reputation Points: 282
Solved Threads: 169
 

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


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.. :(

Ms New to Java
Junior Poster
135 posts since Jan 2012
Reputation Points: 10
Solved Threads: 1
 

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
Team Colleague
693 posts since Dec 2011
Reputation Points: 282
Solved Threads: 169
 

Why is this statement commented out?
/*if (a.equalsIgnoreCase("B"))*/

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

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


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?

Ms New to Java
Junior Poster
135 posts since Jan 2012
Reputation Points: 10
Solved Threads: 1
 
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
Team Colleague
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
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 
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

so this is what i did and it worked:

package botilparin;

import javax.swing.JOptionPane;

public class Rosselleatm {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

    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));
    
    
    String b = "";
    
    
   
  
    	if (a.equalsIgnoreCase("b"))
    	
    	JOptionPane.showConfirmDialog(null,"Your Balance is 0.00","Balance Inquiry",JOptionPane.PLAIN_MESSAGE );
   }
    }


should i also do the same thing on the other options likw quit, withdrawal and others?

Ms New to Java
Junior Poster
135 posts since Jan 2012
Reputation Points: 10
Solved Threads: 1
 

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
Team Colleague
693 posts since Dec 2011
Reputation Points: 282
Solved Threads: 169
 

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


ohoh!!! yes no problem!

Ms New to Java
Junior Poster
135 posts since Jan 2012
Reputation Points: 10
Solved Threads: 1
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: