Results 1 to 6 of 6

Thread: Do loop help

  1. #1
    07.350 is offline Member
    Join Date
    Feb 2013
    Posts
    7
    Rep Power
    0

    Default Do loop help

    I'm in need of a little help... I am trying to use a do loop and I'm stuck on how to make the second inner loop to when it prints "Calculate another interest?" if they type yes to loop up and ask the loan amt, interest, and term again? Any help would be great thanks!

    Java Code:
    public static void main(String[] args )
    	{
    		Scanner input = new Scanner(System.in);
    
    		do{
    			//Prompt user to enter loan amount
    			System.out.print("Enter Loan Amount: ");
    		    int principle = input.nextInt();
    		    
    		    //Prompt user to enter interest rate
    			System.out.print("Enter Yearly Interest Rate (1 to 100 percent): ");
    		    double interest = input.nextDouble(); 
    		    
    		  //Prompt user to enter term 
    		    System.out.print("Enter the Term (in months): ");
    		    int term = input.nextInt(); 
    		    
    		   if (principle > 0) 
    			   {
    			    	if (interest >= 0 && interest <= 100) 
    			    	{
    			    		if(term > 0)
    			    		{
    			    			 double simple= principle*(interest/100.0)*(term/12.0);
    			    			    double repaidSimple= simple+principle;
    			    			    double a = 1.0+((interest/100.0)/365.0);
    			    			    double b = (365.0*(term/12.0));
    			    			    double daily = Math.pow(a,b);
    			    			    double dailyCompound = (principle*daily)-principle; 		
    			    			    double repaidDaily = dailyCompound + principle; 
    			    			    double c = 1.0+((interest/100)/12.0);
    			    			    double d = (12.0*(term/12.0));
    			    			    double monthly = Math.pow(c, d);
    			    			    double monthlyCompound = (principle*monthly)-principle;
    			    			    double repaidMonthly = monthlyCompound + principle;
    			    			    int type = 0;
    			    			    
    			    			  //Prompt user to enter calculation type
    			    			   do{System.out.print("Enter Interest Calculation Type (1  Simple, 2  Monthly Compounded, 3  Daily Compounded):");
    			    			    type = input.nextInt();
    			    			    
    			    			   
    			    			   switch(type) 
    			    			    {
    			    			    case 1:System.out.println("Total calculated interest: "  +simple);
    			    				System.out.println("Total amount to be repaid: "  +repaidSimple);
    			    			    break;
    			    			    case 2:System.out.println("Total calculated interest: "  +monthlyCompound);
    			    				System.out.println("Total amount to be repaid: "  +repaidMonthly);
    			    			    break;
    			    			    case 3:System.out.println("Total calculated interest: "  +dailyCompound);
    			    				System.out.println("Total amount to be repaid: "  +repaidDaily);
    			    			    break;
    			    			    default: System.out.println("Calculation Type Error: You must select 1, 2 or 3. You entered " +type);
    			    			    break;
    			    			     		
    			    		        }
    			    			   }while(type<1||type>3);
    			    			}
    			    	    else
    			    	    {
    			    	    	System.out.println("Data Error: Term must be greater than zero. You entered " +term);
    			    	    	
    			    	    }
    			        }
    			    	else 
    			    	{
    			    		System.out.println("Data Error: Interest rate must be greater than or equal to zero and less than or equal to 100. You entered " +interest);
    			    	}
    			   }
    			   else
    			   {	
    			    		System.out.println("Data Error: Loan amount must be greater than zero. You entered " +principle);
    			   }
    		
    		
    		
    		do{
    			System.out.println("Calculate another loan interest (Yes/No)?");
    		}while(input.next().equalsIgnoreCase("yes")&& input.next().equalsIgnoreCase("no"));
    		
    		
    		
    		
    		}while (input.next().equalsIgnoreCase("yes"));
    		System.out.println("Thank you for using Interest Calculator!");
    	}
     }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,020
    Rep Power
    6

    Default Re: Do loop help

    First, your loops won't ever work properly as written:
    Java Code:
     do{         
        System.out.println("Calculate another loan interest (Yes/No)?");
      }while(input.next().equalsIgnoreCase("yes")&& input.next().equalsIgnoreCase("no"));
    Two problems here:

    Your first input.next() reads an answer so the next input.next() would either block or read something else that is not an answer.
    So you should normally do this:

    Java Code:
    String answer = input.next();
     do{         
        System.out.println("Calculate another loan interest (Yes/No)?");
      }while(answer.equalsIgnoreCase("yes")&& answer.equalsIgnoreCase("no"));
    There is still a huge problem. Answer can't be both yes and no at the same time so the loop will always
    fail.

    Try rewriting the code and putting in print statements as debugging aids.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    07.350 is offline Member
    Join Date
    Feb 2013
    Posts
    7
    Rep Power
    0

    Default Re: Do loop help

    Java Code:
    do{
    	System.out.println("Calculate another loan interest (Yes/No)?");
    		}while(input.next().equalsIgnoreCase("yes"));
    I see that this will cause a problem, I'm just having trouble where I should place the
    Java Code:
     do{
    and then where I should place the
    Java Code:
     }while(input.next().equalsIgnoreCase("yes"));
    to make the program ask for the principle, interest, and term again..
    Thanks for the quick response, I'm still new to using java so bear with me!

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,020
    Rep Power
    6

    Default Re: Do loop help

    Your big loop which does all the processing should be the inner loop. The outer loop is the one which asks if the user wants to repeat the process. So in essence, the outer loop and question/answer determines if the inner loop will run.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    07.350 is offline Member
    Join Date
    Feb 2013
    Posts
    7
    Rep Power
    0

    Default Re: Do loop help

    I think I have it, well it seems to be working properly for me... now when I need is when it prompts "Calculate another loan interest (Yes/No)?" if you type in Okay, maybe, basically anything but yes, no, y,n, etc it returns the error "Please enter 'Yes' or 'No'" Am I just setting it up wrong or how should I key that in? use an if statement?

    Java Code:
     public static void main(String[] args )
    	{
    		Scanner input = new Scanner(System.in);
    
    		do{
    			
    			do{
    				
    				
    			//Prompt user to enter loan amount
    			System.out.print("Enter Loan Amount: ");
    		    int principle = input.nextInt();
    		    
    		    //Prompt user to enter interest rate
    			System.out.print("Enter Yearly Interest Rate (1 to 100 percent): ");
    		    double interest = input.nextDouble(); 
    		    
    		  //Prompt user to enter term 
    		    System.out.print("Enter the Term (in months): ");
    		    int term = input.nextInt(); 
    		    
    		   if (principle > 0) 
    			   {
    			    	if (interest >= 0 && interest <= 100) 
    			    	{
    			    		if(term > 0)
    			    		{
    			    			 double simple= principle*(interest/100.0)*(term/12.0);
    			    			    double repaidSimple= simple+principle;
    			    			    double a = 1.0+((interest/100.0)/365.0);
    			    			    double b = (365.0*(term/12.0));
    			    			    double daily = Math.pow(a,b);
    			    			    double dailyCompound = (principle*daily)-principle; 		
    			    			    double repaidDaily = dailyCompound + principle; 
    			    			    double c = 1.0+((interest/100)/12.0);
    			    			    double d = (12.0*(term/12.0));
    			    			    double monthly = Math.pow(c, d);
    			    			    double monthlyCompound = (principle*monthly)-principle;
    			    			    double repaidMonthly = monthlyCompound + principle;
    			    			    int type = 0;
    			    			    
    			    			  //Prompt user to enter calculation type
    			    			   do{System.out.print("Enter Interest Calculation Type (1  Simple, 2  Monthly Compounded, 3  Daily Compounded):");
    			    			    type = input.nextInt();
    			    			    
    			    			   
    			    			   switch(type) 
    			    			    {
    			    			    case 1:System.out.println("Total calculated interest: "  +simple);
    			    				System.out.println("Total amount to be repaid: "  +repaidSimple);
    			    			    break;
    			    			    case 2:System.out.println("Total calculated interest: "  +monthlyCompound);
    			    				System.out.println("Total amount to be repaid: "  +repaidMonthly);
    			    			    break;
    			    			    case 3:System.out.println("Total calculated interest: "  +dailyCompound);
    			    				System.out.println("Total amount to be repaid: "  +repaidDaily);
    			    			    break;
    			    			    default: System.out.println("Calculation Type Error: You must select 1, 2 or 3. You entered " +type);
    			    			    break;
    			    			     		
    			    		        }
    			    			   }while(type<1||type>3);
    			    			}
    			    	    else
    			    	    {
    			    	    	System.out.println("Data Error: Term must be greater than zero. You entered " +term);
    			    	    	
    			    	    }
    			        }
    			    	else 
    			    	{
    			    		System.out.println("Data Error: Interest rate must be greater than or equal to zero and less than or equal to 100. You entered " +interest);
    			    	}
    			   }
    			   else
    			   {	
    			    		System.out.println("Data Error: Loan amount must be greater than zero. You entered " +principle);
    			   }
    		
    		
    		
    		   System.out.println("Calculate another loan interest (Yes/No)?");
    		}while(input.next().equalsIgnoreCase("yes"));
    			System.out.println("Thank you for using Interest Calculator!");
    		
    		
    		
    		}while (input.next().equalsIgnoreCase("yes"));
    		
    	}
     }

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,020
    Rep Power
    6

    Default Re: Do loop help

    Here is skeleton of what I think you want to do.

    Jim


    Java Code:
    boolean val = true;
    while (val) {
        System.out.print("Would you like to play a game?");
        String ans = input.next();
        if (ans.equal("yes")) {
             do {
                 
             // Your processing code goes here.
    
    
              } while ( /* Some inner test here */ );
        } else {
           val = false;  //causes outer loop to fall thru  (you could also use a break here)
        }
    }
    
    
    System.out.println("Thank you for playing");
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Converting a for loop to a do-while loop
    By awesom in forum New To Java
    Replies: 1
    Last Post: 11-23-2011, 04:02 PM
  2. Replies: 2
    Last Post: 11-07-2011, 03:25 AM
  3. Replies: 1
    Last Post: 10-01-2011, 03:12 AM
  4. JTextField loop 2x for-loop WEIRD!
    By Streetproject in forum AWT / Swing
    Replies: 2
    Last Post: 02-16-2011, 06:46 PM
  5. while-loop stopping on first loop
    By davester in forum New To Java
    Replies: 6
    Last Post: 06-26-2009, 09:46 PM

Posting Permissions

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