0

I've been working on a program that will take the user's input of the name or symbol of an element in the periodic table and then output some facts about that element. After quite a few questions on here I've gotten to the point the program stores all the data correctly, outputs it in the way I want and can accept an input of both the name or symbol. The problem I'm having now is that the breaks I have inserted into a loop are not actually breaking from the loop, and I'm really not sure why. The program will just keep on asking for an input even if it received a correct input. In addition, if the user inputs a symbol rather than a name the program will repeatedly tell the user that their input was invalid before finally outputting correctly (and then restarting the loop rather than breaking as it should). I'm new to Java, so if anyone could help me fix either of these issues and explain why the problem occurred and how they fixed it fairly simply I would greatly appreciate it.

import java.util.Scanner;
public class PeriodicTable {

    public enum Element {
        Hydrogen("H", "Nonmetal", "1.008"),
        Helium("He", "Noble Gas", "4.003"),
        Lithium("Li", "Alkali Metal", "6.941"),
        Beryllium("Be", "Alkaline Earth", "9.012"),
        Boron("B", "Semimetal", "10.811"),
        Carbon("C", "Nonmetal", "12.011"),
        //The rest of the periodic table is here, I just removed it for the sake of this post.

        private String symbol;
        private String group;
        private String weight;

        private Element(String symbol, String group, String weight) {
            this.symbol = symbol;
            this.group = group;
            this.weight = weight;
        }
    }

    static Element cName = null;
    public static void main(String[] args) {

        System.out.println("Enter the name or symbol of an element in the periodic table. ");
        do {
            Scanner reader = new Scanner(System.in);
            String input = reader.nextLine().trim();
            for (Element sy : Element.values()) {
                if (sy.symbol.equalsIgnoreCase(input)) {
                    System.out.println("Element: " + sy + " (" + sy.symbol + ")" + "\nGroup: " + sy.group + "\nAtomic Mass: " + sy.weight);
                    reader.close();
                    break;
                } else {
                    try {
                        cName = Element.valueOf(input.substring(0, 1).toUpperCase() + input.substring(1).toLowerCase());
                        System.out.println("Element: " + cName + " (" + cName.symbol + ")" + "\nGroup: " + cName.group + "\nAtomic Mass: " + cName.weight);
                        reader.close();
                        break;
                    } catch(IllegalArgumentException e) {
                        System.out.println("That name or symbol is not valid. Please try again. ");
                        continue;
                    }
                }
            }
        } while (true);
    }
}
Foxes
  • 1,137
  • 3
  • 10
  • 19

1 Answers1

1

The problem is that the break's are within the for loop, so it only breaks to for loop. If you want to break the do-while loop you can use a label:

outer:
do {
      Scanner reader = new Scanner(System.in);
      String input = reader.nextLine().trim();
      for (Element sy : Element.values()) {
         if (sy.symbol.equalsIgnoreCase(input)) {
                System.out.println("Element: " + sy + " (" + sy.symbol + ")" + "\nGroup: " + sy.group + "\nAtomic Mass: " + sy.weight);
                reader.close();
                break outer;
          } else {
              try {
                    cName = Element.valueOf(input.substring(0, 1).toUpperCase() + input.substring(1).toLowerCase());
                    System.out.println("Element: " + cName + " (" + cName.symbol + ")" + "\nGroup: " + cName.group + "\nAtomic Mass: " + cName.weight);
                    reader.close();
                    break outer;
              } catch(IllegalArgumentException e) {
                    System.out.println("That name or symbol is not valid. Please try again. ");
                    continue;
              }
          }
     }
} while (true);
GBlodgett
  • 12,704
  • 4
  • 31
  • 45
  • Of course, I can't believe I didn't see that! Thanks! I do still have the problem of the program telling the user that their input is invalid when they input a symbol for every item in the enum until it reaches the one where the user's input matches the symbol. Do you know how to get it to tell the user their input is invalid only after it's checked against every item in the enum? – Foxes Dec 01 '18 at 18:30
  • 1
    @Gameskiller01 I would move the print statement outside of the catch block and to after the `for-each` loop. If a element matches, you will break out of the `do-while` and the println will never be reached. – GBlodgett Dec 01 '18 at 18:59