Home > Mobile >  Declare and initialize elements in a matrix Java
Declare and initialize elements in a matrix Java

Time:08-18

I find it challenging to initialize elements in a matrix (2d array). I know how to declare a 2d array and prompt user for input and then print it, but if I have a 2 d array with 5 rows and I in every row want that column 0 is articleNumber, column 1 is price and column 2 is amount I fail to connect the userinput with the "variable".

The task I want to practice and understand is described this way.

"create a matrix that contains information about 5 articles. Each article (row) must contain the article number, price, and number. Show how to declare, assign and print the contents of the matrix (all columns for all rows)."

I would be very thankful if someone could help me out with the initializing. This is what I did so far:

import java.util.Scanner;

public class TwoDArrays 
{

    public static void main(String[] args)
    {
        Scanner s1 = new Scanner (System.in); // create the scanner object
        
        // Variables
        
        int r = 0;
        int c = 0;
        int [][] twodArray; // declare the 2d array object
        
        // prompt user for input
        
        System.out.print("How many rows do you want? ");
        
        r=s1.nextInt();  
        
        System.out.print("How many columns do you want? ");
        
        c=s1.nextInt(); 
        
        
        
        twodArray = new int [r][c]; // the 2d array size is defined by user
        
        
        // insert value.
        
        
        System.out.println("Please insert values ");
        
        for(int i=0; i<r; i  ) 
        {
            
            for(int j=0; j<c; j  ) 
            {
                twodArray[i][j]=s1.nextInt();
                
            }
            
            System.out.println();
            
        }
        
        
        // Prints out values
        
        System.out.println("Here is what you entered");
        
        for(int i=0; i<r; i  ) 
        {
            
            for(int j=0; j<c; j  ) 
            {
                System.out.print(twodArray[i][j]   " " " ");
                
            }
            
            System.out.println();
            
        }
    }
}

CodePudding user response:

There is no need for you to ask how many rows and columns the user wants to enter because those are fixed values specified by your assignment: You need 5 rows - 1 for each item, and 3 columns for the 3 attributes each item has.

You first declare your array as

int[][] items = new int[5][3];

And you could then just initialize/fill that array with a simple loop:

for(int i=0; i<items.length; i  ) {
    System.out.println("Enter Data for item "  i);
    System.out.println("Enter article number:");
    items[i][0] = s1.nextInt();  
    System.out.println("Enter price:");
    items[i][1] = s1.nextInt();
    System.out.println("Enter number:");
    items[i][2] = s1.nextInt();  
}
    

Of course you could still use a nested loop to fill the columns of each item. But with a fixed value of 3 I really see no reason to do that.

Edit:

It should be noted that using a 2D array like this is a really bad choice for a data structure. Your assignment specifically asks for that but in reality you would be much better of creating a class for the Item:

public class Item {
    private int articleNumber;
    private BigDecimal price;
    private int number;
    
    public Item() {
    }
    
    public int getArticleNumber() {
        return articleNumber;        
    }
    
    public void setArticleNumber(int articleNumber) {
        this.articleNumber = articleNumber;        
    }

    public BigDecimal getPrice() {
        return price;
    }
    
    public void setPrice(BigDecimal price) {
        this.price = price;
    }
    
    public int getNumber() {
        return number;
    }
    
    public void setNumber(int number) {
        this.number = number;
    }
}

And then you would use a List or Array of that custom class

final List<Item> itemList= new ArrayList<>();

And using your getters/setters to set the attributes of each item

final Item newItem = new Item();
itemList.add(newItem);
    
System.out.println("Enter article number:");
newItem.setArticleNumber(s1.nextInt());  
// ... etc pp. for the other attributes
  • Related