Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
user input n number of rows in 2d array
Elements are stored in an array of size [n][4].
Input Format:
First line of the input is an integer “n” that denotes the number of rows.
Next n lines contain the elements of the row

Output Format:
if 2 rows match then separate it by hyphen
If no rows is same, print "None".
If the array size is negative, print "Invalid Input" and terminate the program.

Sample Input 1:
5
3 1 2 4
2 4 5 1
3 1 2 4
2 4 5 1
3 1 2 4
Sample Output 1:
1-3
1-5
2-4
3-5

Sample Input 2:
3
3 1 2 4
2 4 5 1
3 4 2 5
Sample Output 2:
None

Sample Input 3:
-5
Sample Output 3:
Invalid Input

What I have tried:

import java.util.Arrays; 
import java.util.Scanner;
import java.lang.*;
import java.util.*; 

public class Main 
{ 
    public static void main(String[] args)
{ 
     
    Scanner sc = new Scanner(System.in); 
    int n = sc.nextInt(); 
   
    if(n <= 0)
    {
       System.out.println("Invalid Input");
    }
    else
    {
        int[][] data = new int[n][4]; 
        for (int i = 0; i < n; i++) 
        { 
            for (int j = 0; j < 4; j++) 
                { 
                    data[i][j] = sc.nextInt(); 
                }
        } 
        Object[] array = new Object[4];
        Object[] array1 = new Object[4];
        int idx = 0;
        int idx1 = 0;
        for (int i = 0; i < n; i++) 
        { 
            for (int j = 0; j < 4; j++) 
                { 
                   array[idx] = data[i][j];
                   array1[idx1] = data[i + 1][j];
                   idx++;
                   idx1++;
                    if (Arrays.deepEquals(array, array1))
                        {
                            System.out.println(i + "-" + j);
                        }
                        else
                        System.out.println("None");
                }
                }
        } 
       
    
    

        
} 

}
Posted
Updated 23-Jun-19 23:01pm
Comments
Richard MacCutchan 4-Aug-17 11:44am    
And your question is ... ?
ANANYA PRAMANIK 4-Aug-17 12:38pm    
i am not getting the desired output
Richard MacCutchan 4-Aug-17 13:22pm    
Then you need to do some debugging to find out where the code is going wrong. You know what values are being input so you should be able to see where the error lies. Either use your debugger, or add some print statements to show the different values as the program runs.
Member 14510381 24-Jun-19 5:12am    
import java.util.*;
import java.lang.*;
public class team_rival {

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int c=0,n=sc.nextInt();
if(n>0) {
int[][] a = new int[n][4];
for(int i=0;i
CHill60 26-Jun-19 4:22am    
What was the point of this?

Many problems in your code
- your code try to compare a row with next row when you need to compare a row with all other rows. Row 0 must be compared against row 1, row 2, row 3 ...
- your comparison test is misplaced.

There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 
import java.util.*;
import java.lang.*;
public class team_rival {

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int c=0,n=sc.nextInt();
if(n>0) {
int[][] a = new int[n][4];
for(int i=0;i
 
Share this answer
 
Comments
wseng 26-Jun-19 12:50pm    
Your answer is incompleted.
Member 14510381 26-Jun-19 22:52pm    
I have posted full solution, but it was showing the preview as above.If you know how to post full length solution then tell me.
wseng 27-Jun-19 23:25pm    
Tray wrap your code using code tag

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