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
*********************************************************************

Java recursive method

Sometime we need to solve a problem using the same way. For example you need to build a program that will print number from 1 until number that input by a user. First program will print number without using recursive technique and second program we will use recursive technique to print number from 1 until number that input by a user.You should see how a recursive method will do the same thing until problem solve.

FIRST PROGRAM :


import java.util.Scanner;

public class Test
{
public static void main(String[]args)
{
Scanner s=new Scanner(System.in);
System.out.println("Put a number");
int a=s.nextInt();

System.out.println("IT'S RESULT");
System.out.println("***************");

for(int i=1;i<=a;i++)
{
System.out.println(i);
}
}
}


SECOND PROGRAM :
print method in PrintNumber class is example of recursive method.


import java.util.Scanner;

public class Test
{
public static void main(String[]args)
{
Scanner s=new Scanner(System.in);
System.out.println("Put a number");
int myNumber=s.nextInt();
PrintNumber pn=new PrintNumber();
System.out.println("IT'S RESULT");
System.out.println("***************");
pn.print(myNumber);
}
}

class PrintNumber
{
public void print(int a)
{
if(a==1)
{
System.out.println(a);
}
else
{
int temp=a-1;
print(temp);
System.out.println(a);
}
}
}


For example if you put 2 into second program, when it enter print method in PrintNumber class, firstly program will check if the number is 1 or not. If not, the number will be minus with 1 before it send it's result to print method again. In this case it will send 1 to print method. So System.out.println(a) for a=2 will stop for a while until a=1. Now after it send 1 to print method, condition for a==1 will be true. 1 will be print, and System.out.println(a) for a=2 will continue again by print 2.

I hope it will help.
Related Posts Plugin for WordPress, Blogger...

I Also Can Create T-Shirt Logo For You