Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Min Number from array

  1. #1
    Junior Member
    Join Date
    Jan 2022
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Min Number from array

    package com.company;
     
    public class ArrayMinMaxEachString {
        public static void main(String[] args) {
            int[][] array = {
                    {1, 2, 3, 4, 5, 6, 7, 8, 9},
                    {11, 12, 13, 14, 15, 16, 17, 18, 19}
            };
     
            for (int i = 0; i < array.length; i++) {
                for (int j = 0; j < array[0].length; j++) {
                    System.out.print(array[i][j] + " ");
                }
                System.out.println();
            }
     
            //min
            int min = array[0][1];
            for (int k = 0; k < array.length; k++) {
                if (min > k) {
                    min = k;
     
                }
            }
            System.out.println("\n\nMin is: " + min);
        }
    }

    Can you please explain what exactly am I doing wrong? Why doesn't it show the minimum number?

    --- Update ---

    My assignment looks like this, in case it helps anyone:

    Create a two-dimensional 5x8 array of type int and initialize it with an initialization block.
    Find the maximum and minimum value in each "line" and write these values into a 5x2 two-dimensional array.
    Print both arrays.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Min Number from array

    doesn't it show the minimum number?
    Please post the program's output so we can see what you are talking about.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jan 2022
    Posts
    20
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Min Number from array

    Problem #1 is you have not defined a 5x8 array.
    You have defined a 2x9 array [0 to 1][0 to 8].
    Each bracketed set of numbers is one 'row' of the matrix.
            int[][] array = {
                    {1, 2, 3, 4, 5, 6, 7, 8},
                    {11, 12, 13, 14, 15, 16, 17, 18},
                    {111, 112, 113, 114, 115, 116, 117, 118},
                    {211, 212, 213, 214, 215, 216, 217, 218},
                    {311, 312, 313, 314, 315, 316, 317, 318}
            };
    Next problem is that you are initializing min with the SECOND value not the first. Why are you skipping [0][0]?
    The biggest problem however is that you are only looping the single dimension of the array. You need to nest two for loops and process every element of the array. Two dimensions means two for next loops nested.

Similar Threads

  1. How do you add a value to a 2-d array by row number and column number?
    By JohnEliot in forum Java Theory & Questions
    Replies: 1
    Last Post: April 14th, 2013, 06:39 AM
  2. random number array
    By my21 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 12th, 2013, 06:15 AM
  3. How do I return the current number of trades(number) from an array?
    By lovely92 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: December 12th, 2012, 06:15 PM
  4. Labelling Number Array
    By andewor in forum Collections and Generics
    Replies: 0
    Last Post: October 14th, 2012, 03:51 AM
  5. Number Array
    By TheJavaGuy in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 17th, 2011, 04:59 AM