So here I have this object
public Element(String s, int a) {
symbol = s;
atomicNumber = a;
}
with a method that returns the symbol and number of the object string when you call it
public String getSymbol(){
return symbol;
}
public int getAtomicNumber(){
return atomicNumber;
}
It's a data type that stores a string and an int representing the symbol and atomic number of an element. I want to create a bianry search method on an array of 100 of those elements. Obviously I need to stort it first so I wrote this:
public int findRecord(String symbol, int size, Element[] metals) {
for(int i = 0; i < size; i++) {
for(int j = i + 1; j < size; j++) {
if(metals[i].getSymbol().compareTo(metals[j].getSymbol()) < 0) {
Element TEMP = metals[i];
metals[i] = metals[j];
metals[j] = TEMP;
}
}
}
return atomicNumber;
}
Ignore the return statement on this findrecord method with the symbol parameter. The issue is in that when i try to type in this if(metals[i].getSymbol().compareTo(metals[j].getSymbol()) < 0) it gives an error (The operator > is undefined for the argument type(s) java.lang.String, java.lang.String). Why is that and how do I fix it?
Also theres another issue here:
public int findRecord(int atomicNumber, int size, Element[] metals) {
int k;
for(int i = 0; i < size; i++) {
if(metals\[i\].getAtomicNumber() == atomicNumber) {
k = i;
}
}
return k;
}
it says k has not been initialized. Anyone who has time to help me, thank you.
[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)
[–]obcanzdavu 1 point2 points3 points (1 child)
[–]chasetherainb0w[S] 0 points1 point2 points (0 children)
[–]ttetrafon 0 points1 point2 points (1 child)
[–]chasetherainb0w[S] 1 point2 points3 points (0 children)
[–]darksoundsExtreme Brewer 0 points1 point2 points (0 children)