Saturday, April 24, 2010

Java receive user input and sort it in ascending order  

0 comments

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.


  1. import java.util.Scanner;
  2. public class AscendingString
  3. {
  4. public static void main(String[]args)
  5. {
  6. Scanner s=new Scanner(System.in);
  7. String[]a=new String[5];
  8. for(int i=0;i<5;i++)
  9. {
  10. System.out.println(i+1+".Put your String");
  11. String temp=s.nextLine();
  12. a[i]=temp;
  13. }
  14. System.out.println("**********");
  15. System.out.println("RESULT");
  16. System.out.println("**********");
  17. for(int i=0;i<5;i++)
  18. {
  19. for(int j=0;j<5;j++)
  20. {
  21. String temp=a[i];
  22. String tempB=a[j];
  23. if(temp.compareTo(tempB)<0)
  24. {
  25. a[i]=tempB;
  26. a[j]=temp;
  27. }
  28. }
  29. }
  30. for(int i=0;i<5;i++)
  31. {
  32. System.out.println(a[i]);
  33. }
  34. }
  35. }

What next?

You can also bookmark this post using your favorite bookmarking service:

Related Posts by Categories



Extension Factory Builder