Here is my code and I think my swapping method isn't working which I need for the sorting algorithm to work which Lethalwire helped me figure out.
I also get an error for array out of bounds
import java.util.Scanner;
public class ArrayTestFA {
public static void main(String[] args) {
Scanner in = new Scanner (System.in);
int input;
int var = 0;
System.out.println("How many values would you like your array to hold?");
var = in.nextInt();
int array[] = new int[var];
for(int count=0; count<array.length; count++){
System.out.println("Enter the value for array index: " + count);
input = in.nextInt();
}
int index = 0;
int smallestValueIndex = 0;
for (index=0; index < array.length; index++){
smallestValueIndex = index;
for(int j = index + 1; j < array.length; j++){
if (array[j] < array[smallestValueIndex]){
smallestValueIndex = j;
}
}
swap(array, array[index], array[smallestValueIndex] );
}
printArray(array);
}
private static void swap(int[] array,int index, int smallestValueIndex) {
int swap1 = array[index];
array[index] = array[smallestValueIndex];
array[smallestValueIndex] = swap1;
}
private static void printArray(int[] array){
for(int count=0; count< array.length; count++){
System.out.println(array[count]);
}
}
}
Edited by An Alien, 27 February 2011 - 09:08 PM.


Sign In
Create Account


Back to top









