0

I have an assignment, and I need to use a loop to allow a user to enter ten different numbers in a programme which then adds up the variables.

I have found various pieces of code and stitched them together to create this:


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

public class exercise6
{
   public static void main (String []args)
   {

     //Input

      String totalNum, num1, num2, num3, num4, num5, num6, num7, num8, num9, num10;

       Scanner in = new Scanner (System.in);
       System.out.println("Please enter ten numbers:");
       int[] inputs = new int[10];

       for (int i = 0; i < inputs.length; ++i)
         {
            inputs[i] = in.next();
         }

      //Process
      totalNum = num1 + num2 + num3 + num4 + num5 + num6 + num7 + num8 + num9 + num10;

      //Output
      JOptionPane.showMessageDialog(null, "Total = " + totalNum);


   }
}

It's not great, but it's the best I have so far. Please help?

  • Were do you use the varialbes num1 ... num10?Also why to use them when you have already created an array to store the inputs. – theVoid May 04 '16 at 10:34

5 Answers5

1

You don't need the variables num1 to num10. You can simply sum up in the loop itself. Like:

int sum = 0;
for (int i = 0; i < 10; i++) {
    sum += = in.next(); // sum = sum + in.next();
}

Furthermore you assigned your variables as Strings, but you need int. In your case it would print something like 1111111111, if the input would always be a 1.

Take a look here how you would handle Integers properly.

Community
  • 1
  • 1
Martin
  • 239
  • 1
  • 15
0

You can achieve that in two ways, either inside the loop itself just add the number or if you need to keep track of them for later just add them to the array.

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

public class exercise6
{
   public static void main (String []args)
   {

      String total;

       Scanner in = new Scanner (System.in);
       int numOfInputValues = 10;
       System.out.println("Please enter ten numbers:");
       int[] inputs = new int[numOfInputValues];

       for (int i = 0; i < numOfInputValues; ++i)
         {
            // Append to array only if you need to keep track of input
            inputs[i] = in.next();
            // Parses to integer
            total += in.nextInt();
         }

      //Output
      JOptionPane.showMessageDialog(null, "Total = " + totalNum);
   }
}
Oss
  • 4,232
  • 2
  • 20
  • 35
0

First of all, your class should be in CamelCase. First letter is always in capital letter.

Second, you don't need an array to save those numbers.

Third you should make a global variable that you can change with ease. That is a good practice.

And you should always close stream objects like Scanner, because they leak memory.

import java.util.Scanner;

public class Exercise6 {
public static void main(String[] args) {

    int numberQuantity = 10;

    int totalNum = 0;

    Scanner in = new Scanner(System.in);
    System.out.println("Please enter ten numbers:");

    for (int i = 0; i <= numberQuantity; i++) {

        totalNum += in.nextInt();

    }
    in.close();
    System.out.println(totalNum);

    }
}
hyrulelink16
  • 389
  • 1
  • 5
  • 17
0

So the simplest answer I found is:

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

public class exercise6
{
   public static void main (String []args)
   {

     //Input

      int totalNum, num1;
      totalNum = 0;

         for (int numbers = 1 /*declare*/; numbers <= 10/*initialise*/; numbers ++/*increment*/)
         { 
                 num1 = Integer.parseInt(JOptionPane.showInputDialog("Input any number:"));
                 totalNum = totalNum + num1;
         }


//Output
      JOptionPane.showMessageDialog(null, "Total = " + totalNum);
0

Try this way I only re-edit your code:

import javax.swing.*;

public class InputNums {

    public static void main(String[] args) {
        int total = 0;

        for (int i = 0, n = 0; i < 10;) {
            boolean flag = false;

            try {
                n = Integer.parseInt(JOptionPane
                        .showInputDialog("Input any number:"));
            } catch (NumberFormatException nfe) {
                flag = true;
            }
            if (flag) {
                flag = false;
                JOptionPane.showMessageDialog(null,
                        "Invalid no Entered\nEnter Again...");
                continue;
            }
            total += n;
            i++;
        }

        JOptionPane.showMessageDialog(null, "Total = " + total);
    }
}
dinesh saini
  • 431
  • 5
  • 8