Sal
Sal

Reputation: 109

Input and storing Strings in two-dimensional arrays

My teacher explained two dimensional arrays in literally two paragraphs. He didn't give me any information on how to create them besides that and now I have to do an assignment.

I've read up a lot about it and I somewhat understand how a 2D array is like an array of arrays, but I'm still completely and utterly confused about how to apply it.

The assignment itself is very simple. It asks me to create a program that will ask a user for ten Criminal Records, (name, crime, year). This program will store the records in a two-dimensional array and then sort them using the selection sort.

I know this is probably wrong, but here is what I have so far based on what I've read:

    public static void main(String[] args)throws IOException {

    //create array
    String[][] Criminals = new String[10][3]; // create 3 columns, 10 rows

    int i, j;
    int smallest; //smallest is the current smallest element
    int temp; //make an element swap
    String line;

    //loop to request to fill array   
    for (int row = 1; row < Criminals.length; row++){
        for (int col = 1; col < Criminals[row].length; col++){
            System.out.print("Enter a criminal name: ");
            Criminals[row][col] = br.readLine();

        }
    }
}

So far, I'm just trying to get the input and store it.

(Please try to be patient and thorough with me! Coding isn't my strongest point, but I'm trying to learn.) Any help would be amazing! Thanks in advance. :)

Upvotes: 0

Views: 31187

Answers (2)

Marco Altran
Marco Altran

Reputation: 366

import java.util.Scanner;


public class Main {


    public static void main(String[] args) {



        Scanner in = new Scanner(System.in);

             //create array
            String[][] criminals = new String[10][3]; // create 3 columns, 10 rows

            int i, j;
            int smallest; //smallest is the current smallest element
            int temp; //make an element swap
            String line;

            //loop to request to fill array   
            for (int row = 0; row < criminals.length; row++){
                    System.out.print("Enter a criminal name: ");
                    while(in.hasNext()){
                    criminals[row][0] = in.nextLine();
                    System.out.print("Enter a crime: ");
                    criminals[row][1] = in.nextLine();
                    System.out.print("Enter a year: ");
                    criminals[row][2] = in.nextLine();
                    }


            }

    }


}

This will print the commands you need from user and will store it in criminals. You may sort in the end. Since you didn't gave any information how you want it sorted, I will leave it for you to do it.

PS: I changed the 2d array name from Criminals to criminals, it's a java's good practice to not use capital words for attributes and variables (use it only for class names)

Upvotes: 1

Zong
Zong

Reputation: 6230

It looks fine for the most part. You should index arrays starting from 0, not 1. Your current code works but I'm guessing you don't want the same prompt for all entries. Thus it may be a good idea to use a single loop instead:

for (int row = 0; row < Criminals.length; row++) {
    System.out.print("Enter a criminal name: ");
    Criminals[row][0] = br.readLine();

    System.out.print("Enter a crime: ");
    Criminals[row][1] = br.readLine();

    System.out.print("Enter a year: ");
    Criminals[row][2] = br.readLine();
}

Upvotes: 1

Related Questions