lundi 1 mai 2017

Unable to check int array inside a lambda expression

I tried to check score[0] :

if score[0] is 1 , pos+1

if score[0] is 0 ,neu+1

if score[0] is -1, neg+1

 final int[] score = {    0};
    final  int post =     0;
        final int neu =    0;
            final int neg =    0;
stream.forEach(line -> {


           ps.println("Code: " + analysis.parse(line).getCode()); 
    score[0] = score[0] + analysis.parse(line).getCode(); 

    if(score[0]==1){ post ++;}   //problem line
    else if (score[0]==0) {neu++;}  //problem line 
    else{ neg++; }  //problem line 


    });

update

I tried to remove final in the variable initiation

local variable referenced from a lambda expression must be final or effectively final

sample code

  public static void main (String args[]) throws UnsupportedEncodingException, IOException {

   final int[] score = {    0};
      int post =     0;


try (Stream < String > stream = Files.lines(Paths.get("e:\\in.txt")).sequential()) { //retrieve txt file
    File file = new File("e:\\out.txt"); //output file
    FileOutputStream fos = new FileOutputStream(file);
    PrintStream ps = new PrintStream(fos);
    System.setOut(ps);

    stream.forEach(line -> {



    if(score[0]==1){ post ++;}



    });


       System.out.println("postive=" + post); 

}
    }

Aucun commentaire:

Enregistrer un commentaire