I definitely need assistance with homework assignment: I seem to have a lot of issues creating a program when I have to use JOptionPanes(s). After the instructions is my current code.

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.

public class Project2 {
  public static void main(String[] args) {
 
      Scanner input = new Scanner(System.in);
 
      for (int counter = 1; counter <= 10; counter++) {
      JOptionPane.showInputDialog(null, "Please enter ten numbers",
      "Question to User.",
      JOptionPane.QUESTION_MESSAGE);}
      
      int number = input.nextInt();{
      
      int array[] = new int [number];
      
      String output = " ";
      
      for (int number, numbers) {

      output += " " + number;

}
      
      JOptionPane.showMessageDialog(null, "Here are the numbers you selected." + output);
      
  }
  }
}

Recommended Answers

All 16 Replies

for (int number, numbers) {

this is not how you should make a for loop, your missing the increment and a proper condition
it should have a format like so,
for (initialization; termination/condition; increment)

using:

for (int number, numbers);

where would this be placed. Because I know that the counter might be wrong when it comes to asking the user to input a number ten times.

for (int number, numbers)

This is wrong syntactically..What are you trying to achieve here?


EDIT : All three posted at the same time :) :)

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.

Right now, I am just trying to get the user to input 10 numbers with a input dialog box, and then have the array print out.

I think you are trying to do something like this :
for(declaration : expression)
{
//Statements
}

Note the colon, Its not a comma

True, but with a colon it still does not read correctly.

for (int number, numbers)

Also , what is numbers here ..?? YOu need to use array

for (int number, numbers) {

Why the opening brace here. You don't need that

Well there are many mistakes out there, lets go one by one.

In your first for loop you are simply taking the values. You need to put them somewhere. Since you want to put them in an array, that code should come within the for loop.

int number = input.nextInt();{
int array[] = new int [number];

Declare number and array before the for loop. Inside the for loop you will only have

number = input.nextInt();

and

array[counter] = number; // here you are assigning number to element of array

And then finally after the for loop is complete you write another loop (with corrections mentioned above to traverse through that array and concatenate it to output string)

public class Project2 {

  public static void main(String[] args) {

 

      Scanner input = new Scanner(System.in);

 

      for (int counter = 1; counter <= 10; counter++) {

      JOptionPane.showInputDialog(null, "Please enter ten                      numbers",

      "Question to User.",

      JOptionPane.QUESTION_MESSAGE);}

      number = input.nextInt();
      array[counter] = number; 

      String output = " "; 
 
      JOptionPane.showMessageDialog(null, "Here are the numbers you selected." + output);


  }

  }

}

Is this better!!!??

Before you ask DaniWeb you should ask the Java compiler. Try compiling your code - it's a lot faster :)
When you compile you'll get a load of error messages. Read them very carefully because most will tell you exactly what's wrong with your code. If you still don't understand a message post the whole message here, along with the code it refers to, and someone will help.

public class Project2 {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

for (int counter = 1; counter <= 10; counter++) {

JOptionPane.showInputDialog(null, "Please enter ten numbers",

"Question to User.",

JOptionPane.QUESTION_MESSAGE);}

number = input.nextInt();
array[counter] = number;

String output = " ";

JOptionPane.showMessageDialog(null, "Here are the numbers you selected." + output);


}

}

}

How far did you get ? Are you getting any errors? if so post them.Or post what output you are getting now

Here is what I have since yesterday...

package project2;

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

public class Project2 {
  public static void main(String[] args) {
 
      Scanner input = new Scanner(System.in);
 
      int array[] = new int [10];
      for (int counter = 0; counter < 10; counter++){
      JOptionPane.showInputDialog(null, "Please enter ten numbers",
      "Question to User.",
      JOptionPane.QUESTION_MESSAGE);
      
      int number = input.nextInt();
      
      array[counter] = number;
      }
      
      JOptionPane.showMessageDialog(null, "Here are the numbers you selected.");
      printArray(array);
  }
}

I am receiving an error with printArray(array);
The error is that it cannot find the symbol.

After the close bracket, am I suppose to call for the array, so it can be displayed as an output. Furthermore, this is what I have for the other sections of this program but I do not know how to incorporate them. How can I take the code below and have it function within the code above.

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;
    }
}

printArray(array);

What is printArray function ?

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class NewUser
 */
@WebServlet("/NewUser")
public class NewUser extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public NewUser() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see Servlet#init(ServletConfig)
     */
    public void init(ServletConfig config) throws ServletException {
        // TODO Auto-generated method stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String s = null;// TODO Auto-generated method stub
        response.setContentType("text/html");
        PrintWriter p=response.getWriter();
        String str_username = request.getParameter("username");
        String str_password = request.getParameter("password");
        String str_confirmpassword = request.getParameter("confirmpassword");
        String str_male = request.getParameter("male");
        String str_female = request.getParameter("female");
        String str_email = request.getParameter("email");   
        String str_phoneno = request.getParameter("phoneno");
        if(s.equals("submit"))
        {
        try{
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            Connection con = DriverManager.getConnection("jdbc:odbc:real");
            Statement st = con.createStatement();
                        int i= st.executeUpdate("insert into register values('"+str_username+"','"+str_password+"','"+str_confirmpassword+"','"+ str_male+"','"+str_female+"','"+ str_female+"','"+ str_email+"',"+ str_phoneno+"");
            System.out.println(str_username +"is now registered.");
            System.out.println("<p align=\"center\"><a href=\"login.htm\">sign in</a></p>");
            }catch(Exception ee){System.out.println("2nd"+ee);}
            }



}}
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class NewUser
 */
@WebServlet("/NewUser")
public class NewUser extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public NewUser() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see Servlet#init(ServletConfig)
     */
    public void init(ServletConfig config) throws ServletException {
        // TODO Auto-generated method stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String s = null;// TODO Auto-generated method stub
        response.setContentType("text/html");
        PrintWriter p=response.getWriter();
        String str_username = request.getParameter("username");
        String str_password = request.getParameter("password");
        String str_confirmpassword = request.getParameter("confirmpassword");
        String str_male = request.getParameter("male");
        String str_female = request.getParameter("female");
        String str_email = request.getParameter("email"); 
        String str_phoneno = request.getParameter("phoneno");
        if(s.equals("submit"))
        {
        try{
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            Connection con = DriverManager.getConnection("jdbc:odbc:real");
            Statement st = con.createStatement();
                        int i= st.executeUpdate("insert into register values('"+str_username+"','"+str_password+"','"+str_confirmpassword+"','"+ str_male+"','"+str_female+"','"+ str_female+"','"+ str_email+"',"+ str_phoneno+"");
            System.out.println(str_username +"is now registered.");
            System.out.println("<p align=\"center\"><a href=\"login.htm\">sign in</a></p>");
            }catch(Exception ee){System.out.println("2nd"+ee);}
            }
            
    

}}

Start your own thread if you want help with that then add more details on what you need with

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.