Reputation: 9
The problem is :
a program prompts the user to enter a list of six positive integers, and then declares an array of six elements. The program should sort the list’s elements in ascending order. In addition, the program should display both the original and the sorted list.
My code is:
import java.util.*;
public class q2 {
static Scanner scan = new Scanner (System.in);
public static void main (String[] args ) {
int i;
int [] fList = new int [6];
int [] sList = new int [6];
System.out.println ("Enter 6 positive integers :");
for ( i=0 ; i<fList.length ;i++)
fList[i]=scan.nextInt();
for( i=0 ; i<fList.length ; i++)//to copy
fList=sList;
int min;
{
for ( i=0;i<sList.length ; i++)//sort
min = i ;
for (int j=i+1 ; i<sList.length ; i++)
if (sList[j] < sList[min] )
min=j;
int temp=sList[i];
sList[i]=sList[min];
sList[min]=temp;
}
System.out.println("Original array : ");
for ( i=0 ; i < fList.length ; i++)
System.out.println(fList[i] + " ");
System.out.println();
System.out.println("Array after sorting :");
for ( i=0 ; i < sList.length ; i++)
System.out.println(sList[i] + " ");
}
}
it says :
q2.java:23: variable min might not have been initialized if (sList[j] < sList[min] ) ^ q2.java:27: variable min might not have been initialized sList[i]=sList[min]; ^ 2 errors
while I have already initialize it
Upvotes: 0
Views: 84
Reputation: 20431
You should use braces for all blocks even when they're not needed (at the moment) - otherwise it can get unwieldy. Also you don't really need both i
and min
, but I left it in.
import java.util.*;
public class q2 {
static Scanner scan = new Scanner(System. in );
public static void main(String[] args) {
int i;
int[] fList = new int[6];
int[] sList = new int[6];
System.out.println("Enter 6 positive integers :");
for (i = 0; i < fList.length; i++) {
fList[i] = scan.nextInt();
}
//to copy
for (i = 0; i < fList.length; i++) {
fList = sList;
}
//sort
for (i = 0; i < sList.length; i++) {
int min = i;
for (int j = i + 1; i < sList.length; i++) {
if (sList[j] < sList[min])
min = j;
int temp = sList[i];
sList[i] = sList[min];
sList[min] = temp;
}
}
System.out.println("Original array : ");
for (i = 0; i < fList.length; i++) {
System.out.println(fList[i] + " ");
}
System.out.println();
System.out.println("Array after sorting :");
for (i = 0; i < sList.length; i++) {
System.out.println(sList[i] + " ");
}
}
}
Upvotes: 0
Reputation: 15533
You initialize min inside the for
loop. The compiler can not know if sList
is empty or not. If it is empty, then the initialization code will not be run. That's why it is complaining that the variable may not have been initialized.
You should initialize your min
to some value beforehand.
That begin said, the first for loop after the declaration of min
does not make any sense: min
will always be equal to sList.length-1
then (and i
will be sList.length
. You should rethink your logic.
Upvotes: 0
Reputation: 45070
int min;
You've not initialized the min
. I can see that you've done min = i ;
in the for
loop, but that's still conditional(the for
may not always execute, say in the case, the array is empty) and that's why the compiler says that. You need to initialize it with a default value
int min = 0;
or assign some value to it outside any if
or for
, or any conditional statement in that case.
Upvotes: 4