Search This Blog

Monday 11 August 2014

02. Corrected Program to Sort List of Names and Search for a Key by Ignoring Case.

Filename: Sort.html
//Html program to read search key
<HTML>
    <HEAD>
        <TITLE>Sorting and Searching</TITLE>
    </HEAD>

    <BODY>
        <H1>Program to sort list of names and search for key</H1>
        <FORM ACTION="Sort.jsp" METHOD="POST">
              Please enter your the search key:
            <BR>
           
              <input NAME="text1" type="text">
            <BR>
            <INPUT TYPE="SUBMIT"   VALUE="Submit">
        </FORM>
    </BODY>
<HTML>


Filename: Sort.jsp
<html>
<body>
<%@ page import="java.io.*,java.util.*" %>
<%
    String str[]={"Pineapple","Orange","Grapes","Banana","Apple"};
    String  temp;
    int flag=0,j;
    out.println("<H2>The sorted list of names:</H2>");
    for(j=0; j<str.length;j++)
    {
         for (int i=j+1 ; i<str.length; i++)
        {
             if(str[i].compareToIgnoreCase(str[j])<0)
            {
                temp= str[j];
                str[j]= str[i];
                str[i]=temp;
            }
        }
        out.print(str[j]+"<br>");
    }
 %>
 <br>
 <br>
 <%

    String key=request.getParameter("text1");
    for(j=0; j<str.length;j++)
    {
         if(str[j].compareTo(key)==0)
        {
              flag=1;
              break;
        }
    }

    if(flag==1)
        out.println("<h3>Search Key Found at location "+ (j+1) +"<h3>");
    else
        out.println("<h3>Search Key Not Found!</h3>");


%>
<body>
</html>

No comments :

Post a Comment

FREE Hit Counters