-1

My problem is that I need to getSymbol from Element class. I would normally establish an object in PeriodicTable like this: Element e = new Element(); then use e.getSymbol within method in order to use it for comparison.

So, in order to complete first task and print entire list of elements, I declared an array within PeriodicTable like this: Element[] objects = new Element[ARRAY_SIZE]; I'm guessing I declared it correctly, as it does run entire list of elements.

Again, I am having problems getting getSymbol into my method in PeriodicTable. Any helpful suggestions, please?

For this method, a user will input a symbol for an element. The method will search for the element and return its index (in the array). Then, it will use the index to display that single element and all of its other information, using the toString method from the Element class.

public int searchBySymbol(String sym)
    {       
        int index = 0;
        boolean found = false;

        for (int i = 0; i < objects.length; i++)
        {
            objects[i] = objects.getSymbol;
        }   
        while (index < objects.length && !found)
        {
            if (objects[index].equals(sym))
            {
                found = true;
            }
            else
            {
                index++;
            }
        }       
        if(found)
        {
            System.out.println("Found at position: " + index);
            System.out.println(objects[index].toString());
        }
        else
        {
            System.out.println("Not found");
        }       
    }
KitKat2
  • 1
  • 2
  • please show your code instead of describing it. It will be much easier to help you! – Dan O Apr 23 '17 at 20:12
  • I tried to but I'm on a VPN and it would not copy over. Maybe if I log into here on VPN.... Will try. – KitKat2 Apr 23 '17 at 20:14
  • For this method, a user will input a symbol for an element. The method will search for the element and return its index (in the array). Then, it will use the index to display that single element and all of its other information, using the toString method from the Element class. – KitKat2 Apr 23 '17 at 20:32
  • This code won't compile. `objects[i] = objects.getSymbol` cannot be valid. – MC Emperor Apr 23 '17 at 20:37
  • @MasterYushi Nope, because `objects` is an array, which doesn't have the field `getSymbol`. An array object is a language construct and therefore it's not overridable. – MC Emperor Apr 23 '17 at 20:51

1 Answers1

-1

You definitely don't need two loops in there first of all, there are two solutions to this:

  1. (Recommended) If searching Elements by symbol will be the your main way of looking up Elements, consider using a HashMap to contain the data rather than an Element array as HashMaps allow look up of objects by a key e.g. HashMap<String, Element>. Lookup the HashMap API or check this example: http://beginnersbook.com/2013/12/hashmap-in-java-with-example/

  2. (Quick fix) Rather than using two loops to get the field and compare, in Java it is good practice to define accessor methods such as getSymbol() and return the field rather than directly accessing it. Using this method you can simplify your code into...

    for (Element e : objects) {
        if (e.getSymbol().equals(sym) {
        return true; 
        }
    }
    //return false after the loop omits the need for an explicit boolean variable`
    

Edit: Usual for loop construct for index access. The index number is essentially tracked by the iterator variable int i so you do not need a separate variable to track it.

for (int i = 0; i < objects.length; i++) {
    if (objects[i].getSymbol().equals(sym)) {
        //print i to show index number
        //print objects[i].toString();
        return true;
    }
}
//print not found...
return false;
Karen
  • 32
  • 5
  • So then I would just need to code further in this method in order to have the index of the symbol returned and also use index to code for returning the rest of the info. about the element?? – KitKat2 Apr 24 '17 at 00:24
  • Yes - if you need the index then use a normal for loop to make things easier to read, I have edited my post and added another example. – Karen Apr 24 '17 at 12:11