do-while loop

im working on a basic atm example using a do-while loop, but the outpu

Page 1 of 1

4 Replies - 3466 Views - Last Post: 06 July 2009 - 03:31 PM Rate Topic: -----

#1 jinnyishere  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 46
  • View blog
  • Posts: 127
  • Joined: 06-July 09

do-while loop

Posted 06 July 2009 - 01:46 PM

	do
		{
			System.out.println("Enter what would you like to do\n");
			System.out.println("D--Deposite, W--Withdraw, C--Check Balance, E--Exit");
			System.out.print("Enter your selection now: ");
			choices = key.nextLine();
		
			if(choices.equals("W") || choices.equals("w"))
			{
				System.out.println("You have selected with draw\n");
				System.out.print("Please enter the amount you want to with draw: \n");
				withdraw = key.nextInt();
				
				totalamount -= withdraw;
				

			}
			else if(choices.equals("C") || choices.equals("c"))
			{
				System.out.println("You have selected check balance\n");
				System.out.print("Your account balance is " + totalamount + "\n" );
				
			}
		
			else if(choices.equals("d") || choices.equals("D"))
			{
				System.out.println("You have selected deposite\n");
				System.out.print("Please enter the amount you want to deposite: ");
				depot = key.nextInt();
				
				totalamount += depot;	
				
			}
			else if(choices.equals("E") || choices.equals("e"))
			{
				System.exit(0);
			}
		}while(true);
	



here is the output from my code

Please enter your pin number: 111
Enter what would you like to do

D--Deposite, W--Withdraw, C--Check Balance, E--Exit
Enter your selection now: Enter what would you like to do

D--Deposite, W--Withdraw, C--Check Balance, E--Exit
Enter your selection now:


So, whenever it gets into the do-while loop it prints out this part twice

D--Deposite, W--Withdraw, C--Check Balance, E--Exit
Enter your selection now: Enter what would you like to do

D--Deposite, W--Withdraw, C--Check Balance, E--Exit
Enter your selection now:

Can anyone please point out to me where is my error, thx?

Is This A Good Question/Topic? 0
  • +

Replies To: do-while loop

#2 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: do-while loop

Posted 06 July 2009 - 01:51 PM

change this line
choices = key.nextLine();



to
choices = key.next();


Was This Post Helpful? 0
  • +
  • -

#3 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10823
  • View blog
  • Posts: 40,349
  • Joined: 27-December 08

Re: do-while loop

Posted 06 July 2009 - 02:08 PM

Mostyfriedman has provided you with the correct solution, but let me expand on this. If you execute a print() statement,then always use the Scanner next() method to retrieve input. If you use the println() method, then you can use the nextLine() method; however, I still prefer the next() method. The reason is because it retrieves the next token, and a character is only a single token. If you are reading a file or tons of console input, then the nextLine() method may work better. It all depends on the volume and organization of the input. Hope this helps!
Was This Post Helpful? 1
  • +
  • -

#4 jinnyishere  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 46
  • View blog
  • Posts: 127
  • Joined: 06-July 09

Re: do-while loop

Posted 06 July 2009 - 02:24 PM

Thx for all the help...now my program works fine...

Wow!! macosxnerd101 thx for the explaination...it is very clear and I can't believe they have never taught us how to distinguish the nextLine() and next() in any of my Java course....In addition I don't recall books mentioning about it either...they would just tell you to use different reader or readLine method...
Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10823
  • View blog
  • Posts: 40,349
  • Joined: 27-December 08

Re: do-while loop

Posted 06 July 2009 - 03:31 PM

No problem! Glad I could be of assistance. Just for future reference, if you need to look at a Java class definition efficiently, google the name of the class and the keyword "java." You should come up w/a sun microsystems site with the class name. It will cover all the constructors, methods & accessible fields w/descriptions for all.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1