im trying to do a program like this:

i need to do a program like this:

Automatic Teller Machine Balance [D] Deposit [W] Withdrawal [Q] Quit select you OPtions:

once i have chosen an option then i should proceed here

if i choose b:

YOur Balance is __

if i chose D:

Enter you deposit amount:

if i choose W:

Enter the Withdrawal amount:

(if balance is >= withdrwal display transaction, if balance is < withdrawal display "insufficient Amount"
every after each of the choices a window like this should come out:

Would you Like to generate bank slip? YES NO

if yes: for balance:

BALANCE INQUIRY AMOUNT BALANCE:

Printing bank slip....
for deposit: DEPOSIT AMOUNT:_ printing book slip:

for withdrawal: your withdrwal amount:

if no: it will return to: Would you like another transaction?

this is what i did :


public static void main(String[] args) {
	int balance= 0;
	int deposit = 0;

    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));
   
    String B= "";
    if (a == B)
	{
		JOptionPane.showMessageDialog(null, "Your Balance is: ");
		
	
 
 
	}
 
	
	
	
	
	
	
	
	
	
	}

}

why cant this program run the way that i want it to?

Recommended Answers

All 17 Replies

a and B are String objects. you never should compare equality using '==' between two objects. this will work for primitives, but for objects, you should use the equals or the equals method (or a method derived of the equals method)

if ( a.equals(B))

a and B are String objects. you never should compare equality using '==' between two objects. this will work for primitives, but for objects, you should use the equals or the equals method (or a method derived of the equals method)

if ( a.equals(B))

so i did this:

String D="";
   String W="";
   
   if(a.equals(B))
	{
		JOptionPane.showMessageDialog(null, "Your Balance is: ");
		
	}
 
    else if (a.equals(D))
    {
	
	int dep =Integer.parseInt(JOptionPane.showInputDialog (null,"Enter Your Deposit Amount" )); 
	
	
	
    }
	
    else if (a.equalsIgnoreCase(W))
	
    	int draw= Integer.parseInt(JOptionPane.showInputDialog(null," Enter the Withdrawal Amount:"));
	
	}

}

its not yet finish, but i dont know why when i choose an Option, for example B, my If in B does not come out :(

B is an empty String object.
replace

B= "";

by

B= "B";

now if you enter B as value in your inputBox, that if will return true and the print will be performed.
if you are only going to input one character, you can use chars instead of String objects, but first try it as I described above :)

B is an empty String object.
replace

B= "";

by

B= "B";

now if you enter B as value in your inputBox, that if will return true and the print will be performed.
if you are only going to input one character, you can use chars instead of String objects, but first try it as I described above :)

sir i have another question, our teacher said that even if she put a lower case or a big letter, same thing should come up, how do i do that?

B is an empty String object.
replace

B= "";

by

B= "B";

now if you enter B as value in your inputBox, that if will return true and the print will be performed.
if you are only going to input one character, you can use chars instead of String objects, but first try it as I described above :)

and here:
if(a.equals(B))
{
JOptionPane.showMessageDialog(null, "Your Balance is: ","BALANCE INQUIRY",JOptionPane.PLAIN_MESSAGE );
JOptionPane.showConfirmDialog(null, "Would you like to generate bank slip?");

if ()
}

if the answer in the confirm dialog is yes or no, how do i create another if statement in there? because

if my teacher will click yes then i will go to the next step, but if she clicks no, i will as her if she would like to do another transaction :)!!

well, as I said, if you use (String) Objects, you either use the equals method, or a method derived from equals.

when using Strings, you can do several things:

if ( a.equals("b") || a.equals("B"))

or, you can adjust a bit, by making it an uppercase

a = a.toUpperCase();
if ( a.equals("B"))

or, the best way is to use the equalsIgnoreCase method, provided by the String class:

if ( a.equalsIgnoreCase("B"))

this will return true if a is either 'b' or 'B'

and here:
if(a.equals(B))
{
JOptionPane.showMessageDialog(null, "Your Balance is: ","BALANCE INQUIRY",JOptionPane.PLAIN_MESSAGE );
JOptionPane.showConfirmDialog(null, "Would you like to generate bank slip?");

if ()
}

if the answer in the confirm dialog is yes or no, how do i create another if statement in there? because

if my teacher will click yes then i will go to the next step, but if she clicks no, i will as her if she would like to do another transaction :)!!

if you take a look at the api of the JOptionPane class, you'll see that showConfirmDialog returns an int.
the value of this int is decided by what option you've chosen.

try this:

for ( int i = 0; i < 5; i++){
  int input = JOptionPane.showConfirmDialog(null, "Would you like to generate bank slip?");
System.out.println("the value for that button = " + input);
}

run this code and press a different button each iteration, that way you can easily spot which value is returned for 'OK','Cancel', ...

well, as I said, if you use (String) Objects, you either use the equals method, or a method derived from equals.

when using Strings, you can do several things:

if ( a.equals("b") || a.equals("B"))

