I'm a beginner in Java and need some help with this problem. First of all let me show you my actual program.
import java.util.Scanner;
import java.util.Arrays;
public class IntArraySort {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Array-Size: ");
int num = in.nextInt();
int[] arr = new int[num];
for(int i = 0; i < arr.length; i++){
System.out.printf("%2d-tes element: ", i+1);
arr[i] = in.nextInt();
}
System.out.printf("%nOutput before sort: ");
System.out.println(Arrays.toString(arr));
int minIndex, tmp;
int n = arr.length;
for(int i = 00; i < n - 1; i++) {
minIndex = i;
for(int j = i + 1; j < n; j++)
if (arr[j] < arr[minIndex])
minIndex = j;
if (minIndex != i) {
tmp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = tmp;
}
System.out.printf("%n%2d-ter Run: ", i+1);
System.out.println(Arrays.toString(arr));
}
}
}
It's a program to sort an Int-array from smallest to biggest number while you scan the size of the array and the numbers via keyboard input. But I want to scan my array from a .dat-file. I named it sort.dat and it looks like this:
0
20
12
8
16
6
10
14
2
18
4
21
4
-1
-3
What do I need to change in my Java-program to scan in my sort.dat file via input redirection (IntArraySort.java < sort.dat) on Unix?
sort.dat
. So, it is unclear why you just don'tjava IntArraySort <sort.dat
– Ingo Jan 16 at 19:25