Showing posts with label Advanced level Java Code asked in Google. Show all posts
Showing posts with label Advanced level Java Code asked in Google. Show all posts

Friday, June 13, 2014

How to check String array is sorted or not ?

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class SortString {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.println("Enter the inputs name and when you want to check then enter 'check' : ");
        List<String> input = new ArrayList<String>();
       
        int i =0, j=0;
        while(true){
            String name = in.next();
            if(name.equals("check")){
                break;
            }
            input.add(name);
        }
        for(; i<input.size()-1; i++){
            if(input.get(i).compareToIgnoreCase(input.get(i+1))>0){
                System.out.println("string arrary is not sorted in ascending order.");
                for(; j<input.size()-1; j++){
                    if(input.get(j).compareToIgnoreCase(input.get(j+1))<0){
                        System.out.println("string arrary is not sorted in descending order.");
                        break;
                    }
                }
                if(j==input.size()-1){
                    System.out.println("String array is sroted in descending order");
                }
                break;
            }
        }
        if(i==input.size()-1){
            System.out.println("String array is in ascending order");
        }
    }
}


Output
Enter the inputs name and when you want to check then enter 'check' :
abc
dfs
def
check
string arrary is not sorted in ascending order.
string arrary is not sorted in descending order.

OutputEnter the inputs name and when you want to check then enter 'check' :
abc
bbc
cbc
check
String array is in ascending order

OutputEnter the inputs name and when you want to check then enter 'check' :
Cbc
bBc
Abc
check
string arrary is not sorted in ascending order.
String array is sroted in descending order

Identify a number is well ordered or not ?

Note- A number is well ordered if all digits are in ascending order ex- 123456789.
Not well ordered number - ex- 2341 (because here 1 is coming after 4 so it is not well ordered.)


Code-

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

public class NumWellOrbered {
    public static void main(String[] args) {
        System.out.print("Enter a number: ");
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int r=0, s=n, j=0;
        List<Integer> num = new ArrayList<Integer>();
        while(s!=0){
            r = s%10;
            s = s/10;
            num.add(r);
        }
       
        for(j=(num.size()-1); j>0; j--){
            if(num.get(j)>num.get(j-1)){
                System.out.println("Number "+n+" is not well ordered");
                break;
            }
        }
        if(j==0){
            System.out.println("Number "+n+" is well ordered");
        }
    }
}


Output 1-
Enter a number: 5436
Number 5436 is not well ordered

Output 2-
Enter a number: 456789
Number 456789 is well ordered