Results 1 to 6 of 6
  1. #1
    SrJustEasy is offline Member
    Join Date
    Dec 2016
    Posts
    5
    Rep Power
    0

    Default do-while loop problem

    This is the program (sorry if it's in italian, i tried to traduce something to help you understand it).
    Java Code:
    import java.util.Scanner;
    
    public class Esercizio3 {
    	public static final double TASSO_INTERESSE_ANNUO = 0.12;
    	public static final double TASSO_INTERESSE_MENSILE = TASSO_INTERESSE_ANNUO/12;	
    	public static void main(String[] args) {
    		
    		Scanner tastiera = new Scanner(System.in);
    		double balance, withdrawal, deposit;
    		String risposta = "yes";
    		String movimento;
    		System.out.println("Inserire il mese (month) in cui ci troviamo (number)");
    		int month = tastiera.nextInt();
    		System.out.println("Inserire il proprio saldo iniziale (initial balance).");
    		balance = tastiera.nextDouble();
    		
    		do {
    			System.out.println("Si vuole fare un prelievo (withdrawal), un deposito (deposit) o nulla (nothing)?");
    			movimento = tastiera.nextLine();
    			if (movimento.equalsIgnoreCase("prelievo")) {
    				System.out.println("Inserire quanto vuole prelevare");
    				withdrawal = tastiera.nextDouble();
    				if (withdrawal < balance) {
    					balance = balance - withdrawal;
    				}
    				else {
    					System.out.println("Non ci sono abbastanza soldi");
    				}
    			}
    			else if (movimento.equalsIgnoreCase("deposito")) {
    				System.out.println("Inserire quanto vuole depositare");
    				deposit = tastiera.nextDouble();
    				balance = deposit + balance;
    			}
    			else {
    				continue;
    			}
    			balance = balance * (1-TASSO_INTERESSE_MENSILE);
    			System.out.println("Il saldo corrente corrisponde a " + balance);
    			if (month < 12) {
    				month++;
    			}
    			else {
    				month = 1;
    			}
    			System.out.println("Desidera procedere con un altro mese?");
    			risposta = tastiera.nextLine();
    		} while (risposta.equalsIgnoreCase("yes"));
    		
    		tastiera.close();
    	}
    	
    }
    I have a problem because, when i run it, first of all the String "Si vuole fare un prelievo, un deposito o nulla" is run twice, and i can't understand why.
    Then, the bigger problem, is the fact that i can't say yes to the last answer, so the do-while loop run only one time. can someone help me please?
    Last edited by SrJustEasy; 12-08-2016 at 02:03 PM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: do-while loop problem

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.

    The problem could be with the way the Scanner class handles the lineend character from the keyboard. Mixing calls to nextLine with calls to other next... methods will result in the nextLine() method returning an empty String from the lineend character left in the Scanner's buffer.
    Solution is to call nextLine to clear the lineend character before calling the nextLine method for data.
    Last edited by Norm; 12-08-2016 at 02:04 PM.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    SrJustEasy is offline Member
    Join Date
    Dec 2016
    Posts
    5
    Rep Power
    0

    Default Re: do-while loop problem

    Quote Originally Posted by Norm View Post
    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    ty, here you are

  4. #4
    SrJustEasy is offline Member
    Join Date
    Dec 2016
    Posts
    5
    Rep Power
    0

    Default Re: do-while loop problem

    Quote Originally Posted by Norm View Post
    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.

    The problem could be with the way the Scanner class handles the lineend character from the keyboard. Mixing calls to nextLine with calls to other next... methods will result in the nextLine() method returning an empty String from the lineend character left in the Scanner's buffer.
    Solution is to call nextLine to clear the lineend character before calling the nextLine method for data.
    sorry, i didn't understand so much. how would you correct this thing on the program?

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: do-while loop problem

    After calling next... which leaves a endline character in Scanner's buffer, call nextLine one time to clear the endline before calling nextLine to get the user's input.

    Write a small test program to experiment with the problem and its solution.
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: do-while loop problem

    Or, which might be easier as your input appears to be single words, use the next() method instead of nextLine().
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 2
    Last Post: 11-07-2011, 03:25 AM
  2. for loop problem
    By donal-ctn in forum JCreator
    Replies: 1
    Last Post: 01-31-2011, 08:26 PM
  3. For Loop Problem
    By saqib15 in forum New To Java
    Replies: 6
    Last Post: 02-20-2010, 01:59 AM
  4. simple line problem / for loop problem
    By helpisontheway in forum New To Java
    Replies: 1
    Last Post: 11-17-2009, 07:12 AM
  5. Help with a loop-like problem
    By Jnoobs in forum New To Java
    Replies: 1
    Last Post: 10-14-2009, 02:15 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •