简体   繁体   中英

Java - Go back to beginning of program flow if condition failed

Just as I write this I can imagine how basic the question is but here I go ..

I have a Java client wherein I validate a 'date' input entered by user and if valid pass it along elsewhere. I am using JOptionPane to show input dialog box.

Basically when an invalid date format is entered by the user my program quits and the user has to restart the program again. Instead of that I wanted the pop-up inputdialog box to be shown after 'Invalid Date Entered" message is displayed. What would be the best way to pass control to that line of code again ?

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Properties;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import javax.swing.JOptionPane;

public class Client {

  static SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

  public static void main(String[] args) throws IOException, ParseException {

    Properties prop = new Properties();
    InputStream IP = null;
    //  SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

    try {

      IP = new FileInputStream("Client.properties");
      prop.load(IP);
      String host = prop.getProperty("ServerIP");
      String port = prop.getProperty("Port");
      int port1 = Integer.parseInt(port);

      Socket s = new Socket(host, port1);
      String reportDate = JOptionPane.showInputDialog("Enter Date:");
      PrintWriter out = new PrintWriter(s.getOutputStream(), true);

      /**
       * User Input Validation
       */
      if (reportDate.matches("^((19|20)\\d\\d)-(0?[1-9]|1[012])-(0?[1-9]|[12][0-9]|3[01])$")) {

        out.println(reportDate);
        s.close();

        System.exit(0);
      } //if
      else {
        String Error = "INCORRECT date format entered!";
        JOptionPane.showMessageDialog(null, Error);
      } //else
    } catch (IOException ex) {
      ex.printStackTrace();
    } finally {
      if (IP != null) {
        try {
          IP.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    }
  }
}

在成功情况下使用System.exit(0)时,只需将代码包装在while (true) { }的无限循环中即可。

Do something like this (pseudocode):

String reportDate = "badDate";
while (!reportDate.matches("^((19|20)\\d\\d)-(0?[1-9]|1[012])-(0?[1-9]|[12][0-9]|3[01])$"))
{
//do all the things that are currently in your if statement
}

I know this is not directly related to the response you are looking for. In fact the direct answer to your question is use a loop and it will work the way you are expecting.

However, if you want to do this for selecting date you should consider using Java SwingX .

SwingX library has a lot of good components including excellent date picker

在此处输入图片说明

I think this may help you to take a valid Date :

Date reportDate;
do { 
    try {
        String str = JOptionPane.showInputDialog("Enter Date:");
        reportDate = new SimpleDateFormat("yyyy-MM-dd").parse(str);
        break;// loop break when got a valid date e.g. 2016-09-16
    } catch (Exception mistake) {
        System.err.println("Invalid input: " + mistake);
    }
} while (true);

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