The type of the expression must be an array type, but it is allowed by ArrayList (an attempt to compare a string in two arrays

I try to compare each row or int in an array with another array, then print the results according to whether the row exists in another array: Below is the whole code: I get an error in for loops when trying to compare two values ​​using .equals (not sure its the correct method, ... I'm new to this) Please help!

public class comparer { public void compare (){ ArrayList NameofFileinDir = new ArrayList (); ArrayList Stocks = new ArrayList(); // populate array with files names in dir try { Scanner reads = new Scanner (new File("G:/Programming/StockDataDownload/NameOfFileinDir.txt")); String FileCode; while (reads.hasNext()){ FileCode = reads.nextLine(); //read next line NameofFileinDir.add(FileCode); } reads.close(); } catch (IOException e) { } // populate array with original stocks try { Scanner reads = new Scanner (new File("G:/Programming/StockDataDownload/AllSecurities.txt")); String StockCode; while (reads.hasNext()){ StockCode = reads.nextLine(); //read next line Stocks.add(StockCode); } reads.close(); for(int i = 0; i < Stocks.size(); i++) { for (int j = 0; j < NameofFileinDir.size(); j++) { if (Stocks[i].equals(NameofFileinDir[j])) > 0) { System.out.println("Stock:" + Stocks[i]);} else{ System.out.println("Stock not in files:" + Stocks[i]); } } } } catch (IOException e) { } }} 
+6
source share
7 answers
  1. An ArrayList not an array . The syntax for get the element at index i is different: list.get(i) vs arr[i] . Read the ArrayList , especially the get(int) method.

  2. Investigating what String.equals(Object) returns and what it means. A look at Object.equals(Object) will not hurt either, since every object in java gets this file by default.

  3. Consider using the foreach loop instead of the old for the loop. The old for loop might be crazy useful, but it makes no sense. See how cleaner this is:

     for (String stock : Stocks) { for (String filename : NameofFileinDir) { if (stock.equals(filename)) { System.out.println(" Stock: " + stocks); } else { System.out.println("Stock not in files: " + stocks); } } } 
  4. ArrayList Stocks = new ArrayList(); . Cool. So you have an ArrayList . Whom?! Now this is not technically illegal, but if your java compiler supports type parameters, you really need to use them. Instead, write List<String> stocks = new ArrayList<String>() . Now everyone who reads your code will immediately know what the list is doing. In addition, the compiler ensures that your list contains only strings. If you try to do something crazy or stupid, for example, throw some integers and cards into a list with your own lines, the compiler will be wary.

  5. If what you type in if..else is what you mean, your logic is deadly wrong. You print “stock is not in the file list” if the current stock line and the file line you are comparing are not equal. But does this logically mean that none of the stock lines and file lines are equal?

+10
source

You cannot use an array notation on an ArrayList . You need to call instead of get(index) .

+3
source

change stocks [i] to Stocks.get (i) and NameofFileinDir [j] to NameofFileinDir.get (j)

Additional offers:

Also make NameofFileinDir for hashSet for better performance

Also follow the java camelCase assignment convention, e.g. change NameofFileinDir, to nameofFileinDir

0
source

You cannot get ArrayList objects using the [] operator, such as the arrays.Use get() method on ArrayList , to get the objects. Therefore change the code

  if (Stocks[i].equals(NameofFileinDir[j])) > 0) { 

to

  if (Stocks[i].equals(NameofFileinDir.get([j]))) > 0) { 
0
source

Your mistake:

 if (Stocks[i].equals(NameofFileinDir[j])) > 0) { 

First, you have to say Stocks.get(i).equals(NameofFileinDir.get(j) The equals function returns a boolean, so you compare true/false > 0 You can simply discard > 0)

0
source

If you are using an ArrayList, you need to use the get (index) method to access the variable at that index. Also .equals (object) returns a boolean, so no need> 0

 if (Stocks.get(i).equals(NameOfFileINDir.get(j)) { } 
0
source

If you do not want to use for each cycle, you can follow this.

 for(int i = 0; i < Stocks.size(); i++) { for (int j = 0; j < NameofFileinDir.size(); j++) { if (Stocks[i].equals(NameofFileinDir[j])) > 0) { System.out.println("Stock:" + Stocks[i]);} else{ System.out.println("Stock not in files:" + Stocks[i]); } } } 

In the abovecode, instead of having stock [i], do the following:

 for(int i = 0; i < Stocks.size(); i++) { for (int j = 0; j < NameofFileinDir.size(); j++) { if (Stocks.get(i).equals(NameofFileinDir.get(j))) > 0) { System.out.println("Stock:" + Stocks[i]);} else{ System.out.println("Stock not in files:" + Stocks.get(i)); } } } 
0
source

Source: https://habr.com/ru/post/953869/


All Articles