Pages

Java receive user input and sort it in ascending order

Complete source code below is a simple java program that will receive user input and after that sort it in ascending order before display it's result.

*********************************************************************
COMPLETE SOURCE CODE FOR : AscendingString.java
*********************************************************************


import java.util.Scanner;

public class AscendingString
{
public static void main(String[]args)
{
Scanner s=new Scanner(System.in);
String[]a=new String[5];
for(int i=0;i<5;i++)
{
System.out.println(i+1+".Put your String");
String temp=s.nextLine();
a[i]=temp;
}

System.out.println("**********");
System.out.println("RESULT");
System.out.println("**********");

for(int i=0;i<5;i++)
{
for(int j=0;j<5;j++)
{
String temp=a[i];
String tempB=a[j];
if(temp.compareTo(tempB)<0)
{
a[i]=tempB;
a[j]=temp;
}
}
}

for(int i=0;i<5;i++)
{
System.out.println(a[i]);
}
}
}


*********************************************************************
JUST COMPILE AND EXECUTE IT
*********************************************************************
Related Posts Plugin for WordPress, Blogger...