-1

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());

}
L.Spillner
  • 1,772
  • 10
  • 19
  • 4
    Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: [How to create a Minimal, Complete, and Verifiable example.](https://stackoverflow.com/help/mcve) – Ben Apr 24 '18 at 07:56
  • 1
    What is not working? – Sheetal Mohan Sharma Apr 24 '18 at 07:59
  • Hey, welcome at Stackoverflow, please read advised links above- [mcve] and [How to ask](https://stackoverflow.com/help/how-to-ask), then feel free to edit your question, thanks.. Eg. - what is the actual output, what is not working, what is the error if you are getting some, etc.. :) – xxxvodnikxxx Apr 24 '18 at 08:01

1 Answers1

0

first of all, as the previous answer said, you shouldn't be using static variables since it will modify the value for all objects and you'll end up with identical values.

second, if you can always count on your information being formatted this way you should use regular expressions since splitting as you're currently doing would only cause more bugs.

here's an example of how you could use it:

     Pattern pattern = Pattern.compile("\\w*\\s:\\s\\w*"); // this will split on groups of information, like name : name1
        List<String> studentInfo = new ArrayList();
        Matcher matcher = pattern.matcher(test);
        while (matcher.find()) {
            studentInfo.add(matcher.group());
        }


studentInfo.get(0).substring(studentInfo.get(0).trim().lastIndexOf(" ")); // this is the name
        studentInfo.get(1).substring(studentInfo.get(1).trim().lastIndexOf(" ")); // this is the age
        studentInfo.get(2).substring(studentInfo.get(2).trim().lastIndexOf(" ")); // this is the address
user1
  • 254
  • 1
  • 15