I am learning Java, now working with arrays. I am doing an excercise where I am going to make the user input the values for a two-by-three integer array. Then I am supposed to find the smallest value. I do this by using an if-statement. The problem is that the compiler always prints the number "0" for the smallest value. I cannot find out what´s wrong with my code. Can anyone please help me? The code is as follows:
import java.util.Scanner;
public class Oppgave79k
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
int t[][] = new int[2][3];
int smallest = t[0][0];
for (int row = 0; row < t.length; row++)
{
for (int column = 0; column < t[row].length; column++)
{
System.out.println("Enter values for array: ");
t[row][column] = input.nextInt();
if (t[row][column] < smallest)
{
smallest = t[row][column];
}
}
}
for (int row = 0; row < t.length; row++)
{
for (int column = 0; column < t[row].length; column++)
{
System.out.printf("%d ", t[row][column]);
}
}
System.out.printf("Smallest element is: %d\n", smallest);
}
}
Arrays.sortand grab the first element from the sorted array?