recognizing a date in the form: 03/02 for February 3 , For simplicity, we consider that each month is made up of 30 days

40 views Asked by At

this is the code :

package Tp2;

import java.util.Date;
import java.util.Scanner;

public class EX2_Date_forme {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        while (true) {
            System.out.println("give a string or (exit) to leave : ");
            String input = sc.nextLine();

            if (input.equals("exit")) {
                break;
            } else {
                System.out.println();
                Date(input);
            }
        }
    }

    public static void Date(String input) {
        int len = input.length();
        int state = 0;
        int tracker = 0;
        for (int i = 0; i < len; i++) {
            char c = input.charAt(i);
            boolean c_isBetween_zero_in_AndNine = Character.getNumericValue(c) >= 0 && Character.getNumericValue(c) <= 9;
            boolean c_isBetween_zero_out_AndNine = Character.getNumericValue(c) > 0 && Character.getNumericValue(c) <= 9;
            switch (state) {

                case 0:
                    if (c == '0'){
                        state = 1;
                        tracker++;
                    }
                    else if (c == '1' || c == '2'){
                        state = 2;
                        tracker++;
                    }
                    else if (c == '3'){
                        state = 3;
                        tracker++;
                    }
                    else System.out.println("error");
                    break;
                case 1:
                    if (c_isBetween_zero_out_AndNine) {
                        state = 4;
                        tracker++;
                    }
                    else System.out.println("error");
                    break;
                case 2:
                    if (c_isBetween_zero_in_AndNine){
                        state = 4;
                        tracker++;
                    }
                    else System.out.println("error");
                    break;
                case 3:
                    if (c == '0') {
                        state = 4;
                        tracker++;
                    }
                    else System.out.println("error");
                    break;
                case 4:
                    if (c == '/') {
                        state = 5;
                        tracker++;
                    }
                    else System.out.println("error");
                    break;
                case 5:
                    if (c == '0'){
                        state = 6;
                        tracker++;
                    }
                    else if (c == '1'){
                        state = 7;
                        tracker++;
                    }
                    else System.out.println("error");
                    break;
                case 6:
                    if (c_isBetween_zero_out_AndNine){
                        state = 8;
                        tracker++;
                    }
                    else System.out.println("error");
                    break;
                case 7:
                    if (c == '0' || c == '1' || c == '2'){
                        tracker++;
                        state = 8;
                    }
                    else System.out.println("error");
                    break;
                case 8:
                    if (tracker == len) System.out.println("in the language");
                    else
                        System.out.println("error");
                    break;
            }
        }
    }
}

We want to recognize a date in the form: 03/02 for February 3, limited to the current year.

For simplicity, we consider that each month is made up of 30 days.

When, for example, I type "01/01" in debug mode, everything works as expected until the last iteration where i=4, len=5, and state=8. However, the for loop doesn't continue to enter case 8.

i expected to see the "in the langage message" but I got no result

it worked when i added this after the loop and it worked :

    if (state == 8 && tracker == len) {
        System.out.println("in the language");
    } else {
        System.out.println("error");
    }

but i want it to enter the case number 8

0

There are 0 answers