Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java and JDK > JSP Basics
|
JSP Basics Beginning-level questions on JSP. More advanced coders should post to Pro JSP.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the JSP Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 14th, 2006, 06:56 PM
Registered User
 
Join Date: May 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default JSP: Count character in text file

Hi,

I am trying to count character in my text file by using the below JSP code. Even though i am free from error in the code, but the result is not what i want. I just get 1 and 0 after counting it. For example:

I have C D E G D F F F E A A in my text file.

I want to display:

A=2
C=1
D=2
E=2
F=3

And after i added some other characters into my text file the the value of the charater will increase following the whatever character that i add.

My code is below:

<HTML>
<HEAD>
<TITLE>The result.....</TITLE>
</HEAD>
<BODY>

<%@ page language = "java" import="java.io.*"%>

<% try

{

    int countA = 0, countB = 0, countC = 0, countD = 0, countE = 0, countF = 0;

    String strPath ="C:/Program Files/Apache Software Foundation/Tomcat 5.5/webapps/ROOT/15870416.txt";
    String line;

    BufferedReader objReader = new BufferedReader(new FileReader(strPath));
                     line = objReader.readLine ();

    for (int i=0; i<line.length(); i++)

    {

    if (line.charAt(i) == 'A') countA = countA + 1;
    if (line.charAt(i) == 'B') countB = countB + 1;
    if (line.charAt(i) == 'C') countC = countB + 1;
    if (line.charAt(i) == 'D') countD = countB + 1;
    if (line.charAt(i) == 'E') countE = countB + 1;
    if (line.charAt(i) == 'F') countF = countB + 1;

    }
    out.println(countA);
    out.println(countB);
    out.println(countC);
    out.println(countD);
    out.println(countE);
    out.println(countF);

} catch(IOException e){}

%>

</BODY>
</HTML>

Could you please advise??

Kimsan


 
Old June 7th, 2006, 09:55 AM
Authorized User
 
Join Date: Oct 2003
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to yescarthik
Default

Its just a simple logical error.. i think you must have copied and pasted countB+ . change it to countC, countD...etc in their appropriate places

 if (line.charAt(i) == 'A') countA = countA + 1;
    if (line.charAt(i) == 'B') countB = countB + 1;
    if (line.charAt(i) == 'C') countC = countB + 1;
    if (line.charAt(i) == 'D') countD = countB + 1;
    if (line.charAt(i) == 'E') countE = countB + 1;
    if (line.charAt(i) == 'F') countF = countB + 1;








Similar Threads
Thread Thread Starter Forum Replies Last Post
count occurrence of a character in a string surya Javascript How-To 6 May 7th, 2012 04:23 PM
How can i import a local text file into a jsp file ravibabu Java GUI 1 April 25th, 2007 04:05 AM
Count all occurences of a character in a string thomaz C# 2 February 9th, 2007 01:52 PM
Count character value using Stored Procedure itsurshailesh SQL Language 1 November 14th, 2006 01:31 AM
character count of ms-word in asp.net sh.rajkumar ASP.NET 1.0 and 1.1 Professional 0 May 19th, 2006 07:24 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.