I know i asked this question already but my program is not running and i have a
ID: 3651887 • Letter: I
Question
I know i asked this question already but my program is not running and i have a mid term tomorrow at 5pm, please help me solve this..there's going to be something like this in my midterm tomorrow...help pleeeeease....will rate lifesaver i swear....How to use Arrays library's built in sort and binary-search methods
import java.io.*;
import java.util.*;
public class Lab5
{
public static void main( String[] args ) throws Exception
{
if (args.length < 1 )
{
System.out.println("CMD LINE INPUT ERROR: Must enter one filename on command line ");
System.exit(0);
}
Scanner infile1 = new Scanner( new File(args[0]) ); // load this file into the array then sort using Arrays.sort
int[] array = new int[20]; // Array can hold 20 ints - your input file will not fill the array
int arrCnt = 0; // num of values put into the array so far.
// READ THE NUMBERS FROM FILE1 INTO THE ARRAY. THEN SORT USING Arrays.sort
while ( infile1.hasNextInt() )
array[ arrCnt++ ] = infile1.nextInt();
infile1.close();
System.out.print("ORIGINAL ORDER " + args[0] + " " );
printArray( array, arrCnt ); // must come out in original order as in file1
// YOUR LINE OF CODE HERE (add below or replace this comment) THAT SORTS THE ARRAY USING Arrays LIBRARY
System.out.print("AFTER SORTING " + args[0] + " " );
printArray( array, arrCnt ); // must come out in sorted order
// WARNING: BINARY SEARCH WILL NOT WORK UNLESS ARRAY CORRECTLY SORTED
Scanner kbd = new Scanner( System.in );
do
{
System.out.print("Enter positive number to search for in array: ");
int key = kbd.nextInt();
if (key==0) break; // 0 is the user's signal to quit
// startInd INCLUSIVE but stopInd NOT inclusive, otherwise we would pass in cnt-1
// YOUR CODE HERE (below this comment) THAT SEARCHES THE ARRAY USING Arrays LIBRARY
int ind = -1; // replace -1 with a call to binarySearch // REMEMBER: stopInd not inclusive
if ( ind < 0 ) // a negative return value indicates the key was NOT found
System.out.println( key + " NOT FOUND");
else
System.out.println( key + " FOUND at index " + ind );
} while( true ); // this is an infinite loop - only way out is to BREAK out
} // END MAIN
// ########################################################################################
// USE THIS METHOD AS GIVEN: DO NOT CHANGE
private static void printArray( int[] arr, int cnt )
{
for( int i=0 ; i<cnt ;++i )
System.out.printf("%d ", arr[i] );
System.out.println();
}
} // END PROGRAM5
Thanks for taking the time out to help me, i swear i appreacite it..