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

Automatic Teller Machine
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!

Recommended Answers

All 18 Replies

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

String a =(String)(JOptionPane.showInputDialog(null, "Automatic Teller Machine\n 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.

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)) {}

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

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?

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

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

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!

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

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

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

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

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?

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")) {}

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

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?

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

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!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.