Home > other >  How am i suppose to use my List Java in other method
How am i suppose to use my List Java in other method

Time:04-27

Hello can someone explain why i cant use "moviesList" in other method?

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.JsonParseException;

import java.io.File;
import java.io.IOException;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Parser {

    private Movies mov;

    public Parser(){
    }

    public void mapperParser() throws JsonParseException, JsonMappingException, IOException {
        ObjectMapper mapper = new ObjectMapper();

        List<Movies> moviesList = mapper.readValue(new File("Movies.json"),new TypeReference<List<Movies>>() {});

        Comparator<Movies> cmp = Comparator.comparing(Movies::getName);
        moviesList.sort(cmp);

        Comparator<Movies> cmp1 = Comparator.comparingInt(Movies::getAge);
        moviesList.sort(cmp1);

        Comparator<Movies> cmp2 = Comparator.comparing(Movies::getDirector);
        moviesList.sort(cmp2);
    }

    public void hashMapParser{
        Map<Movies, Movies> moviesHashMap = new HashMap<>();
        for (int i = 0; i < movieslist.size(); i  ){
        }
    }
}

I want to add all values from moviesList to hashMap by algorithm but i cant use moviesList for some reason How should i change my code?

CodePudding user response:

Right now, the variable movieList only exists inside the mapperParser() method. So, its not accessible outside that method. If you want to use it elsewhere, add another class variable like this:

public class Parser {

    private Movies mov;

    private List<Movies> moviesList; // here is the variable

    public Parser(){
    }

    public void mapperParser() throws JsonParseException, JsonMappingException, IOException {
        ObjectMapper mapper = new ObjectMapper();

        this.moviesList = mapper.readValue(new File("Movies.json"),new TypeReference<List<Movies>>() {});

        Comparator<Movies> cmp = Comparator.comparing(Movies::getName);
        moviesList.sort(cmp);

        Comparator<Movies> cmp1 = Comparator.comparingInt(Movies::getAge);
        moviesList.sort(cmp1);

        Comparator<Movies> cmp2 = Comparator.comparing(Movies::getDirector);
        moviesList.sort(cmp2);
    }

    public void hashMapParser{
        Map<Movies, Movies> moviesHashMap = new HashMap<>();
        for (int i = 0; i < this.moviesList.size(); i  ){
            //do stuff here
        }    
    }    
}

CodePudding user response:

moviesList is a local variable that is only accessible from the mapperParser() method. You probably want to make it a field like mov, something like this:

public class Parser {

    private Movies mov;
    private List<Movies> moviesList; 

    ...

    public void mapperParser() throws JsonParseException, JsonMappingException, IOException {
        ...
        moviesList = mapper.readValue(new File("Movies.json"),new TypeReference<List<Movies>>() {});
        ...
    }
    ...
}

The field will be accessible from hashMapParser().

Another point: Java is case-sensitive, the l in moveslist should be an L.

CodePudding user response:

If you define the List inside a method, it's only visible inside that method.

If you want other methods in this class to access the List, instead define it in your class just like you have with "private Movies mov"

You can use your class constructor to initialize it.

  •  Tags:  
  • java
  • Related