Click here to Skip to main content
15,868,136 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to input a 2*3 String array from the user which contains details about the customer including their customer ID, their name and their city name. I want to sort according to their ID and display accordingly. But I'm getting the above error.

Ex:
Input:
50
Atharva
Mumbai

20
Sam
Delhi

Output:
20
Sam
Delhi

50
Atharva
Mumbai

What I have tried:

My logic is:
String info[][] = new String[2][3];
        String temp;
        Scanner sc = new Scanner(System.in);
        for (int i = 0; i < 2; i++) {
            for (int j = 0; j < 3; j++) {
                info[i][j] = sc.nextLine();
            }

        }
        for (int i = 0; i < 2; i++) {
            for (int j = 0; j < 3; j++) {
                if (info[i][0].compareTo(info[i + 1][0]) > 0) {
                    temp = info[i][0];
                    info[i][0] = info[i + 1][0];
                    info[i + 1][0] = temp;
                }
                System.out.println(info[i][j]);
            }

        }
Posted
Updated 10-Jan-22 21:36pm
v2

You declare the array as [2][3] - a total of six elements:
String info[][] = new String[2][3];
Then you loop though each valid index:
for (int i = 0; i < 2; i++) {
    for (int j = 0; j < 3; j++) {
But ... you then try to access element [i + 1] which is not a valid index:
if (info[i][0].compareTo(info[i + 1][0]) > 0) {
because when i is 1, i + 1 is 2 and the valid indexes are zero and one only.

Hence the system complains: "Array index out of bounds"

Even then, you are trying to load three "users" who each have three "Items of information" so your array isn't going to be the right size anyway!

I would strongly suggest that instead of a 2D array, you create a class to keep related information in, and create a 1D array of the class instances.
 
Share this answer
 
Comments
Sourabh Jambale 11-Jan-22 4:06am    
Actually the question asks to have a 2d array.
Quote:
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
if (info[i][0].compareTo(info[i + 1][0]) > 0) { // <<== here is your problem

In the highlighted line, when i=1, your code is trying to access info[1 + 1][0], hence the error.

Have also a look at Arrays.sort() in Java with examples - GeeksforGeeks[^].
 
Share this answer
 
v2
Comments
Sourabh Jambale 11-Jan-22 4:09am    
What should be done so it accesses the next element? It is recommended to have a 2d array in the question.
CPallini 11-Jan-22 5:23am    
When i=1 there is NO 'next element', so you simply change the loop to
for (int i = 0; i < 1; i++) {

However, you didn't properly implement the sort.
Sourabh Jambale 11-Jan-22 5:36am    
but doesn't i<2 means that it would skip 2? the only values will be 0 and 1 and when i=2, it will come out of the loop?
CPallini 11-Jan-22 6:01am    
That's true. But that also implies that info[i + 1] would go 'out of bounds'.
Sourabh Jambale 11-Jan-22 7:10am    
I did
for (int i = 0; i < 1; i++) {

but when I display the array, it only shows the first part of the array(only details of 1 customer and not the second one). Can you please help with the sorting part? I'm new to Java.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900