Results 1 to 3 of 3

Thread: given this code

  1. #1
    Join Date
    Feb 2009
    Posts
    96

    given this code

    why doesnt the sum work?

    public static void main(String[] args){


    String Input;
    double[] numbers; // An array for storing the input values.
    int numCt; // The number of numbers saved in the array.
    double num; // One of the numbers input by the user.

    numbers = new double[4]; // Space for 5 doubles.
    numCt = 0; // No numbers have been saved yet.

    Input = JOptionPane.showInputDialog(null, "Please enter a price.");

    while (numCt < numbers.length) { // Get the numbers and put them in the array.
    Input = JOptionPane.showInputDialog(null, "Please enter a price.");
    num = Double.parseDouble(Input);
    if (num <= 0)
    break;
    numbers[numCt] = num;
    numCt++;
    }
    prices aPrice = new prices();
    double s;
    s = aPrice.getSum();

    //aveArray(numbers);
    //highPrices();
    JOptionPane.showMessageDialog(null,"The sum of the prices is " + s);
    // JOptionPane.showMessageDialog(null,"ddd" + nag);
    //}
    System.exit(0);
    }
    public double getSum()
    {
    double[] numbers = new double[4];
    double Sum = 0; // Start the total sum at 0.
    for (int i=0; i<numbers.length; i++) {
    Sum = Sum + numbers[i]; // Add the next element to the total

    }
    return Sum;
    }
    public void setSum(double numbers)
    {
    Sum = numbers;
    }

  2. #2
    Join Date
    Dec 2008
    Posts
    202

    Re: given this code

    The Above written code seems to perfect but have you properly Import the Global Methods in the header, because if you forget to import that into your program then still there are possibilities that the code might not run.

    such as:
    import javax.swing.*;
    import javax.swing.text.html.HTMLEditorKit;
    import java.awt.*;
    import java.io.*;

    What error this program gives when you Execute the program.

  3. #3
    Join Date
    Feb 2009
    Posts
    96

    Re: given this code

    import java.util.*;
    import javax.swing.JOptionPane;


    now the error is that the sum always comes out blank
    it says
    the sum is 0

Similar Threads

  1. Replies: 15
    Last Post: 02-05-2014, 03:00 PM
  2. startup error code:0x80090006 ,,unable to locate activation code
    By Mr.Walter in forum Windows XP Support
    Replies: 3
    Last Post: 04-01-2012, 06:08 PM
  3. Replies: 25
    Last Post: 28-12-2011, 07:58 PM
  4. Replies: 5
    Last Post: 08-07-2011, 07:53 PM
  5. Replies: 4
    Last Post: 05-10-2010, 07:08 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,575,312.17502 seconds with 16 queries