The objective is to make a periodic table of elements search engine. The user is asked questions to narrow down the results or if the user knows the name of the element it just be entered in. Once entered in or the element is found it's going to give information about the element. I only have a few details and a few elements to get the code working and more elements and details will be added such as structure and atomic weight.
Whenever the result is found it doesn't give the information it just gives a blank screen. I thought to put it in the class as toString and then calling it at the bottom of my main was enough but I can't figure out what I'm missing. It seems like a simple system out print command but that doesn't work either.
Any help is greatly appreciated.
import java.util.ArrayList;
import javax.swing.*;
public class Main {
public static void main(String args[]) {
ArrayList<Element> e = new ArrayList<Element>();
String choice, element = "", searchBy, symbol, chemGroupBlock, list = "", str = "";
e.add(new Element("Hydrogen", "Nonmetal", "H", 1));
e.add(new Element("Helium", "Nobel Gas", "He", 2));
e.add(new Element("Lithium", "Alkali Metal", "Li", 3));
choice = JOptionPane.showInputDialog(null,
"Do you know the name of the element you're looking for?"
+ "(Yes/No)", "Welcome to the Chem Table!", 3);
if (choice.equalsIgnoreCase("yes")) {
element = JOptionPane.showInputDialog(null,
"Enter the name of the Element.. ",
"Welcome to the Chem Table!", JOptionPane.PLAIN_MESSAGE);
}else if (choice.equalsIgnoreCase("no")) {
searchBy = JOptionPane.showInputDialog(null,
"Search by: 'chemical group block' or 'symbol'?", "Welcome to the Chem Table", 3);
if (searchBy.equalsIgnoreCase("chemGroupBlock")) {
chemGroupBlock = JOptionPane.showInputDialog(null,
"Enter Chemical Group Block (Nonmetal/Nobel Gases/Alkalies)",
"Welcome to The Chem Table", JOptionPane.PLAIN_MESSAGE);
for (int i = 0; i < e.size(); i++){
if (e.get(i).getchemGroupBlock().equals(chemGroupBlock)){
list = list + e.get(i).getName() + "\n";
}
}
element = JOptionPane.showInputDialog(null, list,
JOptionPane.PLAIN_MESSAGE);
} else if (searchBy.equalsIgnoreCase("symbol")) {
symbol = JOptionPane.showInputDialog(null,
"Enter the element symbol:(He/NaCl/Xe)",
"Welcome to the Chem Table",
JOptionPane.PLAIN_MESSAGE);
for (int i = 0; i < e.size(); i++) {
if (e.get(i).getSymbol().equals(symbol)) {
list = list + e.get(i).getName() + "\n";
}
}
element = JOptionPane.showInputDialog(null, list,
JOptionPane.PLAIN_MESSAGE);
}
}
for (int i = 0; i < e.size(); i++) {
if (e.get(i).getName().equals(element)) {
str = e.get(i).toString();
}
}
JOptionPane.showMessageDialog(null, str, "Element Results", 1);
}
}
Here is the class for elements
public class Element {
private String name;
private String chemGroupBlock;
private String symbol;
private int atomicNumber;
public Element(String name, String chemGroupBlock, String symbol, int atomicNumber) {
this.name = name;
this.chemGroupBlock = chemGroupBlock;
this.symbol = symbol;
this.atomicNumber = atomicNumber;
}
public String getName(){
return name;
}
public String getchemGroupBlock(){
return chemGroupBlock;
}
public String getSymbol(){
return symbol;
}
public int getatomicNumber(){
return atomicNumber;
}
public String toString() {
return "Element: " + name + "\nChemical Group Block: " + chemGroupBlock + "\nSymbol: " + symbol + "\nAtomic Number" + atomicNumber;
}
}
Aucun commentaire:
Enregistrer un commentaire