Pocięcie Stringa, aby można było stworzyć obiekt - split

0

Witam,
Mam Stringa i chce z niego utworzyć w tym przypadku akurat 3 obiekty typu Autor, konstruktor klasy autor przyjmuje imię i nazwisko. Mam nadzieję, że kod i obrazek bardziej rozjaśni co próbuje zrobić.

 
    public Set<Author> allAuthorsForArticle(String authors){
        Set<Author> lOfAuthors = new HashSet<Author>();
        String tempOfAuthors[] = authors.split(",");
        List<String[]> eachSeparately = new ArrayList<String[]>();
        
        System.out.println("Tekst do ciecia: " + authors); //Tekst do ciecia: A Einstein, B Podolsky, N Rosen 
        
        for(int i=0; i<tempOfAuthors.length; i++){
          eachSeparately.add(tempOfAuthors[i].split("\\s+")); 
        }
        for(String[] s : eachSeparately){
            for(String author: s){
                System.out.print(author);
                //lOfAuthors.add(new Author(name, lastname)); Potrzebuje moc zrobić takie cos ze Stringa
            }
            System.out.println();
        }
//        for(Author a: lOfAuthors){
//            System.out.println("Test22: " + a.getName() + " " + a.getLastname());
//        }
        return null;
    }

Wynik jego działania:
ciecie.PNG

0

Mógłbyś bardziej wyjaśnić w czym masz problem i jaki rezultat chciałbyś uzyskać?
Bo próbuję zrozumieć to co napisałeś, ale kiepsko mi to idzie :)

0

String var = "A Einstein, B Podolsky, N Rosen";

Teraz mamy klasę Autor, a w niej konstruktor public Author(String name, String lastname).
Z tego tekstu chce, zrobić coś takiego:
Author a1 = new Author("A", "Einstein");
Author a2 = new Author("B", Podolsky");
Author a3 = new Author("N, "Rosen");
coś takiego chce uzyskać.

 
    public Set<Author> allAuthorsForArticle(String authors){
        Set<Author> lOfAuthors = new HashSet<Author>();
        String tempOfAuthors[] = authors.split(",");
        List<String[]> eachSeparately = new ArrayList<String[]>();
        
        System.out.println("Tekst do ciecia: " + authors); //Tekst do ciecia: A Einstein, B Podolsky, N Rosen 
        
        for(int i=0; i<tempOfAuthors.length; i++){
          System.out.println("A: " + tempOfAuthors[i]); 
          String line[] = tempOfAuthors[i].split("\\s+");
          lOfAuthors.add(new Author(line[0], line[1]));
        }
       
        for(Author a: lOfAuthors){
            System.out.println("Test22: " + a.getName() + "  " + a.getLastname());
        }
        return null;
    }

dasda.PNG
Powinno wypisać N Rossen itp, ale nie gdzieś ucina.

1
String var = "A Einstein,B Podolsky,N Rosen";
    String[] arr = var. split(",", -1);
    Set<Author> set = new HashSet<Author>();
    for (String str : arr) {
      String[] s = str.split(" ", - 1);
      set.add(new Author(s[0], s[1]));
    }
0

Po dodaniu trim(), działa jak należy.
//roznica
String var = "A Einstein, B Podolsky, N Rosen";
String var1 = "A Einstein,B Podolsky,N Rosen";

 
    public Set<Author> allAuthorsForArticle(String authors) {
	    String[] arr = authors.split(",", -1);
	    Set<Author> setOfAuthors = new HashSet<Author>();
	    for (String str : arr) {
	      String[] s = str.trim().split(" ", - 1);
	      setOfAuthors.add(new Author(s[0], s[1]));
	    }
	    for(Author a: setOfAuthors){
	    	System.out.println(a.getName() + " " + a.getLastname());
	    }
        return null;
    }

1 użytkowników online, w tym zalogowanych: 0, gości: 1