简体   繁体   中英

How do I get this java error window to print out in this situation?

I am writing a program to return the user a list of all possible combinations of 3 lettered strings for an input of a 3 digit phone number in a pop up window. Algorithm code is perfect and I do not need help on that. The only issue is returning the user with error messages. The code that returns an error message for a blank input is fine. When the user inputs an invalid character (anything not 2, 3, 4, 5, 6, 7, 8, or 9), it does not return an error message unless their entire input is invalid characters. For example, if I input 123, the error window is not popping up when "1" the only character in the string is invalid. However, if I inputted "111" (all characters are invalid), then the error message shows up. I want to return an error window if the user has invalid characters inputted in a situation where not all the characters are invalid. Example: Inputting 123, should give user an error window.

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.geometry.HPos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
import javafx.scene.text.*;
import javafx.scene.control.Alert; 
import javafx.scene.control.Alert.AlertType; 

public class Runner extends Application 
{

  private TextField input = new TextField();

  private Button outputButton = new Button("Get all possible mnemonics"); 

  @Override
  public void start(Stage primaryStage) 
  {

    GridPane gridPane = new GridPane();
    gridPane.setHgap(5); 
    gridPane.setVgap(5); 

    Text sceneMenu = new Text("Phone digits:\n" + "----------\n" + "0: No letters\n" + "1: No letters\n" +
                 "2: ABC\n" + "3: DEF\n" + "4: GHI\n" + "5: JKL\n" + "6: MNO\n" + "7: PQRS\n" +
                 "8: TUV\n" + "9: WXYZ\n"); 
    sceneMenu.setFont(Font.font("Arial", FontWeight.BOLD, 16)); 
    gridPane.add(sceneMenu, 5, 0, 1, 10);                     
    gridPane.add(new Label("Enter your three digit phone number:"), 0, 0); 
    gridPane.add(input, 1, 0);
    gridPane.add(outputButton, 1, 7);


    gridPane.setAlignment(Pos.CENTER); 
    input.setAlignment(Pos.BOTTOM_RIGHT);  Textbox.
    GridPane.setHalignment(outputButton, HPos.RIGHT); 




    outputButton.setOnAction(e -> {


    String theInput = input.getText();



      if (theInput.isEmpty())
      {
         Alert error1 = new Alert(AlertType.ERROR);
         error1.setTitle("Blank input");
         error1.setHeaderText("BLANK INPUT!");
         error1.setContentText("Please enter a three digit number.");
         error1.showAndWait();

      }

      else
      {

         String [] matches = new String[]{"2", "3", "4", "5", "6", "7", "8", "9"};

         for (String s : matches)
         {
            if (theInput.contains(s)== false)
            {
               Alert error2 = new Alert(AlertType.ERROR);
               error2.setTitle("Invalid digit");
               error2.setHeaderText("INVALID DIGIT!");
               error2.setContentText("Please enter three digits ranging from 2 through 9");
               error2.showAndWait();
               break;
            }

            else
            {
               calculateMnemonics();
               break;
            }


         }         
       }

   });


    Scene scene = new Scene(gridPane, 700, 600);
    primaryStage.setTitle("Customer order");
    primaryStage.setScene(scene); 
    primaryStage.show(); 

  }

  private void calculateMnemonics() 
  {

    String theInput = input.getText();


    Calculator calc = new Calculator(theInput);

    Alert outputWindow = new Alert(AlertType.INFORMATION);
    outputWindow.setTitle("Mnemonic list");
    outputWindow.setHeaderText("Your mnemonic list for your numbers");
    outputWindow.setContentText(calc.printMnemonics());

    outputWindow.showAndWait();

  }


  public static void main(String[]args) 
  {
    launch(args);
  }
}

import java.util.List;
import java.util.LinkedList;
import java.util.Arrays;
public class Calculator 
{
   private String inputtedNumbers;


   public Calculator() 
   {
      this("");
   }


   public Calculator(String inputtedNumbers)
   {
      this.inputtedNumbers = inputtedNumbers; 
   }

   public String getInputtedNumbers()
   {
      return inputtedNumbers;
   }
   public void setInputtedNumbers(String inputtedNumbers)
   {
      this.inputtedNumbers = inputtedNumbers;
   }

    public List<String> listMnemonics(String digits) 
    {
        String[] letters = {"ABC", "DEF", "GHI", "JKL", "MNO", "PQRS", "TUV", "WXYZ"};
        List<String> rec = new LinkedList<>();
        StringBuilder string = new StringBuilder();
        listMnemonics(digits, 0, letters, string, rec);
        return rec;
    }

    private void listMnemonics(String digits, int number, String[] letters, StringBuilder string, List<String> rec) 
{
        if (digits.length() == number) 
        {
            rec.add(string.toString());
            return;
        }
        String letter = letters[digits.charAt(number) - '2'];
        for (int i = 0; i < letter.length(); i++)
        {
            string.append(letter.charAt(i));
            listMnemonics(digits, number + 1, letters, string, rec);
            string.deleteCharAt(string.length() - 1);
        }


    }


    public String printMnemonics()
    {
          Calculator o = new Calculator();
          List<String> list1 = o.listMnemonics(inputtedNumbers);
          String [] stringArray = list1.toArray(new String[0]);
          return Arrays.toString(stringArray);      
    }
}

The following should do the trick

if (!theInput.matches("[2-9]+"))
{
    Alert error2 = new Alert(AlertType.ERROR);
    error2.setTitle("Invalid digit");
    error2.setHeaderText("INVALID DIGIT!");
    error2.setContentText("Please enter three digits ranging from 2 through 9");
    error2.showAndWait();
}
else
{
    calculateMnemonics();
}

This solution makes use of Regular Expressions (regex for short). It tests if the provided String matches any character from 2 - 9 one or more times (that's what the + is doing)

This will even check if the input is empty, so if you wanted you could ditch the separate check for emptiness. If you don't want this to check for emptiness, you can use !theInput.matches("[2-9]*") as the * checks if the preceding part is existing zero or more times.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM