Reputation: 492
I am testing my college class and when the user types in "add" it adds a new student to the arraylist in the college class.
Scanner myInput=new Scanner (System.in);
String information=myInput.next(); //I know this is incorrect not sure what else to do
directory.addStudent(information);
here the directory is the object of the arraylist containing students. the addStudent method is in the College class, and looks like this:
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
}
anyways my question seems simple but can't figure it out. I want to know how to split the information so that it can satisfy the parameters in addStudent method...As you can see information will be a String but I want studentID to be a long not a string what can I do?
Upvotes: 0
Views: 3756
Reputation: 1181
You can use split()
method to break your information
What you have to do is
String str = new String(information);
String[] s = str.split(" ", 3);
i am splitting on the space basis , second argument 3 means you have to break into 3 strings i.e name,id,address
after this you can get name via s[0] , id via s[1] and address via s[2]
so you can pass easily over addStudent()
method . but you need to convert the values according to the arguments of addStudent()
method
Upvotes: 0
Reputation: 14707
Try this:
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;
}
}
Upvotes: 0
Reputation: 3870
Splitting the string after reading is not necessary. You can read delimited string using Scanner.
I assume your inputs are separated by whitespace. If so, you need to do this:
String name = myInput.next();
long id = myInput.nextLong();
String address = myInput.next();
dictionary.add(name, id, address);
Upvotes: 2