0

first I have three classes:

Data.java:

public class Data {
    
    public static List<String> studentdataget(){
        List<String> studentData = new ArrayList<String>();
        // Format:
        // Name ; Number ; Subject
        
        studentData.add("Quee;810283;MathI");
        studentData.add("Hor;812227;Math I");
        studentData.add("Oper;810369;Math II");
        studentData.add("Graf;811090;MathI");
        studentData.add("Ingri;811911;MathI");

Student.java:

public class Student {
    
    private static String name = "";
    private static int number = 0;
    private static String subject = ""; 
    

    public Student(String name, int number, String subject) {
        this.name = name;
        this.number = number;
        this.subject = subject;
    }
    
    
    public static String getnumber(String segs) {
        return number;
    }
    
    public static  int getnumber(String segs) {
        return number;
    }

    public static String getName() {
        return name;
    }

    public void setsubject(String subject) {
        this.subject = subject;
    }

    public void setnumber(int number) {
        this.number = number;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String toString() {
        String returnString = name + " (" + number + "), Subject: " + subject;      
        return returnString;
    }
}

main.java:

public class main {

        public static void main(String[] args) {
            String path = "Name;number;subject";
            String[] segs = path.split( Pattern.quote( ";" ) );
            //System.out.println( (segs[2]));
            
            HashMap<String, String> studentData = new HashMap<String, String>();
            
            studentData.put(segs[1],segs[2]);
            
            
            for (Map.Entry<String, String> pair: studentData.entrySet()) {
                  System.out.format("key: %s, value: %s\n", pair.getKey(), pair.getValue());
                }
            for(Map.Entry<String, String> pair: studentData.entrySet()) {
                
                
            }
        }
                
       }

So the question is how can I put in my second for loop the list from my Data.class? I want to display only the number and the subject is this possible ? I have try so many ways and no one is getting me to the solution .. I hope someone can give me a solution

maksimov
  • 5,792
  • 1
  • 30
  • 38
  • I'm guessing you need to traverse your `studentData` *list* from `Data.java` and for each of its elements you need to do the string split (why did you call it `path`?). Not sure what you are doing with that map there in the `main` (btw the Java convention is for all class names to start with a capital letter) – maksimov Nov 19 '20 at 22:35
  • I`m not sure what you want to say to me :) .. I don´t know how I can fill them up. So what I don't know is how to access data from another class? – basic_talent Nov 19 '20 at 22:37
  • So your `studentdataget` from `Data` is declared `public` which means any other class can access it, and also `static` which means you don't need to have an instance of `Data` to use this method (this is a very bad usage pattern for a usecase like this, but ok for learning). So what you can do in `main` is this: `List data = Data.studentdataget();` and work with this list. – maksimov Nov 19 '20 at 22:43
  • Thanks that helped me :) – basic_talent Nov 19 '20 at 22:45
  • Yes, I understand this now. – basic_talent Nov 19 '20 at 23:03

1 Answers1

1

To access the studentData method from another class, you can use this notation Data.studentdataget(); since studentdataget() is static.

You just split() on ; in each entry of yout list to split them into [Quee, 810283, MathI]. Therefore you can use the last two indexes to get the desired data.

This assumes all the data in studentData remains uniform.

public static void main(String[] args) {
    List<String> students = Data.studentdataget(); // access Data here

    HashMap<String, String> classes = new HashMap<>();

    // fill our map from the data found in Data.studentdataget
    for (String student: students) {
        String[] arr = student.split(";"); // split string into array on each ;
        classes.put(arr[1], arr[2]); // fill map based on student data set
    }

    // print the map
    for (Map.Entry<String, String> entry : classes.entrySet()) {
        System.out.println(entry.getKey() + ":" + entry.getValue());
    }
}
cela
  • 2,352
  • 3
  • 21
  • 43