检查Scanner的错误?

时间:2017-04-24 03:21:54

标签: java validation java.util.scanner

所以我决定制作一个程序来制作每个都有名字的Orc对象。

import java.util.*;
public class Main {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        for (int x = 0; x < 10; x++){

            System.out.println("Input the name of your Orc here:");

            String name = input.nextLine(); //THIS is where the inputed name is registerd.

            Orcmaker Orc = new Orcmaker(name);

            System.out.println(Orc);

            input.close();
        }

        System.out.println("Fire");

        //Find a way to run validation code while not obstructing the for loop to make multiple orc objects.

    }
}

所以我做了for循环来完成代码,这样我就可以为每个兽人命名,直到最终我成为10个兽人。问题是我现在遇到问题注入输入验证,这些验证不会使某些代码行变成空白,因为循环吞噬了重要代码。

此外,我只是不知道如何制作它,以便我的if语句可以看到一个数字或特殊字符无效。因为这些是名字我只想要一个字符串。

只是为了完整&#39;这里是我的构造函数。这是我自己制作的这个迷你项目中唯一的其他课程。

public class Orcmaker {

private String name;
private int age;
private String weapon;

public Orcmaker(String OrcName){
    this.name = OrcName;
}

@Override
  public String toString() {
    return String.valueOf(name);
  }

}

0 个答案:

没有答案