or, you can adjust a bit, by making it an uppercase

a = a.toUpperCase();
if ( a.equals("B"))

or, the best way is to use the equalsIgnoreCase method, provided by the String class:

if ( a.equalsIgnoreCase("B"))

this will return true if a is either 'b' or 'B'

oh thank you thank u! sir, sir can i ask more questions if i have problems regarding my program ? :)

oh thank you thank u! sir, sir can i ask more questions if i have problems regarding my program ? :)

that's the whole idea of the forum :)

that's the whole idea of the forum :)

sir sir!!!

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));
   
   String B= "B";
   String D="D";
   String W="W";
   
   int balance= 0;
   int deposit = 0;
   int withdraw= 0;
   int bal = 0;
   int wht = 0;
int ans;
int trans = 0;
   
   if(a.equalsIgnoreCase(B))
	{
		JOptionPane.showMessageDialog(null, "Your Balance is: ","BALANCE INQUIRY",JOptionPane.PLAIN_MESSAGE );
		ans=JOptionPane.showConfirmDialog(null, "Would you like to generate bank slip?","Generate Bank Slip",JOptionPane.YES_NO_OPTION);
		
		if (ans == 1){ //no
	JOptionPane.showConfirmDialog(null,"Would You Like Another Transaction?","Another Transaction",JOptionPane.YES_NO_OPTION);
		if (trans == 1)
		{
		JOptionPane.showMessageDialog(null, "Thanks For Using My Program!!");
		}
		else if (trans == 0)
		{ 
			 String b =(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));
		
		
		
		
		}
		
		}
	
		else if (ans == 0){ // yes
			JOptionPane.showMessageDialog(null, "Good Afternoon");
		}
	
	
	
	}
   
   
   
   
   
   
 
    else if (a.equalsIgnoreCase(D))
    {
	
	int dep =Integer.parseInt(JOptionPane.showInputDialog (null,"Enter Your Deposit Amount" )); 
	
	balance = dep - wht;
	
    }
	
    else if (a.equalsIgnoreCase(W))
	
    	 wht= Integer.parseInt(JOptionPane.showInputDialog(null," Enter the Withdrawal Amount:"));
	
   
   
   
   
   
	}

}

as u can see im using lots of if else, but our lesson for yesterday was do while, but i dont know where to use do while in my program! ?

a while loop is used to iterate over a block of code as long a certain expression returns true.
in your case, your application should run while the user did not input 'Q', so, it would be something like this:

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));
   
   String B= "B";
   String D="D";
   String W="W";
   String Q="Q";

while ( !a.equalsIgnoreCase(Q) ){
// here comes the rest of your code


// at this point, you'll need to ask the user for input again, otherwise, the value
// of a will never change, and so you'll be stuck in an infinite loop
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));
}

a while loop is used to iterate over a block of code as long a certain expression returns true.
in your case, your application should run while the user did not input 'Q', so, it would be something like this:

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));
   
   String B= "B";
   String D="D";
   String W="W";
   String Q="Q";

while ( !a.equalsIgnoreCase(Q) ){
// here comes the rest of your code


// at this point, you'll need to ask the user for input again, otherwise, the value
// of a will never change, and so you'll be stuck in an infinite loop
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));
}

how about do while sir??? how do i use it? whats the difference between while only, from do while?

how about do while sir??? how do i use it? whats the difference between while only, from do while?

I would suggest making a new thread, as you already marked this as solved, so people may not know that there are still posts happening here? Just a suggestion. however here is a link to answer your question http://docs.oracle.com/javase/tutorial/java/nutsandbolts/while.html

I would suggest making a new thread, as you already marked this as solved, so people may not know that there are still posts happening here? Just a suggestion. however here is a link to answer your question http://docs.oracle.com/javase/tutorial/java/nutsandbolts/while.html

im afraid to create new thread! because, if i will creat new thread ..other people will think that im flooding the site :(

im afraid to create new thread! because, if i will creat new thread ..other people will think that im flooding the site :(

are you flooding the site with proper java questions? yes, so do not worry, its not like you are spamming or anything, you are asking honest questions which pertain to java

im afraid to create new thread! because, if i will creat new thread ..other people will think that im flooding the site :(

considering the amount of threads already on the forum, I wouldn't worry about it :)
it is better to start a new thread for several reasons:
1. it's about another matter
2. as cOrRuPtG3n3t!x already mentioned, this thread is marked 'solved', so less people will be reading it, which may cause you to miss out on a lot of help

but to answer your question about the do-while
the main difference is when you run your check
in the while loop, you check if the expression is valid before you run your first iteration.
if the initial value of the expression is false, this means the block of code in your while loop will never run.
in the do while, you check your expression after the first time you run your code block, so even if the expressions original value is false, you will always run your code block at least once.

are you flooding the site with proper java questions? yes, so do not worry, its not like you are spamming or anything, you are asking honest questions which pertain to java

thank you!!! now i feel comfortable :) thank u sir! ok ill post a new thread

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.