Results 1 to 3 of 3
  1. #1
    jellyfish888 is offline Member
    Join Date
    Dec 2007
    Posts
    7
    Rep Power
    0

    Default I need help with my java servlet homework(average)

    My teacher asked me to do a java servlet code that will get the average of x numbers...I am planning to make a code that will put a dilimeter(such as comma or space) to ask the user to input their x numbers...unfortunately I don't know how to make this and how I am going to compute for the average...please help me...for now I only manage to make the very basic of the script...please help me...

    My HTML code:
    Java Code:
    <!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 which I don't know how to continue:
    Java Code:
    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);
          }
    }

  2. #2
    jellyfish888 is offline Member
    Join Date
    Dec 2007
    Posts
    7
    Rep Power
    0

    Default edited java servlet

    I've done some changes in my code...This time I was able to somehow organize my thought to compute for average...I used stringtokenizer to split the numbers...unfortunately I am experiencing some errors...please help me...please check my code...

    Java Code:
    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);
    	}
    	}
    }

  3. #3
    james_kabi is offline Member
    Join Date
    Dec 2007
    Posts
    1
    Rep Power
    0

    Default I hope this could be helpful

    Hey buddy,

    I attached two zip files, one of them in AverageNumber.zip which is a web application (source included) in order to deploy it, first extract it and then copy the extracted folder to webapps directory of your tomcat,

    the second one is a netbeans project (AverageNumber_nbproject.zip), if you download the netbeans 6.0 IDE. you can extract the zip file and open it with netbeans.

    download the NetBeans 6.0 IDE from : Welcome to NetBeans
    it'll be solve most of your programming problems.

    I hope it would be helpful

    Regards,

    james
    Attached Files Attached Files

Similar Threads

  1. Calculate Average
    By sthack99 in forum New To Java
    Replies: 4
    Last Post: 06-13-2008, 11:09 AM
  2. Replies: 3
    Last Post: 12-26-2007, 06:42 AM
  3. Help with my java servlet homework
    By jellyfish888 in forum Java Servlet
    Replies: 2
    Last Post: 12-21-2007, 05:41 PM
  4. working out wrong average
    By gemtez in forum New To Java
    Replies: 4
    Last Post: 12-12-2007, 08:35 AM
  5. get the average and maximum score
    By Eric in forum Advanced Java
    Replies: 2
    Last Post: 07-01-2007, 04:15 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •