Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.io.*;
- import java.util.Arrays;
- public class Lab9
- {
- public static void main(String[] args) throws IOException
- {
- final int MAX_COUNT = 30;
- if (args.length != 2)
- {
- System.out.println("Usage: java Lab8 <filename> integer-search-value");
- System.exit(0);
- }
- Scanner infile = new Scanner(new File(args[0]));
- int[] array = new int[MAX_COUNT];
- int count = 0;
- while (infile.hasNextInt())
- {
- array[count] = infile.nextInt();
- ++count;
- }
- showArray(array, count);
- System.out.println("Array Capacity: " + array.length);
- System.out.println("Array In-Use Count: " + count);
- System.out.println("Original Array: Is array sorted? " + isSorted(array));
- Arrays.sort(array);
- System.out.println("Modified Array: Is array sorted? " + isSorted(array));
- // TODO: 4. Extract the search value from args....
- int searchValue = args[2];
- // TODO: 5. Add in the call to search the array using the Java Arrays.binarySearch
- int index = Arrays.binarySearch(array, searchValue);
- if(index > 0 && index < array.length )
- {
- System.out.println("The element " + searchValue + " was found at index " + index + ".");
- }
- else
- {
- System.out.println("Error. The element " + searchValue + "was not found.");
- }
- // Print out a message indicating if the element was found or not found.
- }
- public static boolean isSorted(int[]arr)
- {
- if(arr == null)
- {
- return false;
- }
- else if(arr.length == 0)
- {
- return true;
- }
- for(int i = 0; i < arr.length-1; i++)
- {
- if (arr[i] > arr[i+1])
- {
- return false;
- }
- }
- return true;
- }
- public static void showArray(int[] arr, int count)
- {
- System.out.println("Array has " + count + " in use elements");
- System.out.print("Elements: ");
- for (int ii = 0; ii < count; ++ii)
- System.out.print(arr[ii] + " ");
- System.out.println();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement