0
package javaapplication5;
import javax.swing.JOptionPane;
public class JavaApplication5 {
public static void main(String[] args) {

   // String answer = JOptionPane.showInputDialog(null, "What is the S(n)?  If you do not need, input 0");
   // double x = Double.parseDouble(answer);
   // System.out.println("Your S(n) is :" + x);

    double x;
    String answer1 = JOptionPane.showInputDialog(null, "What is the first term of your Sigma expression or series?");
    double y = Double.parseDouble(answer1);
    System.out.println("Your a(1) is :" + y);
    String answer2 = JOptionPane.showInputDialog(null, "What is the last term of your Sigma expression or series?");
    double z = Double.parseDouble(answer2);
    System.out.println("Your a(n) is :" + z);
    String answer3 = JOptionPane.showInputDialog(null, "What is the total term of numbers in your sequence?  If you don't know this, put 0.");
    double n = Double.parseDouble(answer3);
    System.out.println("Your n is :" + n);
      if(n==0)
    {
    String answer4 = JOptionPane.showInputDialog(null, "What is the increment of your numbers?  How much is it being increased, divided or multiplied by?");
    double i = Double.parseDouble(answer4);
    System.out.println("Your i is: " + i); 
    double nn = Math.round(n*100.0)/100.0;

        nn = (((z - y)/i) + 1);

    System.out.println("Your n is :" + nn); 

      x = (((y+z)/2) * nn );

      System.out.println("The sum of your numbers is: " + x);
    }

        x = (((y+z)/2) * n );

    System.out.println("The sum of your numbers is: " + x);

}

}

Above is a program that is used for calculating the arithmetic series of a Sigma expression, if I'm saying it right. My question is that when the program prompts me for a variable, it will work for positive numbers, but not for negative. Do I have to label a type of variable? Must it be entered in a different manner? Any questions on the code I'll answer.

Trechubet
  • 15
  • 2
  • What is the exception you get while using -ve number ? – Amit Kumar Feb 04 '17 at 08:21
  • you can see [this](http://stackoverflow.com/questions/18799789/string-double-not-working-for-negative-numbers) for help. – jack jay Feb 04 '17 at 08:23
  • Try catching the exception while parsing, this will help in finding the source of error. – Amit Kumar Feb 04 '17 at 08:29
  • It's working now, I don't know what I did...maybe I was typing it wrong, I was renaming the program and then ran it again, and it works fine, I remember reading the error message about a floating-decimal, but I also wasn't clicking ok and was pressing enter, I believe the problem was I was pressing enter and the selector was on 'cancel', because when I wanted to cancel out of the program, I was returned with the same message. Human error, :( sorry for the time waste. – Trechubet Feb 04 '17 at 08:36
  • Exception in thread "main" java.lang.NullPointerException at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1838) at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110) at java.lang.Double.parseDouble(Double.java:538) at javaapplication5.JavaApplication5.main(JavaApplication5.java:12) – Trechubet Feb 04 '17 at 08:36

0 Answers0