0

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);
}

}

2
  • Why not use Arrays.sort and grab the first element from the sorted array? Commented Jan 26, 2012 at 14:11
  • @mre: That's O(nlogn) instead of O(n). It also modifies the array, unless you make a copy. Commented Jan 26, 2012 at 15:49

4 Answers 4

8

In these lines:

int t[][] = new int[2][3];
int smallest = t[0][0];

you've started off with smallest as 0. So unless you enter a negative number, it's never going to change.

You could use:

int t[][] = new int[2][3];
int smallest = Integer.MAX_VALUE;

to make sure that the first number entered is used.

As an aside, your indentation style is somewhat unconventional. Either

for (...) {
    // Code
}

or

for (...)
{
    // Code
}

look fine to me, but

for (...)
    {
    // Code
    }

is very unusual.

Sign up to request clarification or add additional context in comments.

Comments

2

Your initial value of smallest is 0. If you enter negative numbers in via the input you should get values different than 0.

Consider setting smallest to the first input value.

Comments

0

If you are learning Java or any other programming language

  1. Learn how to debug your code, this will help you understand how computers "think"
  2. If you are at the very beginning, try solving the problem without writing any single line of code, and this will help you to think like a "computer"

Then you would see that your initial assignment of smallest was wrong.

Comments

0

I would suggest you to do the input reading part first and store the 2D array. And then you might able to set smallest= t[0][0]. And then it algorithm will never break. And also I would suggest you to implement a sorting algorithm for this rather than find a smallest element. I can assure it would be a great experience. And also use eclipse IDE which is an open source IDE.There you can debug your point and it will make easy your coding life.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.