I have a homework to get the average of x number...I was thinking that in order to get this I should just ask the user to input all the numbers that that want to get and using comma as separator...unfortunately, I don't know how to do this...below I have my HTML and my servlet which I don't know how to continue...please help...

This is my HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<p>Average</p>
<form action="AverageNumber" method=get>
<p><input type=text name=number></p>
<input type=submit name=submit value=Submit>
</form>
</body>
</html>

This is my servlet:

import java.io.*;
import java.util.Arrays;

import javax.servlet.*;
import javax.servlet.http.*;


public class AverageNumber extends HttpServlet
{
    public void doGet(HttpServletRequest request,
         HttpServletResponse response)
            throws IOException, ServletException
    {
      int number = Integer.parseInt(request.getParameter("number"));
      response.setContentType("text/html");
      PrintWriter out = response.getWriter();
      out.println("<html>");
      out.println("<head>");
      out.println("<title>Sorting Servlet</title>");
      out.println("</head>");
      out.println("<body>");
      out.println(answer);
      out.println("</body>");
      out.println("</html>");
      }

      public void doPost(HttpServletRequest request,
      HttpServletResponse response)
      throws IOException, ServletException
      {
      doGet(request, response);
      }
}

Recommended Answers

All 7 Replies

When the user inputs the numbers, you will have a String. Take a look at the API for the StringTokenizer class - I think you will see that this will be a useful class to separate the numbers (called tokens) by the commas (separators).

Hope this has given a hint in the right direction,
darkagn

I've edited my code...please help me correct the error...

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;


public class AverageNumber extends HttpServlet
{
    public void doGet(HttpServletRequest request,
         HttpServletResponse response)
            throws IOException, ServletException
      {
      int number = Integer.parseInt(request.getParameter("number"));
      response.setContentType("text/html");
      PrintWriter out = response.getWriter();
      out.println("<html>");
      out.println("<head>");
      out.println("<title>Sorting Servlet</title>");
      out.println("</head>");
      out.println("<body>");
      out.println(answer);
      out.println("</body>");
      out.println("</html>");
      }

      public void doPost(HttpServletRequest request,
      HttpServletResponse response)
      throws IOException, ServletException
      {
      doGet(request, response);
      }

      public int process(Integer number){
      int count=0;
      int sum=0;

      StringTokenizer st=new StringTokenizer(number);
      while (st.hasMoreTokens()){
      System.out.println(st.nextToken());
      }

	for (int=0; i<number.length; i++){
	count=count+1;
	sum=average+number;	
	}
	int average=sum/count;
	System.out.println("Result: "+average);
	}
	}
}

Instead of

System.out.println(st.nextToken());

you should assign the nextToken to a String then get the intValue from it using the Integer.parseInt method. Then you will be able to sum your numbers.
To get the average you will need to cast either the sum or the number to a double or float because the result of int / int is always an int. This will not always be accurate.

I don't understand your comment...can you please just show it to me cause I am new in java so I am still unfamiliar of the things that you are saying...

I am having errors from:

out.println(average);  //it does not recognize average 
  StringTokenizer st=new StringTokenizer(number);  //it does not recognize StringTokenizer
  for (int i=0; i<number.length; i++){   //it does not recognize number.length

This is the error that shows...
root cause

java.lang.Error: Unresolved compilation problem:
average cannot be resolved

AverageNumber.doGet(AverageNumber.java:20)
javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)

I don't understand your comment...can you please just show it to me cause I am new in java so I am still unfamiliar of the things that you are saying...

Take a look at the Java API in the Integer class, especially the parseInt and intValue methods. The Java API is located at http://java.sum.com/javase/6/docs/api - you should probably bookmark this page if you want to learn Java.

I am having errors from:

out.println(average);  //it does not recognize average 
  StringTokenizer st=new StringTokenizer(number);  //it does not recognize StringTokenizer
  for (int i=0; i<number.length; i++){   //it does not recognize number.length

This is the error that shows...
root cause

java.lang.Error: Unresolved compilation problem:
average cannot be resolved

AverageNumber.doGet(AverageNumber.java:20)
javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)

average has not been initialised before this line, so it doesn't make sense to print it out. The StringTokenizer error is caused because you need to import it by adding the following code at the very begining of your code:

import java.util.StringTokenizer;

number.length doesn't make sense because number is initialised as an int. I'm not sure what you are trying to do here, but either number needs to be an int array or the for statement should be < number as opposed to number.length.

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.