Home > OS >  Print Java Array Input by User
Print Java Array Input by User

Time:09-23

The program has a 2D array of size [5][3] (columns and rows respectively). I have a problem on how to print the user input, but I think the code below works fine (also not sure).

My question is: How to print the array in this order?

Shirt # 1: [S,red,bench] Shirt # 2: [L,blue,Wrangler] ...so on

Below is the code.

import java.util.Scanner;

public class Main
{
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int sNumber = 4;
        // ang sNumber ay Shirt Number/Shirt Numbering
        String sSize,sColor,sBrand;
        
        String[][] shirt = new String[5][3];
        for (int counter = 0; counter <= sNumber; counter  ){
            System.out.println("Enter Shirt#"   (counter   1)   " Size: ");
            shirt[counter][0] = input.next();
            
            System.out.println("Enter Shirt#"   (counter   1)   " Color: ");
            shirt[counter][1] = input.next();
            
            System.out.println("Enter Shirt#"   (counter   1)   " Brand: ");
            shirt[counter][2] = input.next();
            
        }
        
        
        
    }
}

CodePudding user response:

Add this lines of code after your for:

// this loop will print the array
for (int counter = 0; counter <= sNumber; counter  ) {

  System.out.print("Shirt # "   (counter   1)   " : ["
      shirt[counter][0]   ","
      shirt[counter][1]   ","
      shirt[counter][2]   "] ");

}

The final code will look like this:

import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    int sNumber = 4;
    // ang sNumber ay Shirt Number/Shirt Numbering
    String sSize, sColor, sBrand;

    String[][] shirt = new String[5][3];
    for (int counter = 0; counter <= sNumber; counter  ) {
      System.out.println("Enter Shirt#"   (counter   1)   " Size: ");
      shirt[counter][0] = input.next();

      System.out.println("Enter Shirt#"   (counter   1)   " Color: ");
      shirt[counter][1] = input.next();

      System.out.println("Enter Shirt#"   (counter   1)   " Brand: ");
      shirt[counter][2] = input.next();

    }

    // this loop will print the array
    for (int counter = 0; counter <= sNumber; counter  ) {

      System.out.print("Shirt # "   (counter   1)   " : ["
          shirt[counter][0]   ","
          shirt[counter][1]   ","
          shirt[counter][2]   "] ");

    }

  }
}

CodePudding user response:

An alternative way to achieve this would be to use a class for the Shirt and define a toString method to tell println how to display the object as a string.

An example is shown below.

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

class Shirt {
    private final String size;
    private final String color;
    private final String brand;
    public Shirt(String size, String color, String brand) {
        this.size = size;
        this.color = color;
        this.brand = brand;
    }
    public String getSize() {
        return size;
    }
    public String getColor() {
        return color;
    }
    public String getBrand() {
        return brand;
    }
    @Override
    public String toString() {
        return "["   size   ","   color   ", "   brand   "]";
    }
}

public class Main
{
    public static void main(String[] args) {

        List<Shirt> shirts = new ArrayList<>();
        try(Scanner input = new Scanner(System.in)) {

            System.out.println("How many shirts?");
            int numShirts = input.nextInt();           
            
            for (int counter = 0; counter < numShirts; counter  ){
                System.out.println("Enter Shirt#"   (counter   1)   " Size: ");
                String size = input.next();
                
                System.out.println("Enter Shirt#"   (counter   1)   " Color: ");
                String color = input.next();                
                
                System.out.println("Enter Shirt#"   (counter   1)   " Brand: ");
                String brand = input.next();
                shirts.add(new Shirt(size, color, brand));
            }        
        }

        for (int i = 0; i < shirts.size(); i  ) {
            System.out.println("Shirt # "  (i   1)  ":"   shirts.get(i));
        }
    }
}

You wouldn't have to make the Shirt properties final. I just like to make things immutable.

  • Related