If I close off the for loop it works but I need to get the output to show as a dialog box as well.

I need the output to show inside a dialog box:

Output should include count of numbers entered
the sum of the numbers
the average of the numbers
and the number closest to the average

This is what I have so far:

package project.pkg2a;

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


        
public class Project2a {
    public static void main(String[] args) {
        
        
        Scanner input = new Scanner(System.in);
        double array[] = new double[10];
      
      //Have user input 10 numbers 
        for (int counter = 0; counter < 10; counter++){
         String userInput =      
               JOptionPane.showInputDialog(null, "Please, enter a new number",
               "Question to User.",
               JOptionPane.QUESTION_MESSAGE);
      
        for(int count = 0; count< array.length; count++ ){	
        array[count] = input.nextDouble();
        
        
        
        String output = ("Here are the numbers you selected: " + userInput);
        
        JOptionPane.showMessageDialog(null, output);
    }
}
}
}

This is an outline of how it should look:

import javax.swing.JOptionPane;


public class Project2 {
    public static void main(String[] args) {
       //Method to get user data

    //code to display results

        String message = display(numbersArray, count);
        JOptionPane.showMessageDialog(null, message);
    }
    //Method to find the sum
    public static int find_sum(int[] value){
       
       
        return (total);
    }
    //method to find the average
    public static double find_avg(int[] values, double count){
       
        return (average);
    }
    //method to find the closest to the average
    public static double find_closest(int[] val, double average){
       
        return (closest);
    }
    //Method to build the Display string and return the string
    public static String display(int[] numbersArray, double count){
        //Note that I do not display the results here, just build the string and return it.
        return results;
    }
}

Recommended Answers

All 22 Replies

Where is the JOptionPane you are having problems with? I don't see where you have one that tries to show the numbers you are talking about.

This JOptionPane only ask the user one time, not ten times.

//Have user input 10 numbers
for (int counter = 0; counter < 10; counter++){
String userInput =
JOptionPane.showInputDialog(null, "Please, enter a new number",
"Question to User.",
JOptionPane.QUESTION_MESSAGE);

The loop looks like it will go for 10 times.
Why do you think it only goes once?

When I execute the code I get this message once:
Please, enter a new number
and this one 10 times:
Here are the numbers you selected: ...

Because I have tested the loop

Okay, I am sorry. That loop does work, but how to a get the numbers the user has entered to calculate the sum and average?

When I execute the code I get this message once:
Please, enter a new number
and this one 10 times:
Here are the numbers you selected: ...

Please make a list of the steps the program should do in the order that it should do them.
When I execute the code, I get the results as I said above.

how to a get the numbers the user has entered to calculate the sum and average?

Read them into variables so you can do arithmetic on them.
If you read them as Strings, you can use the Double class's parseDouble method to convert them to double.

//Method to get user data
           Scanner input = new Scanner(System.in);
           double array[] = new double[10];
                        
	   for (int counter = 1; counter <= 10; counter++) {
           String userInput =
           JOptionPane.showInputDialog(null, "Please enter ten numbers");}
      
        for(int count = 0; count< array.length; count++ ){	
        array[count] = input.nextDouble();
        }

        JOptionPane.shwoMessageDialog(null, + userInput);
        
        
    }
}

Now for + userInput

is displaying an error message (cannot find symbol)

Below is a list/outline of what I need to get accomplished, I am having a hard time filling in the sections.

//Method to get user data

    //code to display results

        String message = display(numbersArray, count);
        JOptionPane.showMessageDialog(null, message);
    }
    //Method to find the sum
    public static int find_sum(int[] value){
       
       
        return (total);
    }
    //method to find the average
    public static double find_avg(int[] values, double count){
       
        return (average);
    }
    //method to find the closest to the average
    public static double find_closest(int[] val, double average){
       
        return (closest);
    }
    //Method to build the Display string and return the string
    public static String display(int[] numbersArray, double count){
        //Note that I do not display the results here, just build the string and return it.
        return results;
    }
}

an error message (cannot find symbol)

Please post the full text of the error message.
What symbol on what line?

Line 14 (+ userInput)

//Method to get user data
           Scanner input = new Scanner(System.in);
           double array[] = new double[10];
                        
	   for (int counter = 1; counter <= 10; counter++) {
           String userInput =
           JOptionPane.showInputDialog(null, "Please enter ten numbers");}
      
        for(int count = 0; count< array.length; count++ ){	
        array[count] = input.nextDouble();
        }

        JOptionPane.shwoMessageDialog(null,"Here are the numbers you have selected: ", 
                "\n" + userInput);
        
        
    }
}

line 15 because I moved it down one line.

Please post the full text of the error message.

Please post the full text of the error message.

Cannot find symbol
Symbol: variable userInput
location: class testrun.testrun

Java is case sensitive. Check your spelling.

Now I am not getting any errors, but I cannot get the array to display as an output still. If you know how, please modify code so I can see.

//Method to get user data
      String userInput = null;
      Scanner input = new Scanner(System.in);
 
      for (int counter = 1; counter <= 10; counter++) {
      userInput =
      JOptionPane.showInputDialog(null, "Please, enter a new number",
      "Question to User.",
      JOptionPane.QUESTION_MESSAGE);}
      
      int number = input.nextInt();{     
      int array[] = new int [number];
     
      for (int i = 0; i <= array.length; i++) {
      array[i] = input.nextInt();}     
          
}
      String output = ("These are the numbers you have entered.:" + userInput);
      
      JOptionPane.showMessageDialog(null, output);  
    }
}

get the array to display as an output

Please explain how you want to display the contents of the array.
The Arrays class's toString() method will format the contents of an array for printing.

Please explain how you want to display the contents of the array.
The Arrays class's toString() method will format the contents of an array for printing.

I want the array to print out the numbers that the user inputs into the dialog box.

After that I want the program to calculate the sum, the average, and the number that is closest to the average. Then output the sum, the average, and the number that is closest to the average.

Here are the requirements:

1. Design and implement a Java program that will gather integer numbers and performs some computations on the data entered. The program should use separate methods for inputting the data, calculating the sum, calculating the average, calculating the number closest to the average, and displaying the results. A sentinel value should be used to indicate the user has completed entering their numbers. The output should display a message that includes the count of the numbers entered, the sum of the numbers, the average of the numbers, and the number closest to the average.

2. Additional requirements include:

a. Use JOptionPane.showInputDialog() methods for your user to input their data

b. Use JOptionPane.showMessageDialog() methods to display your messages.

I want the array to print out the numbers that the user inputs into the dialog box.

Have you tried the Arrays toString method?

Have you tried the Arrays toString method?

How do I accomplish this? Because I am not familiar with this.

Can you please demonstrate this in the code I have and then I should be able to take over from there.

Do a Search on this forum for Arrays.toString for many coding samples.

Do a Search on this forum for Arrays.toString for many coding samples.

That did not help.

If you cannot help, then do not bother replying. Because I am extremely new at java.

That did not help.

Please explain what you did and what the problem was. If you don't post the error messages and/or show us what you are doing, its very hard to understand what the problem is.
To use the Arrays toString method to format an array:
Arrays.toString(<THE ARRAY NAME HERE>)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.