Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 15 of 15

Thread: Can someone please tell me why my do while loop isn't working?

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Can someone please tell me why my do while loop isn't working?

    Hi, everyone, can someone please take a look at my code and let me know why my do while loop does not work? The program runs properly until I add the do while loop. I referenced my book and have no idea how to do this differently.

    Ashley

    import java.util.Scanner;
     
    public class Project2
    {
    	public static void main(String[] args)
    	{
     
    		//variables
    		double future = 0;
    		double rate = 0;
    		double numYears = 0;
    		char runAgain;
    		String input;
     
    		System.out.println("This will find present value.");
    		presentValue(future, rate, numYears);
     
     
    	}
     
    	public static double presentValue(double future, double rate, double numYears)
    	{
     
    		//creat a scanner object to read input.
    		Scanner keyboard = new Scanner(System.in);
     
    		//Get input from user
     
    			do
    			{
     
    			System.out.println("What do you want the future value to be? ");
    			future = keyboard.nextDouble();
     
    			System.out.println("What is the annual percentage rate? ");
    			rate = keyboard.nextDouble();
     
    			System.out.println("How long will you keep your money in the account? (in years)");
    			numYears = keyboard.nextDouble();
     
    			//present = (future)/(1+rate)^numYears
     
    			//variables
    			double present = (future)/Math.pow(1+rate,numYears);
     
    			System.out.println("The present value is " + present + " .");
     
    			System.out.println("Would you like to run the program again? (Enter 'y' for yes " +
    									" or 'n' for no");
    			input = keyboard.nextLine();
    			repeat = input.charAt(0);
     
    			} while (runAgain == 'y' || runAgain == 'Y');
     
    			return present;
     
     
    		}
    }

    I also tried adding the "while" part after the return statement and that doesn't change anything. Thank you in advance.

    Ashley


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Can someone please tell me why my do while loop isn't working?

    The first problem is that your code won't compile...is this what you are asking about? What are the errors?

  3. #3
    Junior Member
    Join Date
    Oct 2011
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Can someone please tell me why my do while loop isn't working?

    Yes I know it won't compile. It did fine before I tried adding the loop. Here are the errors:

    Project2.java:50: error: cannot find symbol
    			input = keyboard.nextLine();
    			^
      symbol:   variable input
      location: class Project2
    Project2.java:51: error: cannot find symbol
    			runAgain = input.charAt(0);
    			^
      symbol:   variable repeat
      location: class Project2
    Project2.java:51: error: cannot find symbol
    			runAgain = input.charAt(0);
    			         ^
      symbol:   variable input
      location: class Project2
    Project2.java:53: error: cannot find symbol
    			} while (runAgain == 'y' || runAgain == 'Y');
    			         ^
      symbol:   variable runAgain
      location: class Project2
    Project2.java:53: error: cannot find symbol
    			} while (runAgain == 'y' || runAgain == 'Y');
    			                            ^
      symbol:   variable runAgain
      location: class Project2
    Project2.java:55: error: cannot find symbol
    			return present;
    			       ^
      symbol:   variable present
      location: class Project2
    6 errors

  4. #4
    Member
    Join Date
    Oct 2011
    Posts
    36
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Can someone please tell me why my do while loop isn't working?

    In your code, the variable runAgain is not assigned, so you can not continue to run your code repeatedly.

    just modify the line: repeat = input.charAt(0) to runAgain = input.charAt(0)

  5. #5
    Junior Member
    Join Date
    Oct 2011
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Can someone please tell me why my do while loop isn't working?

    Yes I noticed that. I fixed that and still no fix.

  6. #6
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Can someone please tell me why my do while loop isn't working?

    Yes I know it won't compile.
    This is important information to disclose - as well as those errors - if you want more expedient help, especially when 'while loop won't work' gives little information but suggests a runtime problem, not a compilation problem.

    "cannot find symbol"...where are those variables defined? Are they String? char? int?

  7. #7
    Junior Member
    Join Date
    Oct 2011
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Can someone please tell me why my do while loop isn't working?

    Sorry, I'll make sure to include that next time. They are declared in the main where the other variables are declared. The other inputs work just fine. Again, if the while loop is taken out then the program executes fine and the output is correct. I just have to add the do while loop to give the user as many repeat attempts as they wish, as long as they enter "y" or "Y".

    Here are the variables, and they are declared in the main:

    //variables
    		double future = 0;
    		double rate = 0;
    		double numYears = 0;
    		char runAgain;
    		String input;

    Ashley

  8. #8
    Member
    Join Date
    Oct 2011
    Posts
    36
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Can someone please tell me why my do while loop isn't working?

    char runAgain;
    String input;

    should be declared in presentValue

  9. #9
    Junior Member
    Join Date
    Oct 2011
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Can someone please tell me why my do while loop isn't working?

    I added them to presentValue. Now it won't compile still, but I have just one error:


    Project2.java:59: error: cannot find symbol
    return present;
    ^
    symbol: variable present
    location: class Project2
    1 error
    Last edited by JavaAsh; October 20th, 2011 at 03:15 PM. Reason: EDITED: My mistake on moving the variables

  10. #10
    Member
    Join Date
    Oct 2011
    Posts
    36
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Can someone please tell me why my do while loop isn't working?

    present should be declared before while loop

  11. #11
    Junior Member
    Join Date
    Oct 2011
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Can someone please tell me why my do while loop isn't working?

    It is declared before the while. I moved it up to declare it before the 'do' and and it runs, but then gets an error, incorrect output of "0.0", and then it closes before it gives the option of running again. I will post my new code and the new errors as well.


    import java.util.Scanner;
     
    public class Project2
    {
    	public static void main(String[] args)
    	{
     
    		//variables
    		double future = 0;
    		double rate = 0;
    		double numYears = 0;
     
     
    		System.out.println("This will find present value.");
    		presentValue(future, rate, numYears);
     
     
     
     
    	}
     
    	public static double presentValue(double future, double rate, double numYears)
    	{
     
    		//creat a scanner object to read input.
    		Scanner keyboard = new Scanner(System.in);
     
    		char runAgain;
    		String input;
     
    		//Get input from user
     
    		double present = (future)/Math.pow(1+rate,numYears);
     
    			do
    			{
     
    			System.out.println("What do you want the future value to be? ");
    			future = keyboard.nextDouble();
     
    			System.out.println("What is the annual percentage rate? ");
    			rate = keyboard.nextDouble();
     
    			System.out.println("How long will you keep your money in the account? (in years)");
    			numYears = keyboard.nextDouble();
     
    			//present = (future)/(1+rate)^numYears
     
    			//variables
     
     
    			System.out.println("The present value is " + present + " .");
     
     
    			System.out.println("Would you like to run the program again? (Enter 'y' for yes " +
    									" or 'n' for no");
    			input = keyboard.nextLine();
    			runAgain = input.charAt(0);
     
    			} while (runAgain == 'y' || runAgain == 'Y');
     
    			return present;
     
     
     
    		}
    }

    ERRORS:

     ----jGRASP exec: java Project2
     
    This will find present value.
    What do you want the future value to be? 
    10000.00
    What is the annual percentage rate? 
    .0575
    How long will you keep your money in the account? (in years)
    10
    The present value is 0.0 .
    Would you like to run the program again? (Enter 'y' for yes  or 'n' for no
    Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0
    	at java.lang.String.charAt(String.java:695)
    	at Project2.presentValue(Project2.java:58)
    	at Project2.main(Project2.java:15)
     
     ----jGRASP wedge2: exit code for process is 1.
     ----jGRASP: operation complete.

  12. #12
    Member
    Join Date
    Oct 2011
    Posts
    36
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Can someone please tell me why my do while loop isn't working?

    replace keyboard.nextLine() to keyboard.next()

  13. #13
    Junior Member
    Join Date
    Oct 2011
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Can someone please tell me why my do while loop isn't working?

    It compiles now, but the output is 0.0. I'm pretty sure that's because I have the equation before it actually receives the input. But if I put the equation after the input it gives me that same error again with the "return present;" statement.

  14. #14
    Member
    Join Date
    Oct 2011
    Posts
    36
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Can someone please tell me why my do while loop isn't working?

    It seems working fine on my PC:
     
    What do you want the future value to be? 
    2
    What is the annual percentage rate? 
    3
    How long will you keep your money in the account? (in years)
    4
    The present value is 0.0078125 .
    Would you like to run the program again? (Enter 'y' for yes  or 'n' for no
    y
    What do you want the future value to be? 
    10000.00
     
    What is the annual percentage rate? 
    .0575
    How long will you keep your money in the account? (in years)
    10
    The present value is 5717.369223381666 .
    Would you like to run the program again? (Enter 'y' for yes  or 'n' for no

  15. #15
    Junior Member
    Join Date
    Oct 2011
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Can someone please tell me why my do while loop isn't working?

    Weird. No idea why mine won't work then. Thank you for your help.

Similar Threads

  1. Loop conditions not working
    By bmxershane in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 1st, 2011, 02:25 AM
  2. [SOLVED] Thread.sleep() in while loop not working
    By mds1256 in forum Threads
    Replies: 4
    Last Post: January 13th, 2011, 06:02 AM
  3. [SOLVED] Loop Breaking Not Working
    By aussiemcgr in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 25th, 2010, 09:06 PM
  4. for Loop for my 2D collision not working??
    By DarrenReeder in forum Loops & Control Statements
    Replies: 1
    Last Post: March 7th, 2010, 10:05 AM
  5. Replies: 4
    Last Post: January 27th, 2009, 12:03 AM