My problem is that the result shows me only a not OK in all cases, and this is just a class | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

My problem is that the result shows me only a not OK in all cases, and this is just a class

import java.util.Scanner; public class arrayd { Scanner in=new Scanner(System.in); String array []= new String [2]; public void reed() { for (int i = 0; i < array.length; i++) { System.out.println("enter::"); array[i]=in.next(); } compare(array); } public void compare(String array[] ) { for (int j = 0; j < array.length; j++) { System.out.println("enter::"); array[j]=in.next(); } for (int k = 0; k < array.length; k++) { if (array[k] .equals(array)) { System.out.println(" ok"); } else { System.out.println(" not ok"); } } } }

5th Nov 2021, 7:36 AM
Es -x10
Es -x10 - avatar
8 Answers
+ 1
So you have 2 elements in String array <array> and you want to check whether those 2 elements were equal or is it something else? I'm still not getting what you want to compare actually ...
5th Nov 2021, 8:23 AM
Ipang
+ 2
Why there is no main method though? What you mean by "comparing the elements of two arrays"? you only have 1 String array named <array>. It seems you read <array> elements twice, once in reed() method, and once in compare() method.
5th Nov 2021, 8:12 AM
Ipang
+ 1
Can you explain to me what you're doing with this code? I'm not getting the idea of it.
5th Nov 2021, 7:59 AM
Ipang
+ 1
I understand you now, I realized my mistake, I need to know another matrix so that I can compare, thank you
5th Nov 2021, 8:30 AM
Es -x10
Es -x10 - avatar
+ 1
Es -x10 If you want to compare two arrays then you can do this: Arrays.equals(arr1, arr2)
5th Nov 2021, 10:05 AM
A͢J
A͢J - avatar
0
Here I am comparing the elements of two arrays by string and this class
5th Nov 2021, 8:04 AM
Es -x10
Es -x10 - avatar
0
Es -x10 array is a collection of String Check your this comparison array[k].equals (array) Do you think this is right?
5th Nov 2021, 8:15 AM
A͢J
A͢J - avatar
0
public class master { public static void main(String[] args) { array a = new array(); a.reed(); } } this is main and I want to compare these arrays.. Can I or not ??
5th Nov 2021, 8:19 AM
Es -x10
Es -x10 - avatar