Iam trying to compare each string or int in array with another array then print the results according to whether the string exists or not in the other array: Below is the wh
Your error is here:
if (Stocks[i].equals(NameofFileinDir[j])) > 0) {
First, you should say Stocks.get(i).equals(NameofFileinDir.get(j)
The equals
function returns a boolean, so you compare wether true/false > 0
.
You can just drop the > 0)
If you dont want to use for each loop then 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 Stocks[i], have this :
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));
}
}
}
An ArrayList
is not an array
. The syntax to get the element at index i
is different: list.get(i)
vs arr[i]
. Read up on ArrayList, especially the get(int)
method.
Research what String.equals(Object) returns and what that means. Glancing at Object.equals(Object) also wouldn't hurt, since every object in java gets this one by default.
Consider using a foreach loop instead of the old for loop. The old for loop can be crazy useful, but there's no point here. Look how much 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);
}
}
}
ArrayList Stocks = new ArrayList();
. Cool. So you have an ArrayList
. Of what?! Now, this isn't technically illegal, but if your java compiler supports type parameters, you really need to use them. Write instead List<String> stocks = new ArrayList<String>()
. Now everyone reading your code immediately knows what the list holds. Also, the compiler will guarantee that your list will only contain strings. If you try and do something insane or stupid, like throwing some Integers and Maps into the list with your strings, the compiler will puke.
If what you print in the if..else is what you mean, your logic is fatally flawed. You print "stock not in list of files" if the current stock string and file string you happen to be comparing are not equal. But logically, does this mean none of the stock strings and file strings are equal?
change Stocks[i] to Stocks.get(i) and NameofFileinDir[j] to NameofFileinDir.get(j)
Additional suggestions:
Also make NameofFileinDir to A hashSet for better performance
Also follow java camelCase naming convention like change NameofFileinDir to nameofFileinDir
You can't get ArrayList objects by using []
operator like arrays.Use get()
method on ArrayList
to get the objects.
So change the code
if (Stocks[i].equals(NameofFileinDir[j])) > 0) {
to
if (Stocks[i].equals(NameofFileinDir.get([j]))) > 0) {
If you're using ArrayList, then you need to use the get(index) method to access the variable at that index. Also the .equals(object) returns a boolean value so no need of > 0
if (Stocks.get(i).equals(NameOfFileINDir.get(j)) {
}