This week's book giveaway is in the Agile forum.
We're giving away four copies of Head First Agile and have Andrew Stellman & Jennifer Greene on-line!
See this thread for details.
Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Predicting future values using Scanner and String  RSS feed

 
Megan Long
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is what my code looks like:


When I ran it it gave back errors saying something is wrong with my "scan.nextInt();" and my "marginOfSafety"

And this is an example of what it's supposed to look like:

Company name: Apple
Stock price: 107.73
Future value: 597.20
Required return on investment in percent: 15
Margin of Safety Discount in percent: 25
/* The above values were entered at run time, the bottom are computed */

Stock Apple at Today's Price 107.73
with Future Value 597.2 and Required ROI of 15.0%
At a 25.0% discount the sticker price is 147.6187064959783
and the price you should pay is 110.71402987198373


And I just don't know how to fix those errors mentioned above so any help is appreciated. Thank you

Oh and it might help knowing the instructions:

Investment analysts will often project the future price of a company’s stock by considering the rate at which that
company has increased the value of its assets over the past 5-to-10 years. Here are some examples of estimates that
we’ve seen recently for two companies that you might have heard of:
Company Current Price (September 3, 2016) Estimated Future Value (2026)
Apple                       107.73                                     597.20
Hanesbrands             26.40                                      110.99
Please note that, even though these are genuine estimates, we are NOT explaining the assumptions involved in the
estimation method. These are for illustration only, not for investment decision-making.
 INPUT 1: The price of the stock today is price.
 INPUT 2: Let’s call the Estimated Future Value of a company the fv.
Once you have an estimated future value of a company, you can decide how much you should be willing to pay for
it today. This involves two additional inputs:
 INPUT 3: The minimum percentage amount per year that you are willing to earn on your investment. (For
example, you can earn 3.1% per year if you buy a bond issued by Barclays Bank that matures in August,
2021). Many investors, like Warren Buffett, look for a 15% return per year. Let’s call this variable
annualROI (short for Annual Return on Investment).
Given the annualROI and the fv, your program can calculate a stickerPrice for the stock. This is
the price you would pay today in order to achieve exactly an annualROI (such as 15%) return on
investment if you sell the stock for fv in 5 years. Allow the user to enter annualROI as a percentage,
such as 15 for 15%.
 INPUT 4: The percentage discount you require off the sticker price before you’re willing to buy the stock.
This is often called the Margin of Safety discount. Investors want the discount in case the stock doesn’t
actually reach the estimated future value price. For example, it is common to require a 25% discount. Let’s
call this variable MarginOfSafety and interpret the entered number as a percentage.
To calculate the 10-years-in-the-future value of a stock:
stickerPrice = ____fv________
                   (1 + annualROI)10
The power of 10 reflects the number of years.
To determine the price you should be willing to pay today:
buyPrice = ((100-marginOfSafety)/100) * stickerPrice

Write a program (name it Sticker) that:
1. Asks the user what company she is considering. Use scanner to accept the value and store it in a string.
2. Ask the user for the four inputs described above. Use scanner to accept the values and store them in
appropriate variables.
3. Perform the calculations described above then then display the inputs, the sticker price and the buy price.
Label your outputs so we can understand what the numbers mean.
 
Norm Radder
Ranch Foreman
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
errors saying something is wrong with my "scan.nextInt();" and my "marginOfSafety"

Please copy the full text of the error message and paste it here. It has important info about the error.
 
Junilu Lacar
Sheriff
Posts: 11172
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't see how that code compiles. You declare your Scanner as keyboard and yet you use a non-existent scan in the rest of the code.
 
Junilu Lacar
Sheriff
Posts: 11172
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, the way you've written it, your code will always display "Stock Apple at Today's price ..." regardless of the company name you enter.
 
Knute Snortum
Sheriff
Posts: 4091
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:I don't see how that code compiles. You declare your Scanner as keyboard and yet you use a non-existent scan in the rest of the code.

It doesn't compile.  @Megan Long: please post an SSCCE, or Short, Self-Contained, Compilable Example.
 
Megan Long
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm Radder wrote:
errors saying something is wrong with my "scan.nextInt();" and my "marginOfSafety"

Please copy the full text of the error message and paste it here. It has important info about the error.


Sticker.java:24: error: cannot find symbol
        marginofSafety = scan.nextInt();
        ^
  symbol:   variable marginofSafety
  location: class Sticker
Sticker.java:29: error: cannot find symbol
                buyPrice = ((100 - marginOfSafety)/100) * stickerPrice;
                                   ^
  symbol:   variable marginOfSafety
  location: class Sticker
Sticker.java:37: error: cannot find symbol
                System.out.println("Margin of SafetyDiscount in percent: " + marginOfSafety + "%");
                                                                             ^
  symbol:   variable marginOfSafety
  location: class Sticker
Sticker.java:40: error: cannot find symbol
                System.out.println("At a " + marginOfSafety + "% discount the sticker price is " + stickerPrice);
                                             ^
  symbol:   variable marginOfSafety
  location: class Sticker
4 errors
 
Junilu Lacar
Sheriff
Posts: 11172
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java is case sensitive. You declared marginofsafety but you use marginofSafety, with a capital S, which makes it a different name altogether from what you declared.

Looks like you also have marginOfSafety, with a capital O and S, which is again a different name from both marginofsafety and marginofSafety.  You need to use the name exactly as you declared it.
 
Junilu Lacar
Sheriff
Posts: 11172
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BTW, standard Java naming convention recommends you use marginOfSafety, a form called "camelCase" - change your code so you use this style consistently for variable, method, and method parameter names.
 
Liutauras Vilda
Marshal
Posts: 4670
320
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Megan,

1. fv - what this variable represents? Future value? Why you don't name it futureValue? The variable name you gave it, is non-informative. Each name you give to the variables - they should tell you exactly and right away when you look at them, what they represent.

2. Your code indentation is inconsistent. Things start go wrong from line 10 till 19 including. After line 1 (right before 2nd) supposed to be an empty line, otherwise it adds an extra bit towards chaotic code.

3. Line 6. After the Scanner, right before opening parenthese there shouldn't be space character as it is a constructor invocation. Space character supposed to be added after if, else if, for and while loops.

4. After the first and second prompt texts you don't leave space character, so the user's input wouldn't go right after the 'question mark', while after the 3rd, 4th, 5th you leave space characters. Style mistakes, inconsistency.

5. Personally myself I wouldn't declare so many variables on the same line. Referring to line 8.

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!