Sum Of Numbers

Posted on 2009-07-01
Last Modified: 2013-12-14
Im trying to write a program that asks the user for a positive nonzero integer value. The program should use a loop to get the sum of all the integers from 1 up to the number entered. For example, if the user entered a 50, the loop will find the sum of 1,2,3,4,....50.

i think the code i posted may be wrong but ive spent hours trying to figure it out and my teacher isnt helpful at all. :(

i really would appreciate it if a full solution code is posted. This will help me study for my other programs im having trouble with.
public class SumOfNumbers
        public static void main( String [] args)
            double nonZero;
            double numbers;
            double totalNumbers;
            String input;
            for( int count = 1; count <= nonZero; count++ )
                input = JOptionPane.showInputDialog(null, "Please enter a nonzero positive integer.");
              nonZero = Double.parseDouble(input);
            JOptionPane.showMessageDialog(null, "The sum of the integers is: " + nonZero);

Open in new window

Question by:AgentC4
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Accepted Solution

Opalelement earned 500 total points
ID: 24760313
Not sure if this will work since I don't have a java environment to test in ATM, but it has comments so you can see what it is supposed to do.
public class SumOfNumbers
        public static void main( String [] args)
            double number; // The entered number after it is parsed
            double totalNumber = 0; // Total after adding
            String input; // Input from the input dialog
            input = JOptionPane.showInputDialog(null, "Please enter a nonzero positive integer."); // Ask for the number
            number = Double.parseDouble(input); // change it to a number
            for( int count = 1; count <= number; count++ )
              totalNumber += count; // Loop through and keep adding the numbers
            JOptionPane.showMessageDialog(null, "The sum of the integers is: " + totalNumber); // Show the final

Open in new window


Author Closing Comment

ID: 31599057
You're a life saver, thank you so much yes this works perfectly. ill definitely study it and work more and do examples.

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Custom Java Application connects to database: Which one of the 20? 4 42
Java: anonymous class 4 39
printf performancy 11 86
Html split(text) 2 32
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question