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.


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

Results 1 to 2 of 2

Thread: Help with If Else Satement

  1. #1
    Junior Member
    Join Date
    Sep 2010
    Thanked 0 Times in 0 Posts

    Default Help with If Else Satement

    I'm working on a change in stock price code and all the outputs are right except Intel's Stock Price with change in GDP growth. I know that my GDP if else statement refers to the Fed Fund if else statement and I'm trying to figure out why it works for Jnj and not for Intel.


    Oh, and If you're going to try it, Fed fund = 50, GDP growth = 3.

    Here's the code:
    import java.text.NumberFormat;
    import javax.swing.JOptionPane;
    public class Stocks {
    	static double[ ] jnj = {60, 15, 10, 1};
        static double[ ] intel = {25, 40, 45, 2};
        static double nspone;// new stock price after change in federal fund rate
        static double nsptwo; // new stock price after both fed fund and GDP
        static double fedfunds;
        static double gdpgrowth;
        NumberFormat dollar = NumberFormat.getCurrencyInstance();
        public static double Fedfunds(double beta []){
            if(beta[3] == 2.0) {
           nspone = beta[0] - (beta[0]*((fedfunds*0.20)/100));
        } else {
           nspone = beta[0] - (beta[0]*((fedfunds*0.10)/100));
                return nspone;
        public static double GDPgrowth(double beta[]){
            if(beta[3] == 2.0) {
                   nsptwo = nspone + (nspone*((gdpgrowth*0.07)));
                } else {
                   nsptwo = nspone + (nspone*((gdpgrowth*0.03)));
    		return nsptwo;
        public static void main(String[] args) {
        	NumberFormat dollar = NumberFormat.getCurrencyInstance();
        	fedfunds = Double.parseDouble(JOptionPane.showInputDialog("Please Enter Federal Fund Rate"));
            gdpgrowth = Double.parseDouble(JOptionPane.showInputDialog("Please Enter GDP Growth Rate "));
            System.out.println("Intel's Original Stock Price " +dollar.format(intel[0]));
            System.out.println("JnJ's Original Stock Price " +dollar.format(jnj[0]));
            System.out.println("Intel's Stock Price with change in fed fund " +dollar.format(Fedfunds(intel)));
            System.out.println("Jnj's Stock Price with change in fed fund " +dollar.format(Fedfunds(jnj)));
            System.out.println("Intel's Stock Price with change in GDP growth " +dollar.format(GDPgrowth(intel)));
            System.out.println("Jnj's Stock Price with change in GDP growth " +dollar.format(GDPgrowth(jnj)));
    Last edited by helloworld922; September 22nd, 2010 at 05:23 PM.

  2. #2
    Join Date
    Jul 2010
    Washington, USA
    Thanked 43 Times in 39 Posts

    Default Re: Help with If Else Satement

    You choice of bad method and variable names makes your code VERY hard to follow. Next time please surround your code in [highlight=java][/highlight] tags. Are you getting an error when you try to compile your program, are you not recieving the desired output, What is the desired output? As you can see there is loads of data you left out that makes our job alot easier.

    TIP: If you have a variable named fedfunds, then don't create a method named Fedfunds. Instead make a method named convertToFedFunds or getFedFunds. (just examples. I'm not exactly sure what you're doing in the method)
    Last edited by Brt93yoda; September 22nd, 2010 at 03:18 PM.