如何在这些方法中处理String输入?

时间:2016-11-08 19:31:15

标签: java

如何在以下每种方法中处理用户输入字符串。我尝试了很多次使用不同的建议,错误不断循环。如何通过添加到下面的方法而不是在当前状态下修改它们来强制用户输入int而不是字符串。

    private void readMonth(Scanner keyboardIn)
    {
        boolean success = false;
        while(!success)
        {   
            try
            {
                System.out.print("Enter the month the account opened: ");
                int m = keyboardIn.nextInt();
                dateOpened.setMonth(m);
                success = true;
            }catch(Exception e)
            {
                System.out.println(e.getMessage());
            }   
          } 
       }    


    // Enter the year, checking for errors
    private void readYear(Scanner keyboardIn)
    {
        boolean success = false;
        while(!success)
        {   
            try
            {
                System.out.print("Enter the year the account opened: ");
                int y = keyboardIn.nextInt();
                dateOpened.setYear(y);
                success = true;
            }catch(Exception e)
            {
                System.out.println(e.getMessage());
            }
        }
    }

2 个答案:

答案 0 :(得分:1)

当您尝试输入除数字以外的任何字符时,idValidation函数将返回false。我还打印了退货,这样你就可以很容易地理解它是如何工作的。

import java.util.Scanner;

public class Daniel {
    private static Scanner sc;

    private static boolean idValidation(String id) {
        try {
            Integer.parseInt(id);
            System.out.println("return true");
            return true;
        } catch (NumberFormatException e) {
            System.out.println("return false");
            return false;
        }
    }

      private static void readMonth(Scanner keyboardIn)
        {
            boolean success = false;
            while(!success)
            {   
                try
                {
                    System.out.print("Enter the month the account opened: ");
                    String m = keyboardIn.nextLine();
                    if(!idValidation(m)){
                        return;
                    }
//                  dateOpened.setMonth(m);
                    success = true;
                }catch(Exception e)
                {
                    System.out.println(e.getMessage());
                }   
              } 
           }    


        // Enter the year, checking for errors
        private static void readYear(Scanner keyboardIn)
        {
            boolean success = false;
            while(!success)
            {   
                try
                {
                    System.out.print("Enter the year the account opened: ");
                    String y = keyboardIn.nextLine();
                    if(!idValidation(y)){
                        return;
                    }

//                  dateOpened.setYear(y);
                    success = true;
                }catch(Exception e)
                {
                    System.out.println(e.getMessage());
                }
            }
        }
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        readMonth(sc);
        readYear(sc);
    }
}

答案 1 :(得分:0)

演示代码

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Scanner;

public class ReadDateFromKeyboard {

    private static final int NUM_OF_PARAMS = 3;
    private static final int DAY = 0;
    private static final int MONTH = 1;
    private static final int YEAR = 2;

    private static final String DAY_MESSAGE  = "Enter the day the account opened: ";
    private static final String MONTH_MESSAGE = "Enter the month the account opened: ";
    private static final String YEAR_MESSAGE  = "Enter the year the account opened: ";

    public static void main(String[] args) {
        Scanner keyboardIn = new Scanner(System.in);
        int[] params = new int[NUM_OF_PARAMS];

        readDay(keyboardIn, params);
        readMonth(keyboardIn, params);
        readYear(keyboardIn, params);

        Date date = getDate(params);

        String pattern = "dd-MM-yyyy";
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);

        System.out.println("\nDate: " + sdf.format(date));

        keyboardIn.close();
    }

    private static void readDay(Scanner keyboardIn, int[] params) {
        genericRead(DAY_MESSAGE, keyboardIn, params, DAY);
    }

    private static void readMonth(Scanner keyboardIn, int[] params) {
        genericRead(MONTH_MESSAGE, keyboardIn, params, MONTH);
    }

    private static void readYear(Scanner keyboardIn, int[] params) {
        genericRead(YEAR_MESSAGE, keyboardIn, params, YEAR);
    }

    private static void genericRead(String message, Scanner keyboardIn, int[] params, int paramType) {
        boolean success = false;
        while (!success) {
            try {
                System.out.print(message);
                params[paramType] = keyboardIn.nextInt();

                success = true;
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
        }
    }

    public static Date getDate(int[] params) {
        return getDate(params[0], params[1], params[2]);
    }

    private static Date getDate(int day, int month, int year) {
        Calendar c = Calendar.getInstance();

        c.set(year, month - 1, day, 0, 0);

        return c.getTime();
    }
}

演示输出

Enter the day the account opened: 24
Enter the month the account opened: 1
Enter the year the account opened: 1982

Date: 24-01-1982