need help with getting average using java servlet

Thread Solved

Join Date: Dec 2007
Posts: 9
Reputation: jellyfish888 is an unknown quantity at this point 
Solved Threads: 0
jellyfish888 jellyfish888 is offline Offline
Newbie Poster

need help with getting average using java servlet

 
0
  #1
Dec 21st, 2007
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
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
  5. <title>Insert title here</title>
  6. </head>
  7. <body>
  8. <p>Average</p>
  9. <form action="AverageNumber" method=get>
  10. <p><input type=text name=number></p>
  11. <input type=submit name=submit value=Submit>
  12. </form>
  13. </body>
  14. </html>

This is my servlet:
  1.  
  2. import java.io.*;
  3. import java.util.Arrays;
  4.  
  5. import javax.servlet.*;
  6. import javax.servlet.http.*;
  7.  
  8.  
  9. public class AverageNumber extends HttpServlet
  10. {
  11. public void doGet(HttpServletRequest request,
  12. HttpServletResponse response)
  13. throws IOException, ServletException
  14. {
  15. int number = Integer.parseInt(request.getParameter("number"));
  16. response.setContentType("text/html");
  17. PrintWriter out = response.getWriter();
  18. out.println("<html>");
  19. out.println("<head>");
  20. out.println("<title>Sorting Servlet</title>");
  21. out.println("</head>");
  22. out.println("<body>");
  23. out.println(answer);
  24. out.println("</body>");
  25. out.println("</html>");
  26. }
  27.  
  28. public void doPost(HttpServletRequest request,
  29. HttpServletResponse response)
  30. throws IOException, ServletException
  31. {
  32. doGet(request, response);
  33. }
  34. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 793
Reputation: darkagn has a spectacular aura about darkagn has a spectacular aura about darkagn has a spectacular aura about 
Solved Threads: 109
darkagn's Avatar
darkagn darkagn is offline Offline
Master Poster

Re: need help with getting average using java servlet

 
0
  #2
Dec 21st, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 9
Reputation: jellyfish888 is an unknown quantity at this point 
Solved Threads: 0
jellyfish888 jellyfish888 is offline Offline
Newbie Poster

Re: need help with getting average using java servlet

 
0
  #3
Dec 22nd, 2007
I've edited my code...please help me correct the error...
  1. import java.io.*;
  2. import javax.servlet.*;
  3. import javax.servlet.http.*;
  4.  
  5.  
  6. public class AverageNumber extends HttpServlet
  7. {
  8. public void doGet(HttpServletRequest request,
  9. HttpServletResponse response)
  10. throws IOException, ServletException
  11. {
  12. int number = Integer.parseInt(request.getParameter("number"));
  13. response.setContentType("text/html");
  14. PrintWriter out = response.getWriter();
  15. out.println("<html>");
  16. out.println("<head>");
  17. out.println("<title>Sorting Servlet</title>");
  18. out.println("</head>");
  19. out.println("<body>");
  20. out.println(answer);
  21. out.println("</body>");
  22. out.println("</html>");
  23. }
  24.  
  25. public void doPost(HttpServletRequest request,
  26. HttpServletResponse response)
  27. throws IOException, ServletException
  28. {
  29. doGet(request, response);
  30. }
  31.  
  32. public int process(Integer number){
  33. int count=0;
  34. int sum=0;
  35.  
  36. StringTokenizer st=new StringTokenizer(number);
  37. while (st.hasMoreTokens()){
  38. System.out.println(st.nextToken());
  39. }
  40.  
  41. for (int=0; i<number.length; i++){
  42. count=count+1;
  43. sum=average+number;
  44. }
  45. int average=sum/count;
  46. System.out.println("Result: "+average);
  47. }
  48. }
  49. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 793
Reputation: darkagn has a spectacular aura about darkagn has a spectacular aura about darkagn has a spectacular aura about 
Solved Threads: 109
darkagn's Avatar
darkagn darkagn is offline Offline
Master Poster

Re: need help with getting average using java servlet

 
0
  #4
Dec 22nd, 2007
Instead of
  1. 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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 9
Reputation: jellyfish888 is an unknown quantity at this point 
Solved Threads: 0
jellyfish888 jellyfish888 is offline Offline
Newbie Poster

Re: need help with getting average using java servlet

 
0
  #5
Dec 22nd, 2007
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...
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 9
Reputation: jellyfish888 is an unknown quantity at this point 
Solved Threads: 0
jellyfish888 jellyfish888 is offline Offline
Newbie Poster

Re: need help with getting average using java servlet

 
0
  #6
Dec 22nd, 2007
I am having errors from:
  1. out.println(average); //it does not recognize average
  2. StringTokenizer st=new StringTokenizer(number); //it does not recognize StringTokenizer
  3. 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)
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 793
Reputation: darkagn has a spectacular aura about darkagn has a spectacular aura about darkagn has a spectacular aura about 
Solved Threads: 109
darkagn's Avatar
darkagn darkagn is offline Offline
Master Poster

Re: need help with getting average using java servlet

 
0
  #7
Dec 22nd, 2007
Originally Posted by jellyfish888 View Post
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.


Originally Posted by jellyfish888 View Post
I am having errors from:
  1. out.println(average); //it does not recognize average
  2. StringTokenizer st=new StringTokenizer(number); //it does not recognize StringTokenizer
  3. 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:
  1. 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.
Last edited by darkagn; Dec 22nd, 2007 at 11:06 pm. Reason: Only answered some of your question
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 793
Reputation: darkagn has a spectacular aura about darkagn has a spectacular aura about darkagn has a spectacular aura about 
Solved Threads: 109
darkagn's Avatar
darkagn darkagn is offline Offline
Master Poster

Re: need help with getting average using java servlet

 
0
  #8
Dec 23rd, 2007
Originally Posted by darkagn View Post
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.
Sorry, there is a spelling mistake in that URL. It should be http://java.sun.com/javase/6/docs/api - sorry about that!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC