-2

.jsp

 <% MainProgram.MainAverageProgram myProgram
             = new MainProgram.MainAverageProgram();  %>

 <% out.println("" + myProgram.getForm()); %> 
 // returns simple form with parameter user  

 <% myProgram.comAvgerage(request.getParameter("user"));%>

I wish to get the user field and convert it to an int. Then I want to use this int in a java class, do i convert it in the jsp file or in a java method ?

java method

public String comAvgerage( String number )
{    
    int user = Integer.parseInt(number);       
    return user;
}

i dont understand why this fails, it will always be null when the program runs right ?

Aaron
  • 75
  • 8

2 Answers2

0

How do you expect a String method to return int variable . should be identified in the compile time itself.

public int comAvgerage( String number )
{    
    int user = Integer.parseInt(number);       
    return user;
}

should work as expected , however i dont understand your question exactly . comment to my answer for further questions on this

NOTE:

request#getParameter is used to get the values from the request

Santhosh
  • 8,181
  • 4
  • 29
  • 56
  • request.getParameter("user"); gives me a String right ? so i should be able to pass this to a method that takes a string as a parameter ? – Aaron Oct 09 '14 at 10:20
  • But you cant return an integer variable from a java method which returns string – Santhosh Oct 09 '14 at 10:21
  • No if you make it as void , you cant return. if you wish not to return you can do it .. still i cant understand what you are trying to . can you please elaborate it clearly in your question – Santhosh Oct 09 '14 at 10:26
  • i simply want to get input from a form and use it as an int – Aaron Oct 09 '14 at 10:59
  • A nice tutorial on jsp , servlets here http://courses.coreservlets.com/Course-Materials/csajsp2.html ..read it through – Santhosh Oct 09 '14 at 11:03
0

A wrong use of request.getParameter method, since it is used to obtain parameters from a get request,here you can't get the user field value from your input form which is actually in a post request. try request.getAttribute("user")