Sunday, August 25, 2013

array

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


public class ArraysExample {
       public static void main(String []args)
       {
              int[] nums = {17, 90, 21, 78, 12, 36, 93, 23, 43, 120, 125,64};
              System.out.println("Before Sorting ");
              System.out.println(Arrays.toString(nums));
              Arrays.sort(nums);
              System.out.println("After Sorting ");
              System.out.println(Arrays.toString(nums));
              System.out.print("Enter a value to perform the binary search in the array : ");
              Scanner in = new Scanner(System.in);
              try
              {
                     int position = Arrays.binarySearch(nums, in.nextInt());
                     if(position >= 0)
                     {
                     System.out.println("The entered value found at the position " +  position);
                     }
                     else
                     {
                           System.out.println("The entered value doesn't exists in the array");
                     }
              }
              catch (InputMismatchException e) {
                     System.out.println("Entered value is not a number");
              }
              List<String> list = Arrays.asList("C", "C++", "Java", ".Net", "PHP", "JavaScript", "HTML");
              for(String language : list)
              {
                     System.out.println(language);
              }
              System.out.println(Arrays.toString(Arrays.copyOf(nums, 30)));
       }
}


scanner with hashmap

import java.util.*;
import java.util.Map.Entry;

public class ExampleWithScannerAndHashMap {
                public static void main(String[] args) {
                                int count = 0;// stores the number of names
                                // Scanner the reads the user input from standard input stream
                                Scanner in = new Scanner(System.in);
                                // Request user input till getting a valid number
                                System.out.println("Enter the Number of Names U wants to display :");
                                while (true) {
                                                try {
                                                                // Read the user input and convert into integer
                                                                count = in.nextInt();
                                                                // If it is a valid number break the loop
                                                                break;
                                                } catch (InputMismatchException e) {
                                                                // If it is not a valid number show the error msg
                                                                System.out.println("Please enter a valid number : ");
                                                }
                                }
                                in.nextLine(); // reads the enter char from buffer
                                // Hashmap to store the names
                                Map<String, String> hashMap = new HashMap<String, String>();
                                for (int i = 1; i <= count; i++) {
                                                System.out.println("Enter Name " + i + " : ");
                                                // Add the names to hashmap with different keys
                                                hashMap.put("name" + i, in.nextLine());
                                }
                                // Print the names in the hashmap
                                printValues(hashMap);
                                System.out.println("Enter a key to delete : ");
                                String key = in.nextLine();
                                if (hashMap.containsKey(key)) {
                                                hashMap.remove(key);
                                                System.out.println("The element with key " + key
                                                                                + " has been removed successfully");
                                } else {
                                                System.out.println("The entered key does not exists");
                                }
                                //Clears the hashmap
                                hashMap.clear();
                                // close the scanner to release the resource
                                in.close();
                }

                public static void printValues(Map<String, String> hashMap) {
                                Iterator<Entry<String, String>> iterator = hashMap.entrySet()
                                                                .iterator();
                                System.out.println("There are " + hashMap.size() + " elements in HashMap. They are ");
                                System.out.println("Key : Value");
                                while (iterator.hasNext()) {
                                                Entry<String,String> entry = iterator.next();
                                                System.out.println(entry.getKey() + " : "
                                                                                + entry.getValue());
                                }
                }
}