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 10 of 10

Thread: While loop messed up

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question While loop messed up

    First off thanks to anyone willing to give their time to help a newbie.

    So im very new to java/programming and im having trouble with a program. I believe the while loop is messed up as im new to using them. The program is used to calculate inflation in price of an object. The user enters the price, amount of years, and percent inflation. When i try to run this, it cannot print my "finalamount" variable, and i cant figure out what i messed up. Any help/suggestions would be great.

    import java.util.Scanner;
     
    public class inflationcalculator {
     
        public static void main(String[] args) {
     
     
     /*Gather user input*/
     
     
        System.out.print("Input the current cost of the item, just the number:");
        Scanner scan = new Scanner(System.in);
        int amount = scan.nextInt();
     
        System.out.print("Input the rate of inflation, as a percentage without the percent sign:");
     
        int inflat = scan.nextInt();
     
        System.out.print("Input the number of years, must be a whole number:");
     
        int numyear = scan.nextInt();
     
     
     /*Calculate the inflation according to the users input*/
     
    int counter = 0;
    double percent = (inflat/100);
    double amountedit = amount;
    	while(counter < numyear){
    		double finalamount = (amountedit + (amountedit*percent));
    		counter++;
     
    	}
     
     
     
     /*Print/results screen*/
     
    System.out.print("At ");
    System.out.print(inflat);
    System.out.print(" percent inflation per year, the cost in ");
    System.out.print(numyear);
    System.out.print(" year(s) will be $");
    System.out.print(finalamount);
    System.out.print(".");
     
     
        }
    }


  2. #2
    Junior Member
    Join Date
    Feb 2012
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: While loop messed up

    Im pretty certain the mistake lies within the loop, but im not sure, thats why i didnt post in the loop category.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,652
    Thanks
    45
    Thanked 1,959 Times in 1,933 Posts

    Default Re: While loop messed up

    it cannot print my "finalamount" variable
    What does the program print out when it is executed?

    If you get errors, please copy and paste here the full text of the error message.

  4. #4
    Junior Member
    Join Date
    Feb 2012
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: While loop messed up

    i get this error "error: cannot find symbol "line 51", of which is where i tell the program to print finalamount.

  5. #5
    Junior Member
    Join Date
    Feb 2012
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: While loop messed up

    this error: cannot find symbol
    System.out.print(finalamount);
    ^
    symbol: variable finalamount
    location: class inflationcalculator
    1 error

    Process completed.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,652
    Thanks
    45
    Thanked 1,959 Times in 1,933 Posts

    Default Re: While loop messed up

    The definition for the finalamount variable is inside inside of a pair of {}s and is not known outside of those {}s.
    It is out of scope for where you are trying to use it. Read up on variable scope and variable definition.

    You need to move the definition of the variable out of the inner {}s so it is within the same pair of {}s as where you are trying to use its value.

  7. The Following User Says Thank You to Norm For This Useful Post:

    jak15 (February 22nd, 2012)

  8. #7
    Junior Member
    Join Date
    Feb 2012
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: While loop messed up

    It compiles if i move the curly brace down to include the ending print statements but it doesnt do at all what i need it to. It just prints the Final statement over and over according to how many years the user enters. Also it doesnt calculate the finalamount correctly. I must have the formula wrong.
     import java.util.Scanner;
     
    public class inflationcalculator {
     
        public static void main(String[] args) {
     
     
     /*Gather user input*/
     
     
        System.out.print("Input the current cost of the item, just the number:");
        Scanner scan = new Scanner(System.in);
        int amount = scan.nextInt();
     
        System.out.print("Input the rate of inflation, as a percentage without the percent sign:");
     
        int inflat = scan.nextInt();
     
        System.out.print("Input the number of years, must be a whole number:");
     
        int numyear = scan.nextInt();
     
     
     /*Calculate the inflation according to the users input*/
     
    int counter = 0;
    double percent = (inflat/100);
    double amountedit = amount;
    	while(counter < numyear){
    		double finalamount = (amountedit + (amountedit * percent));
    		counter++;
     
     
     
     
     
     /*Print/results screen*/
     
    System.out.print("At ");
    System.out.print(inflat);
    System.out.print(" percent inflation per year, the cost in ");
    System.out.print(numyear);
    System.out.print(" year(s) will be $");
    System.out.print(finalamount);
    System.out.print(".");
     
    	}
        }
    }

  9. #8
    Junior Member
    Join Date
    Feb 2012
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: While loop messed up

    I realize moving it "}"was a dumb idea, that forces the print screen to happen again and again, dumb on my part

  10. #9
    Junior Member
    Join Date
    Feb 2012
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: While loop messed up

    i changed the formula and got it, thanks for helping

  11. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,652
    Thanks
    45
    Thanked 1,959 Times in 1,933 Posts

    Default Re: While loop messed up

    Having the correct formula can improve the program's output.

Similar Threads

  1. Converting a while loop to a for loop and a for loop to a while loop.
    By awesom in forum Loops & Control Statements
    Replies: 3
    Last Post: February 26th, 2012, 07:57 PM
  2. JOption to JFrame HELP...messed my program up
    By scottgilliland2003 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: December 20th, 2011, 07:18 PM
  3. For loop; Problems with my for loop
    By mingleth in forum Loops & Control Statements
    Replies: 5
    Last Post: November 16th, 2011, 06:24 PM
  4. [SOLVED] My while loop has run into an infinite loop...?
    By kari4848 in forum Loops & Control Statements
    Replies: 3
    Last Post: March 1st, 2011, 11:05 AM
  5. hi. i want to rewrite this do loop into a while loop.
    By etidd in forum Loops & Control Statements
    Replies: 3
    Last Post: January 26th, 2010, 04:27 PM