问题
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 whole code: I get error in the for loops when trying to comparing two values using .equals(not sure if its right method,,... I am 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) {
}
}}
回答1:
An
ArrayListis not anarray. The syntax toget the element at index iis different:list.get(i)vsarr[i]. Read up on ArrayList, especially theget(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 anArrayList. Of what?! Now, this isn't technically illegal, but if your java compiler supports type parameters, you really need to use them. Write insteadList<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?
回答2:
You can't use array notation on an ArrayList. You need to call get(index) instead.
回答3:
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
回答4:
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) {
回答5:
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)
回答6:
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)) {
}
回答7:
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));
}
}
}
来源:https://stackoverflow.com/questions/18814182/the-type-of-the-expression-must-be-an-array-type-but-it-resolved-to-arraylist-t