- #1
apiwowar
- 96
- 0
So i haven't done java in about two years, its been a while since I've seen arrays. THe assignment is to fix a program that was given to me. I found one error but in the insertion sort part i can't get it to run the while loop. I can't make any fundamental changes to the program, basically i just have to fix the error in the while loop to get it running.
any pointers or advice would be appreciated, just need a kick in the right direction.
below is the program
import java.io.*; // for BufferedReader
import java.util.*; // for StringTokenizer
public class Prog1Original { // A simple program with no classes
public static void main(String[] args) throws IOException {
int number[] = new int[100];
int ct, num, size, i, j, insel;
BufferedReader stdin = new BufferedReader(
new InputStreamReader(System.in) );
System.out.print("Input integers: ");
size = 0; // The smallest index of an array is always 0
String inputLine = stdin.readLine(); // All input must be on a single line
StringTokenizer input = new StringTokenizer(inputLine);
while (input.hasMoreTokens()) { // extract the integers from the input line
num = Integer.parseInt(input.nextToken());
number[size] = num;
size = size + 1;
}
System.out.println();
System.out.print("The original numbers: ");
for (ct = 0; ct < size; ct++) {
System.out.print(number[ct]);
System.out.print(" ");
}
System.out.println();
// "Insertion Sort" the numbers
for (i = 1; i < size; i++) { // Starting with the second array element
insel = number;
j = i;
while ( (number[j] > insel) && (j >= 0) ) { // shift larger elements right
number[j + 1] = number[j];
j = j - 1;
}
number[j] = insel; // insert the number to its proper place
}
System.out.print("The sorted numbers: "); // Output the sorted array
for (ct = 0; ct < size; ct ++) {
System.out.print(number[ct]);
System.out.print(" ");
}
System.out.println();
}
}
any pointers or advice would be appreciated, just need a kick in the right direction.
below is the program
import java.io.*; // for BufferedReader
import java.util.*; // for StringTokenizer
public class Prog1Original { // A simple program with no classes
public static void main(String[] args) throws IOException {
int number[] = new int[100];
int ct, num, size, i, j, insel;
BufferedReader stdin = new BufferedReader(
new InputStreamReader(System.in) );
System.out.print("Input integers: ");
size = 0; // The smallest index of an array is always 0
String inputLine = stdin.readLine(); // All input must be on a single line
StringTokenizer input = new StringTokenizer(inputLine);
while (input.hasMoreTokens()) { // extract the integers from the input line
num = Integer.parseInt(input.nextToken());
number[size] = num;
size = size + 1;
}
System.out.println();
System.out.print("The original numbers: ");
for (ct = 0; ct < size; ct++) {
System.out.print(number[ct]);
System.out.print(" ");
}
System.out.println();
// "Insertion Sort" the numbers
for (i = 1; i < size; i++) { // Starting with the second array element
insel = number;
j = i;
while ( (number[j] > insel) && (j >= 0) ) { // shift larger elements right
number[j + 1] = number[j];
j = j - 1;
}
number[j] = insel; // insert the number to its proper place
}
System.out.print("The sorted numbers: "); // Output the sorted array
for (ct = 0; ct < size; ct ++) {
System.out.print(number[ct]);
System.out.print(" ");
}
System.out.println();
}
}