Reputation: 189
I am trying to make a periodic table program in java. the code is pretty huge so I am not putting the whole thing here. this is the class I am using to store and print the values of each element:-
class elements
{
int atomicno;
String name;
double weight;
void setdetails(int a, String n, double w)
{
atomicno=a;
name=n;
weight=w;
}
void showdetails()
{
System.out.println("Atomic no.=" +atomicno);
System.out.println("Name=" +name);
System.out.println("Weight=" +weight);
}
}
In the main class I made 118 objects of this class and hard-coded info of each element using setdetails()
like this:
elements H=new elements();
H.setdetails(1, "Hydrogen", 1.008);
now I am asking the user to enter an element symbol like H, He, Li, etc to search for its info but I think you have to use an actual object
to call the showdetails()
function like H.showdetails()
but i still tried with the String
entered by the user like this:
System.out.print("Enter the correct exact symbol to search:");
String sym=sc2.nextLine();
for(i=0; i<118; i++)
{
if(s[i].equals(sym))
{
System.out.println();
sym.showdetails();
System.out.println();
}
}
and undoubtedly sym.showdetails();
doesn't work even if the name of the object and the symbol entered by the user is same. so now if I do H.showdetails();
it works but sym.showdetails();
doesn't work even if sym
is H
. what can be done now?
Upvotes: 1
Views: 215
Reputation: 12883
In Java, a Map associates (or maps) one object (a key) to another (a value). There are different implementations of Maps in Java, but since you're new, just use the HashMap implementation. It's a good general purpose implementation when you don't have to access the HashMap in multiple threads.
A simple way in your case would be to write a function that creates the map, populates it with your element classes, and then returns the fully populated map. After that, you can perform a lookup based on the element's symbol.
e.g. (please note, I've put your class name in caps according to Java style (when in Rome!))
// add these lines to top of file
import java.util.Map;
import java.util.HashMap;
// example population function
public Map<String, Element> getElements() {
Map<String, Element> elements = new HashMap<>();
elements.put("H", new Element(1, "Hydrogen", 1.008));
// etc...
return elements;
}
// Getting your map of data and using it.
Map<String, Element> elements = getElements();
System.out.println(elements.get("H"));
Another thing to keep in mind when you're declaring a Map. You should tell Java what types the key and value are supposed to be. In this code we're associating the element symbol (a string) with your element class, so we need to put that in angle braces. e.g.
Map<String, Element> elements;
Notice that the return type of the function matches the type of the variable elements
.
There are two other things that are different about this example than the code in your question that you should take note of.
First, it uses a constructor to populate the element. A constructor is a like an initializer function with the same name as your class, and it can be used in conjunction with the new
operator.
Second, it defines a toString()
method. A toString()
method returns a string representation of an object in Java. This is preferable to simply printing from within the class because it can be used in many more situations than printing from a method.
Here's the code.
public class Element {
private int atomicNumber;
private String name;
private double weight;
public Element (int a, String n, double w) {
atomicNumber = a;
name = n;
weight = w;
}
public String toString() {
return "Atomic number = " + atomicNumber + ", " +
"Name = " + name + "," +
"Weight = " + weight;
}
}
Upvotes: 1