用户将数据传递给另一个 class

pass data by user to another class

我尝试将学生姓名从主 class 传递给学生 class,但我在运行时遇到错误。

主要class是

 public static void main(String[] args) {
              Students students =new Students ();
              Scanner in = new Scanner (System.in);
              String [] name = new String [5];
              for (int i =0; i <=4; i++){
              System.out.print("Enter student name " + (i+1) + " : ");
                       name [i]= in.next();  
              }
              students.SetData(name);
              students.GetData();

     }// Main

学生class是

public class Students {
    private String Names [] = new String [5]; 
    
    public void SetData (String [] Names){
              for (int i=0 ; i <= Names.length; i++){
                       this.Names [i] = Names[i] ; //HERE is my error
              } //for 
    } //SetData method
    
    public void  GetData (){
              for (int i=0 ; i <= Names.length; i++){
                       System.out.println("Name of student " + (i+1) + " is " +Names[i]);
              } //for
    } //GetData mehtod 
} //Students Class

另外,如何让我的程序检查用户是否输入了 STRING?

谢谢大家

你应该写 i < Names.length 在你的循环中,所以它们提前一步终止。

Names.length == 5比最后一个有效索引大一。

吹毛求疵: 您不应该以大写字母开头变量,它们是为 class 名称保留的。在 [] 数组运算符之前使用 space 看起来也很奇怪。

Also how can I make my program check if user entered STRING or not ?

使用正则表达式验证用户输入:

public static void main(String[] args) {
    isValidName("John  Doe");
    isValidName("John1 Doe");
}   

public static boolean isValidName(String name){
    boolean isValid = name.matches("[a-zA-Z]*\s*[a-zA-Z]*$");
    if(isValid) {
        System.out.println(name + " is a valid name");
    } else {
        System.out.println(name + " is not a valid name");
    }
    return isValid;
}

输出:

John  Doe is a valid name
John1 Doe is not a valid name

根据您的需要更改模式[a-zA-Z]*\s*[a-zA-Z]*$。 更多信息请参考:https://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.html

要接受 动态编号 条目,请使用 ArrayList 而不是 字符串数组 要将名称验证为字符串,请使用带正则表达式的模式匹配功能。

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

        Students students =new Students ();
        Scanner scanner = new Scanner (System.in);
        List<String> names = new ArrayList<String>();
        String input = new String();
        do{
            System.out.print("Enter student name or 'e' to exit : ");
            input = scanner.next();
            if(isValidName(input) && (!input.equals("e"))){
                names.add(input);
            }
        }while(!input.equals("e"));

        students.SetData(names);
        students.GetData();
        
    }

    public static boolean isValidName(String name){
        String regExp = "[a-zA-Z]*\s*[a-zA-Z]*$";
        boolean isValid = name.matches(regExp);
        System.out.println("isValid :" + isValid);
        return isValid;
    }

}

class Students {

    private String names [];
    
    public void SetData (List<String> names){
         this.names = names.toArray(new String[0]);
    }
    
    public void  GetData (){
        for (int i=0 ; i < names.length; i++){
            System.out.println("Name of student " + (i+1) + " is " +names[i]);
        }
    }
}