4 Replies - 567 Views - Last Post: 23 September 2012 - 10:25 PM Rate Topic: -----

#1 dasrhino  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 23-September 12

Using the if method making a bank account.

Posted 23 September 2012 - 09:59 PM

Ok so this is my first posting of many as I am just starting out coding. I am making a simple bank account application using if statements to determine whether you want to deposit or withdraw funds. I have an issue with the scanner where i put
 String myAction = input.nextLine(); 
I think. My program just ends. If i switch it to
 String myAction = input.next() 
it lets me enter deposit or withdraw and just ends. Any ideas or help out there? Much thanks advance!



import java.util.Scanner;

public class BankAccount
{
	public static void main (String[] args)
	{
	
	Scanner input = new Scanner( System.in );
	
	int accountNumber = 123456;
	int password = 654321;
	float balance = 100;
	String withdraw = "withdraw"; 
	String deposit = "deposit";
	
	System.out.println ("\nPlease enter your account number:");
	int myAccountNumber = input.nextInt();
	
	System.out.println ("\nPlease enter your password:");
	int myAccountPassword = input.nextInt();
	
	if ( myAccountNumber == accountNumber && myAccountPassword == password )
	
	{
		System.out.println("\nWelcome to Woody's bank!");
		System.out.printf("\nYour account balance is:$%.2f\n", balance);
		System.out.println("\nWould you like to deposit or withdraw funds?");
		
		String myAction = input.nextLine();
		
		if (myAction == withdraw)
			{ 
			System.out.println("\nHow much?");
			float withdrawAmount = input.nextFloat();
			balance = balance - withdrawAmount;
			System.out.printf("Your balance is now:%.2f", balance);
			}
		
		if (myAction == deposit)
			{
			System.out.println("\nHow much?");
			float depositAmount = input.nextFloat();
			balance = balance + depositAmount;
			System.out.printf("Your balance is now:%.2f", balance);
			}
			
	}
	
	if( myAccountNumber != accountNumber || myAccountPassword != password)
	
	{
	System.out.println("\nNice try ATM shutting down.");
	}					
	
	}	
}



Is This A Good Question/Topic? 0
  • +

Replies To: Using the if method making a bank account.

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2197
  • View blog
  • Posts: 5,224
  • Joined: 10-September 10

Re: Using the if method making a bank account.

Posted 23 September 2012 - 10:05 PM

Use the equals() method to compare Strings.
Was This Post Helpful? 1
  • +
  • -

#3 dasrhino  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 23-September 12

Re: Using the if method making a bank account.

Posted 23 September 2012 - 10:10 PM

View PostGregBrannon, on 23 September 2012 - 10:05 PM, said:

Use the equals() method to compare Strings.


i replaced it with:

if (myAction.equals("deposit")



and does the same thing. just terminates after it asks whether you want to deposit or withdraw. grrrr i think it is somewhere else because i had it working for just withdraw but when i did it for either or it failed. Thanks for the help!
Was This Post Helpful? 0
  • +
  • -

#4 dasrhino  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 23-September 12

Re: Using the if method making a bank account.

Posted 23 September 2012 - 10:19 PM

I got it thanks so much greg!
Was This Post Helpful? 0
  • +
  • -

#5 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2197
  • View blog
  • Posts: 5,224
  • Joined: 10-September 10

Re: Using the if method making a bank account.

Posted 23 September 2012 - 10:25 PM

I assume you flushed the input buffer with an input.nextLine() after line 20.

Good job figuring it out.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1