This is the text file and i want to read the text file and store the names age and address into an object. I am having Problem with the program, Sorry m new to java
name : name1 age : 20 address : kokrajhar
I expect the result as: StudenInfo[name= name1, age= 20, address=kokrajhar]
StudentInfo.java
public class StudenInfo{
private static String name;
private static Integer age;
private static String address;
public String getName() {
return name;
}
public static void setName(String name) {
StudenInfo.name = name;
}
public static Integer getAge() {
return age;
}
public static void setAge(Integer age) {
StudenInfo.age = age;
}
public static String getAddress() {
return address;
}
public static void setAddress(String address) {
StudenInfo.address = address;
}
@Override
public String toString() {
return "StudenInfo [name=" + name + ", age=" +age +", address=" + address +"]";
}
}
Test.java
class test {
public static void main(String[] args) throws Exception{
File f1 = new File("names.txt");
Scanner scanner = new Scanner(f1);
String nextLine= scanner.nextLine();
String[] stuinfo= nextLine.split(":");
scanner.close();
String strname=stuinfo[0];
String strage= stuinfo[1];
String straddress= stuinfo[2];
StudenInfo students=new StudenInfo();
students.setName(strname);
int intage = Integer.parseInt(strage);
students.setAge(intage);
students.setAddress(straddress);
System.out.println(students.toString());
}