Java NameLengthNotValidException总是在domainclass中抛出

时间:2019-03-20 17:10:39

标签: java exception

重要提示:可能是菜鸟问题,但我对解决方法一无所知。

我有一个UI类,可用来使用扫描仪输入姓名和性别(参见以下代码)

var fontName = "project-icons",
    gulp = require("gulp"),
    sass = require("gulp-sass"),
    sourcemaps = require("gulp-sourcemaps"),
    iconfont = require("gulp-iconfont"),
    iconfontCss = require("gulp-iconfont-css");

var sassOptions = {
    errLogToConsole: true,
    outputStyle: "expanded"
};

function iconfont(done) {
    gulp.src(["./icons/*.svg"])
        .pipe(
            iconfontCss({
                fontName: fontName,
                path: "sass",
                targetPath: "../sass/static/icons/_icons.sass",
                fontPath: "./fonts/",
                cssClass: "icon"
            })
        )
        .pipe(
            iconfont({
                formats: ["ttf", "eot", "woff", "woff2", "svg"],
                fontName: fontName,
                normalize: true,
                fixedWidth: true,
                centerHorizontally: true
            })
        )
        .on("glyphs", function(glyphs, options) {})
        .pipe(gulp.dest("./fonts/"));
    done();
}

function desktop_styles() {
    return gulp
        .src("./sass/_en/style.sass")
        .pipe(
            sourcemaps.init({
                loadMaps: true,
                identityMap: true,
                sourceRoot: "../css/"
            })
        )
        .pipe(sass(sassOptions).on("error", sass.logError))
        .pipe(sourcemaps.write())
        .pipe(gulp.dest("./css/"));
}

function desktop_styles_rtl() {
    return gulp
        .src("./sass/_ar/style.sass")
        .pipe(
            sourcemaps.init({
                loadMaps: true,
                identityMap: true,
                sourceRoot: "../css/"
            })
        )
        .pipe(sass(sassOptions).on("error", sass.logError))
        .pipe(sourcemaps.write())
        .pipe(gulp.dest("./css/lang/rtl"));
}

function mobile_styles() {
    return gulp
        .src("./sass/en/mobile/style.sass")
        .pipe(sass(sassOptions).on("error", sass.logError))
        .pipe(gulp.dest("./css/m"));
}

function mobile_styles_rtl() {
    return gulp
        .src("./sass/rtl/m/style.sass")
        .pipe(sass(sassOptions).on("error", sass.logError))
        .pipe(gulp.dest("./css/lang/rtl/m"));
}

gulp.task(
    "watch:all",
    gulp.series(
        "mobile_styles",
        "mobile_styles_rtl",
        "desktop_styles",
        "desktop_styles_rtl",
        function() {
            gulp.watch("./sass/**/*.sass", ["mobile_styles"]);
            gulp.watch("./sass/**/*.sass", ["mobile_styles_rtl"]);
            gulp.watch("./sass/**/*.sass", ["desktop_styles"]);
            gulp.watch("./sass/**/*.sass", ["desktop_styles_rtl"]);
        }
    )
);

gulp.task(
    "watch:AllDesktop",
    gulp.series("desktop_styles", "desktop_styles_rtl", function() {
        gulp.watch("./sass/**/*.sass", ["desktop_styles"]);
        gulp.watch("./sass/**/*.sass", ["desktop_styles_rtl"]);
    })
);

gulp.task(
    "watch:desktop_styles_rtl",
    gulp.series("desktop_styles_rtl", function() {
        gulp.watch("./sass/**/*.sass", ["desktop_styles_rtl"]);
    })
);

gulp.task(
    "watch:desktop_en",
    gulp.series("desktop_styles", function() {
        gulp.watch("./sass/**/*.sass", ["desktop_styles"]);
    })
);

gulp.task(
    "watch:mobile_rtl",
    gulp.series("mobile_styles_rtl", function() {
        gulp.watch("./sass/**/*.sass", ["mobile_styles_rtl"]);
    })
);

gulp.task(
    "watch:mobile_en",
    gulp.series("mobile_styles", function() {
        gulp.watch("./sass/**/*.sass", ["mobile_styles"]);
    })
);

gulp.task(
    "watch:AllMobile",
    gulp.series("mobile_styles", "mobile_styles_rtl", function() {
        gulp.watch("./sass/**/*.sass", ["mobile_styles"]);
        gulp.watch("./sass/**/*.sass", ["mobile_styles_rtl"]);
    })
);

在域类中,我在设置器中检查该名称的长度,但是无论我尝试了什么,它总是会引发异常。调试时,我选择的任何名称的长度都奇怪地介于6到12个字符之间。 (请参见下面的代码)

 private static void geefNaamEnGeslacht(int aantal) {
    String naam, geslacht;
    for (int i = 0; i < aantal; i++) {
        System.out.print("Wat is de naam van speler " + (i + 1) + " (min 6 tekens, max 12 tekens)? ");
        naam = sc.next();
        naam += sc.nextLine();
        System.out.print("Wat is het geslacht van speler " + (i + 1) + "(man=m vrouw=v)? ");
        geslacht = sc.next();
        System.out.println(naam.length());
        try {
            dc.maakSpelerAan(naam, geslacht.charAt(0));
        } catch (NameLengthNotValidException e) {
            System.out.println(e.getMessage());
            i--;
        }
    }
}

任何想法或技巧都很珍贵。

dc.maakSpelerAan的

EDIT1 代码

public final void setNaam(String naam) {
    int lengte = naam.length();
    if (naam.length() < 6 || naam.length() > 12) {
        throw new NameLengthNotValidException("Je naam moet tussen 6 en 12 tekens liggen!");
    }
    this.naam = naam;

}
s.maakspeler的

EDIT2 代码

public void maakSpelerAan(String naam, char geslacht){
    s.maakSpeler(naam, geslacht);   
}

EDIT3使用正确的图像重新发布变量“ naam”的图像在调试时进入IF循环之前。在接口类中,MINLENGTHNAAM和MAXLENGTHNAAM分别设置为6和12。

Loop

EDIT4 我的接口类中的MINLENGTH和MAXLENGTH代码

public void maakSpeler(String naam, char geslacht){
    if (spelCompleet()) {
        throw new ListFilledException("De lijst met spelers is vol !");
    }

    if (!controleerUniekeNaam(naam)) {
        throw new NotUniqueNameException();
    }

    Kaart[] schatkaarten = new Kaart[STARTAANTAL];
    Kaart[] kerkerkaarten = new Kaart[STARTAANTAL];

    for (int i = 0; i > STARTAANTAL; i++) {
        schatkaarten[i] = stapels.get(SCHATKAART).geefBovensteKaart();
        kerkerkaarten[i] = stapels.get(KERKERKAART).geefBovensteKaart();
    }
    try{
    Speler s = new Speler(naam, geslacht);
    s.getKaarten(kerkerkaarten, schatkaarten);
    spelers.add(s);
    }catch (IllegalArgumentException | NameLengthNotValidException e){
       throw e;
   }

    if (spelCompleet()) {
        //UC2
        spelers.get(EERSTESPELERINDEX).setBeurt(true);
    } 


}

}

1 个答案:

答案 0 :(得分:0)

在“ Edit4”中,您需要将绿线更改为:

if(naam.length() > MAXLENGTHNAAM || naam.length() < MINLENGTHNAAM) {

只是一个逻辑问题:)