Computer Science Canada

sortstring.java

Author:  alpesh [ Mon Jan 26, 2004 12:56 am ]
Post subject:  sortstring.java

/*

WAP To accept more than one string and arrange them in alphabetical order

--------------
public int compareTo(String anotherString)

Compares two strings lexicographically. The comparison is based on the Unicode value of each character in the strings.

Parameters:
anotherString - the String to be compared.
Returns:
the value 0 if the argument string is equal to this string; a value less than 0 if this string is lexicographically less than the string argument; and a value greater than 0 if this string is lexicographically greater than the string argument.


*/

import java.io.*;
public class sortstring
{
public static void main(String arg[])
{
int n;
String name[],nS;
n=0;
name = new String[20];
try
{
DataInputStream in;
in = new DataInputStream(System.in);
System.out.println("Enter no of Entry : ");
System.out.flush();
nS = in.readLine();
n = Integer.parseInt(nS);
System.out.println("Enter " + n + " entries ");
System.out.flush();
for(int i=0;i<n;i++)
{
System.out.flush();
name[i] = in.readLine();
}
for(int i=0;i<n-1;i++)
for(int j=i+1;j<n;j++)
{
String t;
if(name[i].compareTo(name[j])>0)
{
t = name[i];
name[i]=name[j];
name[j]=t;
}
}
}
catch(IOException e)
{
System.out.println("In out error");
System.exit(1);
}
System.out.println("Alphabatical order ");
for(int i=0;i<n;i++)
{
System.out.println(name[i]);
}
}
}

Author:  shorthair [ Thu Jan 29, 2004 9:36 am ]
Post subject: 

Great Acomplishment , well done , im quite interested in getting to know java , looks like you can harness alot of power with java


: