Question: Java. I am having trouble sorting a list of integers from a file in numerical order WITHOUT using an array list. The insertInOrder method is

Java. I am having trouble sorting a list of integers from a file in numerical order WITHOUT using an array list. The insertInOrder method is incomplete and incorrect. Every example I have seen uses an array list. The help is appreciated.

Input from file: 59 82 29 52 60

Sample Output: 29 52 59 60 82

My Code:

import java.util.*; import java.io.*;

public class Lab { static final int MAX_CAPACITY = 30; // HARDOCED PLENTY BIG. WE'LL DO TRIM AFTER public static void main( String args[] ) throws Exception { if (args.length < 1 ) { System.out.println("usage: $ java Lab3 "); System.exit(0); } int[] arr = new int[ MAX_CAPACITY ]; int count=0; Scanner infile = new Scanner( new File( args[0] ) ); while ( infile.hasNextInt() ) { insertInOrder( arr, count, infile.nextInt() ); ++count; } arr = trimArray( arr, count ); printArray( arr ); // NOTE: NO COUNT PASSED IN }// END MAIN // ############################################################################################################ // MUST USE ENHANCED FOR LOOP IN THIS METHOD // (YOUR TRIM BETTER HAVE BEEN WRITEN CORRECTLY) static void printArray( int[] array ) { // PRINT EACH NUMBER WITH A SPACE AFTER IT for( int x: array) { System.out.print(x); System.out.print(" "); } System.out.println(); // LEAVE THIS HERE } static int[] trimArray( int[] array, int count ) { int [] arrInt = new Int[count]; for(int x = 0; x < arrInt.length; x++) { arrInt[x] = array[x]; } return arrInt; } // THIS IS NOT CORRECT static void insertInOrder( int[] arr, int count, int newVal ) { int saveVal = newVal if(newVal> saveVal) { } arr[count] = newVal; // MUST REPLACE IT }

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!