在这种情况下如何将此java错误窗口打印出来?

时间:2017-11-02 12:34:07

标签: java user-interface window textfield

我正在编写一个程序,在弹出窗口中为用户返回3个字母字符串的所有可能组合的列表,以输入3位数的电话号码。算法代码是完美的,我不需要帮助。唯一的问题是返回用户的错误消息。返回空白输入错误消息的代码很好。当用户输入无效字符(任何不是2,3,4,5,6,7,8或9)时,除非整个输入都是无效字符,否则不会返回错误消息。例如,如果我输入123,则当" 1"时,错误窗口不会弹出。字符串中唯一的字符无效。但是,如果我输入" 111" (所有字符都无效),然后显示错误消息。 如果用户在不是所有字符都无效的情况下输入了无效字符,我想返回错误窗口。示例:输入123,应该为用户提供错误窗口。

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);      
    }
}

1 个答案:

答案 0 :(得分:0)

以下应该做的伎俩

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();
}

此解决方案使用Regular Expressions(简称regex)。它测试提供的String是否与2 - 9中的任何字符匹配一次或多次(这是+正在做的事情)

这甚至会检查输入是否为空,所以如果你想要你可以放弃单独检查空虚。如果您不希望此检查空虚,则可以使用!theInput.matches("[2-9]*")作为*检查前一部分是否存在零次或更多次。