Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Java向arraylist添加参数_Java_Arraylist_Java.util.scanner - Fatal编程技术网

使用Java向arraylist添加参数

使用Java向arraylist添加参数,java,arraylist,java.util.scanner,Java,Arraylist,Java.util.scanner,我正在测试我的大学课堂,当用户键入“add”时,它会将一个新学生添加到大学课堂的arraylist中 Scanner myInput=new Scanner (System.in); String information=myInput.next(); //I know this is incorrect not sure what else to do directory.addStudent(information); 这里目录是包含学生的arraylist的对象。ad

我正在测试我的大学课堂,当用户键入“add”时,它会将一个新学生添加到大学课堂的arraylist中

   Scanner myInput=new Scanner (System.in);
   String information=myInput.next(); //I know this is incorrect not sure what else to do
    directory.addStudent(information);
这里目录是包含学生的arraylist的对象。addStudent方法在大学课堂上,如下所示:

  public void addStudent (String studentName, long studentID, String address) {
        Student newStu= new Student( studentname, studentID, address);
        collegeList.add(newStu); //here collegeList is the name of the arraylist in College class
   }

不管怎么说,我的问题似乎很简单,但无法解决。我想知道如何分割信息,以便它能够满足addStudent方法中的参数…正如您所看到的,信息将是一个字符串,但我希望studentID是一个长字符串,而不是一个字符串我能做什么

读取后不需要拆分字符串。您可以使用扫描仪读取分隔字符串

我假设您的输入用空格分隔。如果是,您需要执行以下操作:

    String name = myInput.next();
    long id = myInput.nextLong();
    String address = myInput.next();
    dictionary.add(name, id, address);
试试这个:

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class StudentMain {
    private static List<Student> collegeList = new ArrayList<Student> (); 
    public static void main(String...args)throws Throwable {
         String s= null; String name=null; long id=0L; String address = null; 
        System.out.print("What do you want to do today, press q to quit : ");
        Scanner sc = new Scanner(System.in);
        do{
        try{    
                s = sc.next();
                if(s.equalsIgnoreCase("add")){
                    System.out.print("Enter the name of student:  ");
                    name = sc.next();
                    System.out.print("Enter the Id of student : " );
                    id=sc.nextLong();
                    System.out.print("Enter address of student:  ");
                    address = sc.next();
                    addStudent(name,id,address);
                }
        }catch(NumberFormatException e){
            if(!s.equalsIgnoreCase("q"))
                System.out.println("Please enter q to quit else try again ==> ");
            }
        }while(!s.equalsIgnoreCase("q"));
    sc.close();
    }
    private static void addStudent(String name, long id, String address) {
        // TODO Auto-generated method stub
        Student newStu = new Student(name,id,address);
        collegeList.add(newStu);        
    }

}

class Student{
    String name;
    Long id;
    String address;

    Student(String name,Long id,String address){
    this.name= name;
    this.id = id;
    this.address=address;
    }
}
import java.util.ArrayList;
导入java.util.List;
导入java.util.Scanner;
公立班学生{
私有静态列表colleclist=newarraylist();
publicstaticvoidmain(字符串…参数)抛出Throwable{
字符串s=null;字符串名称=null;长id=0L;字符串地址=null;
System.out.print(“您今天想做什么,按q退出:”);
扫描仪sc=新的扫描仪(System.in);
做{
试试{
s=sc.next();
如果(s.equalsIgnoreCase(“添加”)){
系统输出打印(“输入学生姓名:”);
name=sc.next();
系统输出打印(“输入学生Id:”);
id=sc.nextLong();
系统输出打印(“输入学生地址:”);
地址=sc.next();
addStudent(姓名、身份证、地址);
}
}捕获(数字格式){
如果(!s.equalsIgnoreCase(“q”))
System.out.println(“请输入q退出,否则重试==>”;
}
}而(!s.equalsIgnoreCase(“q”));
sc.close();
}
私有静态void addStudent(字符串名称、长id、字符串地址){
//TODO自动生成的方法存根
学生新闻TU=新学生(姓名、id、地址);
添加(新闻TU);
}
}
班级学生{
字符串名;
长id;
字符串地址;
学生(字符串名称、长id、字符串地址){
this.name=name;
this.id=id;
这个地址=地址;
}
}
您可以使用
split()
方法中断您的信息

你要做的是

String str = new String(information);
String[] s = str.split(" ", 3);
我是在空格的基础上拆分的,第二个参数3意味着你必须拆分成3个字符串,即姓名、id、地址

在此之后,您可以通过s[0]获取
名称,通过s[1]获取id,通过s[2]获取地址。

因此,您可以轻松地跳过
addStudent()
方法。但是,您需要根据
addStudent()
method

的参数转换值。请查看各种
X.parseX()
方法,其中X是
Number
的常见子类型。提供有关您的输入的一些信息。名称、id和地址在输入中是如何分开的?通过新线?空白?逗号?