in my program a simple shopping application for my lab exercise, i just calculated the price of items inside a for loop but when i try to print it outside it is not getting printed...pls give me some suggestion.

for (int i = 1; i < 7; i++) {
                String selection = request.getParameter("a" + i);

                if (selection.equals("l")) {

                    price = Integer.parseInt(request.getParameter("b" + i));
                    total = total + price;

                    out.println("<h3>You have purchased the item:<br>Price is:</h3>" + price);
                }
            }
            out.println("THE TOTAL IS"+total);
            out.println("</body>");
            out.println("</html>");

Recommended Answers

All 9 Replies

Just the value of "total" is not getting output, or is that entire line not getting output. I.E. do you see nothing or do you see "THE TOTAL IS" and nothing more.

Edit: And did you look at the web containers logfiles to make sure no errors are being thrown that have not been propogated back to the browser.

The entire line from "THE TOTAL IS" is not getting printed.

Then that means your jsp is getting "short-circuited" inside (or before, as I do not know if those price lines are being returned, either) the for loop.

As I said, check the web containers logfiles for any error that did not get propogated back to the browser. And the code beofre the for loop itself would also offer valuable insight.

ya i have checked it and it shows:
Error: uncaught exception: Error opening input stream (invalid filename?)

The price value is displaying correctly during each iteration , but the total value is not getting displayed and have attached the image below

and have you checked where that exception is thrown?

no i didnt...instead i just cut these three line and i pasted out from the try catch block..The total value is getting printed correctly....

out.println("THE TOTAL IS"+total);
out.println("</body>");
out.println("</html>");

But ther is another problem here whenever i skip a check box (example if i select the first and the last checkbox) the for loop is printing only the first check box value and the loop is exiting..it is not going to the next iteration....can u give some suggestion..

....
do you know why they do print outside, and not inside the try catch block?
as for your other question, concerning the checkboxes: I don't see any reference to checkboxes in the code snippet you've posted, so I have no idea what it might be, I don't even know which loop you are talking about.

ya..I just read that due to scope problem there might be problem with the "total"also that eventhough there is an exception the browser is not showing it..better i just pulled that variable and kept it outside the try block...and it worked...but not fully.
This is my html file i am showing the menu using this and collecting the price data from the text box.

menu.html

<html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title></title>
    </head>
    <body background='img.jpeg'>

        <center><h3>AVAILABLE CD AND DVD   </h3></center>
            <form action="fin" method="post" align="center">
                <table border="1" align="center">
                <tr><td></td><td>Item</td><td>Price</td>
                <tr><td><input type="checkbox" name="a1" value="l"></td><td>Prince of Persia</td><td><input type="text" name="b1" value="450"/></td></tr>
                <tr><td><input type="checkbox" name="a2" value="l"></td><td>Assasins Creed</td><td><input type="text" name="b2" value="333"/></td></tr>
                <tr><td><input type="checkbox" name="a3" value="l"></td><td>Call of Duty</td><td><input type="text" name="b3" value="245"/></td></tr>
                <tr><td><input type="checkbox" name="a4" value="l"></td><td>Beowolf</td><td><input type="text" name="b4" value="200"/></td></tr>
                <tr><td><input type="checkbox" name="a5" value="l"></td><td>Resident Evil</td><td><input type="text" name="b5" value="320"/></td></tr>
                <tr><td><input type="checkbox" name="a6" value="l"></td><td>Total Overdose</td><td><input type="text" name="b6" value="424"/></td></tr>
                <tr><td><input type="submit" value="Purchase"/></td></tr></table>
            </form>
    </body>
    </html>

Using the fin.java i am retrieving the text box value from html and verifying whether the check box are checked are not when i check the list continuously i can get the total amount correctly...but when i skip just one check box it is not printing the total value correctly....it stays with the first check box price itself.....

fin.java

public class fin extends HttpServlet {
int price, total = 0;
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet fin</title>");
out.println("</head>");
out.println("<body background='img.jpeg'><center>");
try {
String items[] = new String[7];
items[1] = "Prince of Persia CD";
items[2] = "Assasins Creed CD";
items[3] = "Call of Duty";
items[4] = "Bewolf CD";
items[5] = "Resident Evil";
items[6] = "Total Overdose";
for (int i = 1; i < 7; i++) {
String selection = request.getParameter("a" + i);
if (selection.trim().equals("l")) {
price = Integer.parseInt(request.getParameter("b" + i));
total = total + price;

out.println("<h3>You have purchased the item:"+items[i]+"<br>Price is:</h3>" + price);
}
}
} catch (Exception e) {
}
out.println("<br><h3>THE TOTAL IS</h3><br>"+total);
out.println("<hr color='black'width='100'>Thankyou for purchasing</center></body>");
out.println("</html>");
out.close();
}
}

scope? it had nothing to do with the scope, all with the exception.
as I interprete your posts well, you don't know the basics of Java well enough to start working with jsp and servlets.

don't try to jump to higher chapters in the book while ignoring those before that you don't understand.

just to explain why they weren't printed:

try{
  line of code 1
  line of code 2
  line of code 3 -> this line throws an exception
  line of code 4
}
catch(Exception e){}
  line of code 5

in the above example, lines 1, 2 and 5 will be executed. 3 will be interrupted (by the exception) and 4 will not be executed, since you'll be re-directed straight to the catch block.

first learn the basics, then go for web applications.

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.