3

I am a novice trying to use basic GUI functions to make a simple question and answer program for the first 56 elements as practice.

However when i run my code, i get errors during the running of the code randomly.

A text box will appear with no dialogue just Message as the name of the text box, and a X button to exit. Sometimes when i press X it goes to the next line in my code, other times it quits out. It is hard for me to reproduce the error, since it seems to happen randomly.

My main method is posted below:

import java.math.*;
import java.text.DecimalFormat;
import java.io.*;
import java.util.*;
import javax.swing.JOptionPane;
import java.util.Random;

//This lab is to study periodc table of elements with simple gui output
public class practiceFiftyEight
{
public static void main(String[] args)
{

    String input;

    boolean answer;

    Random myRan = new Random();

    int randInt = 0;
     //random num from 0 - 56

    String[] arrayElements = {"Hydrogen","Helium","Lithium","Beryllium","Boron","Carbon","Nitrogen","Oxygen","Fluorine","Neon","Sodium","Mangnesium","Aluminium","Silicon","Phosphorus","Sulfur","Chlorine","Argon","Potassium","Calcium","Scandium","Titanium","Vanadium","Chronium","Manganese","Iron","Cobalt","Nickel","Copper","Zinc",
    "Gallium","Germanium","Arsenic","Selenium","Bromine","Krypton","Rubidium","Strontium","Yttrium","Zirconium","Niobium","Molybdenum","Technetium","Ruthenium","Rhodium","Palladium","Silver","Cadmium","Indium","Tin","Antimony","Tellurium","Iodine","Xenon","Cesium","Barium"};

    String[] arrayEleAbriv = {"H","He","Li","Be","B","C","N","O","F","Ne","Na","Mg","Al","Si","P","S","Cl","Ar","K","Ca","Sc","Ti","V","Cr","Mn","Fe","Co","Ni","Cu","Zn","Ga","Ge","As",
    "Se","Br","Kr","Rb","Sr","Y","Zr","Nb","Mo","Tc","Ru","Rh","Pd","Ag","Cd","In","Sn","Sb","Te","I","Xe","Cs","Ba"};

    int repeat;

        do
        {
        randInt = myRan.nextInt(56);

        JOptionPane.showMessageDialog(null, " sizes of arrays " + arrayElements.length + " " + arrayEleAbriv.length);

        JOptionPane.showMessageDialog(null, " What is the symbol of " + arrayElements[randInt]);

        input = JOptionPane.showInputDialog(null," Enter element symbol ( 1 - 56 ) of");

        answer = input.equalsIgnoreCase(arrayEleAbriv[randInt]);

        if(answer)
        {
        JOptionPane.showMessageDialog(null, " Correct " + arrayElements[randInt] + " is represented by " + arrayEleAbriv[randInt] );
        }
        else
        {
            JOptionPane.showMessageDialog(null, " WRONG!!!!! " + arrayElements[randInt] + " is represented by " + arrayEleAbriv[randInt] + " !!!!! " );
        }
        repeat = JOptionPane.showConfirmDialog(null, "Press Yes to repeat, No to quit ", "please confirmm", JOptionPane.YES_NO_OPTION);

        }while(repeat == JOptionPane.YES_OPTION);
    }
}
9
  • 2
    Magnesium is misspelled as "Mangnesium". It might be better to declare variables closer to their use e.g., with input and answer. I wonder, since the class is named practiceFiftyEight and you print the size of the arrays every time around the loop, and yet there are only 56 elements supported, if the problem had to do with an earlier version of your program where the numbers didn't match up? I tried it and was unable to reproduce any error. Commented Sep 10, 2014 at 16:57
  • Do you have anything in your logs ? Commented Sep 10, 2014 at 16:59
  • 1
    You also have several imports that are unused. It would be a good idea, down the road, to read the element data from files rather that hardcoding everything, but I realize this is just a start. Commented Sep 10, 2014 at 16:59
  • 2
    Would you mind to add those errors stack trace here? I've run your program. I was unable to get any error, except misprinting of several elements. Commented Sep 10, 2014 at 19:14
  • 1
    @MockerTim How would i go about that? For the error didnt really put anything on the command line, the program would just hang for a while with no message in the proceeding text box until i clicked X? Could the program just be slow or something? Sorry i am bad with this. Commented Sep 11, 2014 at 11:30

1 Answer 1

3

You should call the swing components from EDT.

And there was also place where NullPointerException could have been thrown, if the user chose Cancel.

The following code should work without freezing.

import java.text.DecimalFormat;
import javax.swing.SwingUtilities;
import javax.swing.JOptionPane;
import java.util.Random;

//This lab is to study periodc table of elements with simple gui output
public class PracticeFiftyEight {
    final static String[] arrayElements = {"Hydrogen","Helium","Lithium","Beryllium","Boron","Carbon","Nitrogen","Oxygen","Fluorine","Neon","Sodium","Magnesium",
                                            "Aluminium","Silicon","Phosphorus","Sulfur","Chlorine","Argon","Potassium","Calcium","Scandium","Titanium","Vanadium",
                                            "Chromium","Manganese","Iron","Cobalt","Nickel","Copper","Zinc","Gallium","Germanium","Arsenic","Selenium","Bromine",
                                            "Krypton","Rubidium","Strontium","Yttrium","Zirconium","Niobium","Molybdenum","Technetium","Ruthenium","Rhodium",
                                            "Palladium","Silver","Cadmium","Indium","Tin","Antimony","Tellurium","Iodine","Xenon","Cesium","Barium"};

    final static String[] arrayEleAbriv = {"H","He","Li","Be","B","C","N","O","F","Ne","Na","Mg","Al","Si","P","S","Cl","Ar","K","Ca","Sc","Ti","V","Cr","Mn","Fe","Co","Ni","Cu","Zn","Ga","Ge","As",
                "Se","Br","Kr","Rb","Sr","Y","Zr","Nb","Mo","Tc","Ru","Rh","Pd","Ag","Cd","In","Sn","Sb","Te","I","Xe","Cs","Ba"};

    public static void main(String[] args) {            

        SwingUtilities.invokeLater(new Runnable() {
            public void run() {

                String input;

                boolean answer;

                Random myRan = new Random();

                int randInt = 0;
                 //random num from 0 - 56                   

                int repeat;

                do {
                    randInt = myRan.nextInt(56);

                    JOptionPane.showMessageDialog(null, " sizes of arrays " + arrayElements.length + " " + arrayEleAbriv.length);

                    JOptionPane.showMessageDialog(null, " What is the symbol of " + arrayElements[randInt]);

                    input = JOptionPane.showInputDialog(null," Enter element symbol ( 1 - 56 ) of");

                    if(input != null) { // if user press cancel, input is null
                        answer = input.equalsIgnoreCase(arrayEleAbriv[randInt]);

                        if(answer) {
                            JOptionPane.showMessageDialog(null, " Correct " + arrayElements[randInt] + " is represented by " + arrayEleAbriv[randInt] );
                        } else {
                            JOptionPane.showMessageDialog(null, " WRONG!!!!! " + arrayElements[randInt] + " is represented by " + arrayEleAbriv[randInt] + " !!!!! " );
                        }
                    } else {
                        System.out.println("input is null: " + input);
                    }

                    repeat = JOptionPane.showConfirmDialog(null, "Press Yes to repeat, No to quit ", "please confirmm", JOptionPane.YES_NO_OPTION);

                } while(repeat == JOptionPane.YES_OPTION);
            }
        });
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.