无法检查lambda表达式中的int数组

时间:2017-05-01 21:27:57

标签: java arrays if-statement lambda

我尝试检查score[0]

  

如果得分[0]为1,pos + 1

     

如果得分[0]为0,则neu + 1

     

如果得分[0]为-1,则为负+ 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 
});

更新

我尝试删除变量启动中的final

  

从lambda表达式引用的局部变量必须是最终的或有效的最终

示例代码

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

目的

我试图逐行读取txt文件。

然后,我使用外部库将每一行转换为代码(-1,0,1)

每行中的代码存储在得分[0]

我创建了pos neu neg来分别计算-1,0,1-1/0/1

更重要的是,我面临的问题是检查pos,neu,neg的一部分。其他部分应该没问题。

最后,翻译的代码将输出到新的txt,包括public static void main (String args[]) throws UnsupportedEncodingException, IOException { final int[] score = { 0 }; int post = 0; final int neu = 0; final int neg = 0; try (Stream < String > stream = Files.lines(Paths.get("e:\\ust.txt")).sequential()) { //retrieve txt file File file = new File("e:\\gg.txt"); //output file FileOutputStream fos = new FileOutputStream(file); PrintStream ps = new PrintStream(fos); System.setOut(ps); final Analysis analysis = new Analysis(); stream.forEach(line -> { ps.println("Code: " + analysis.parse(line).getCode()); //show current line's code score[0] = score[0] + analysis.parse(line).getCode(); //add up total codes if(analysis.parse(line).getCode()==1){ post ++;} // check current codes=>line with problems else if (analysis.parse(line).getCode()==0) {neu++;}// check current codes=>line with problems else{ neg++; } //check current codes=> line with problems }); System.out.println("score=" + score[0]); //print out the total score System.out.println("postive=" + post); System.out.println("neu=" + neu); System.out.println("neg=" + neg); } } 的值。

最初的代码:

<aside class="sidebar-left-collapse">

    <a href="#" class="company-logo">Logo</a>

    <div class="sidebar-links">

        <div class="link-yellow selected">
            <a href="katalog/camgurubu.html">
                KAPAK
            </a>
        </div>

        <div class="link-blue">
            <a href="katalog/cam2.html">
                CAM GURUBU
            </a>
            <ul class="sub-links">
                <li><a href="#">Paşabahçe</a></li>
            </ul>
        </div>

        <div class="link-red">
            <a href="#">
                PORSELEN GURUBU
            </a>
            <ul class="sub-links">
                <li><a href="#">KÜTAHYA PORSELEN</a></li>
                <li><a href="#">BONNA PORSELEN</a></li>
            </ul>
        </div>

        <div class="link-yellow">
            <a href="#">
                ÇATAL/KAŞIK/BIÇAK
            </a>
            <ul class="sub-links">
                <li><a href="#">ARYILDIZ ÇATAL/KAŞIK/BIÇAK</a></li>
                <li><a href="#">ŞEHZADE ÇATAL/KAŞIK/BIÇAK</a></li>
                <li><a href="#">ALMOND ÇATAL/KAŞIK/BIÇAK</a></li>
                <li><a href="#">NARİN ÇATAL/KAŞIK/BIÇAK</a></li>
            </ul>
        </div>

        <div class="link-green">

            <a href="#">
                MUTFAK SET ÜSTÜ MALZEMELERİ
            </a>

            <ul class="sub-links">
                <li><a href="#">DOLLY PLASTİK ÜRÜNLER</a></li>
                <li><a href="#">PİRGE BIÇAKLARI</a></li>
                <li><a href="#">VİCTORİNOX BIÇAKLARI</a></li>
                <li><a href="#">İTHAL MALZEME SET ÜSTÜ GRUBU</a></li>
                <li><a href="#">BORA SAKLAMA KABI VE DİĞER MALZEMELER</a></li>
                <li><a href="#">TÜRKAY POLİETİLEN GRUPLARI</a></li>
                <li><a href="#">CAMBRO ÜRÜNLERİ</a></li>
                <li><a href="#">PLASTİK,POLİKARBON MALZEMELER</a></li>
                <li><a href="#">KÜLSAN (THERMOSET MALZEMELER)</a></li>
                <li><a href="#">NARİN SET ÜSTÜ MALZEMELERİ</a></li>
                <li><a href="#">ARYILDIZ SET ÜSTÜ MALZEMELER</a></li>
                <li><a href="#">ALTINBAŞAK TEFLON TAVALAR,FAJİTA GRUPLARI, SET ÜSTÜ ÜRÜNLER</a></li>
                <li><a href="#">TEL KEVGİR, SÜZGEÇ, ÇIRPICI ÜRÜNLER GURUBU</a></li>
                <li><a href="#">KEPÇE, KEVGİR, SPATULA, MAŞA  SERVİS GRUPLARI</a></li>
                <li><a href="#">TRİBECA ÜRÜN GRUPLARI</a></li>
                <li><a href="#">ALKAN-ZİCCO ÜRÜN GRUPLARI</a></li>
                <li><a href="#">ÇÖP KOVALARI</a></li>
                <li><a href="#">TENCERELER</a></li>
                <li><a href="#">KAÇAROLA/KÜVETLER</a></li>
                <li><a href="#">DERİ ÜRÜNLERİ VE MENÜLER</a></li>
                <li><a href="#">PİZZA TAVALARI</a></li>
                <li><a href="#">FIRIN KÜREKLERİ</a></li>

            </ul>

        </div>
        <div class="link-green">

            <a href="#">
                AÇIK BÜFE EKİPMANLARI
            </a>

            <ul class="sub-links">
                <li><a href="#">İTHAL AÇIK BÜFE MALZEMELERİ</a></li>
                <li><a href="#">CHAFİNG DISH</a></li>
                <li><a href="#">BORA AÇIK BÜFE ÜRÜNLER</a></li>
                <li><a href="#">ALKAN-ZİCCO AÇIK BÜFE ÜRÜN GRUPLARI</a></li>
                <li><a href="#">GÜREN METAL AÇIK BÜFE ÜRÜNLERİ</a></li>
                <li><a href="#">KÜLSAN AÇIK BÜFE ÜRÜNLER</a></li>

            </ul>

        </div>
        <div class="link-green">

            <a href="#">
                PASTAHANE EKİPMANLARI
            </a>

            <ul class="sub-links">
                <li><a href="#">ARYILDIZ PASTAHANE ÜRÜNLERİ</a></li>
                <li><a href="#">PASTA,KEK KALIPLARI ALÜMİNYUM MALZEMELER</a></li>
            </ul>

        </div>
        <div class="link-green">

            <a href="#">
                GENEL MALZEME GRUBU
            </a>

            <ul class="sub-links">
                <li><a href="#">PLASTİK TAŞIMA EKİPMANLARI VE AKSESUARLARI</a></li>
                <li><a href="#">KAHVE MAKİNALARI </a></li>
                <li><a href="#">AHŞAP SERVİS EKİPMANLARI</a></li>
            </ul>

        </div>
        <div class="link-green">

            <a href="#">
                CİHAZLAR
            </a>

            <ul class="sub-links">
                <li>
                    <div class="link-green">
                        <a href="#">
                            HAZIRLIK
                        </a>
                        <ul class="sub-links">
                <li><a href="#"> Arda </a></li>
                <li><a href="#"> Zaman </a></li>
                            <li><a href="#"> Deneme</a></li>
                        </ul>

                    </div>
                </li>
                <li>
                    <div class="link-green">
                        <a href="#">
                            KAFETERYA CİHAZLARI
                        </a>
            <ul class="sub-links">
                <li><a href="#"> Arda </a></li>
                <li><a href="#"> Zaman </a></li>
                            <li><a href="#"> Deneme</a></li>
                        </ul>

                    </div>
                </li>
                <li>
                    <div class="link-green">
                        <a href="#">
                            BULAŞIKHANE
                        </a>
                        <ul class="sub-links">
                            <li><a href="#"></a></li>
                        </ul>

                    </div>
                </li>
                <li>
                    <div class="link-green">
                        <a href="#">
                            PİŞİRME GURUBU 600 SERİ
                        </a>
                        <ul class="sub-links">
                            <li><a href="#"></a></li>
                        </ul>

                    </div>
                </li>
                <li>
                    <div class="link-green">
                        <a href="#">
                            PİŞİRME GURUBU 700 SERİ
                        </a>
                        <ul class="sub-links">
                            <li><a href="#"></a></li>
                        </ul>

                    </div>
                </li>
                <li>
                    <div class="link-green">
                        <a href="#">
                            PİŞİRME GURUBU 900 SERİ
                        </a>
                        <ul class="sub-links">
                            <li><a href="#"></a></li>
                        </ul>

                    </div>
                </li>
                <li>
                    <div class="link-green">
                        <a href="#">
                            KONVEKSİYONLU FIRINLAR
                        </a>
                        <ul class="sub-links">
                            <li><a href="#"></a></li>
                        </ul>

                    </div>
                </li>
                <li>
                    <div class="link-green">
                        <a href="#">
                            SOĞUTMA EKİPMANLARI
                        </a>
                        <ul class="sub-links">
                            <li><a href="#"></a></li>
                        </ul>

                    </div>
                </li>
                <li>
                    <div class="link-green">
                        <a href="#">
                            TEŞHİR ÜRÜNLERİ
                        </a>
                        <ul class="sub-links">
                            <li><a href="#"></a></li>
                        </ul>

                    </div>
                </li>
                <li>
                    <div class="link-green">
                        <a href="#">
                            RAF ÜRÜNLERİ
                        </a>
                        <ul class="sub-links">
                            <li><a href="#"></a></li>
                        </ul>

                    </div>
                </li>
                <li>
                    <div class="link-green">
                        <a href="#">
                            TEZGAH/DAVLUMBAZ/YER SÜZGEÇLERİ
                        </a>
                        <ul class="sub-links">
                            <li><a href="#"></a></li>
                        </ul>

                    </div>
                </li>
                <li>
                    <div class="link-green">
                        <a href="#">
                            ÇAMAŞIRHANE GRUBU
                        </a>
                        <ul class="sub-links">
                            <li><a href="#"></a></li>
                        </ul>

                    </div>
                </li>
                <li>
                    <div class="link-green">
                        <a href="#">
                            İTHAL ÜRÜNLER
                        </a>
                        <ul class="sub-links">
                            <li><a href="#"></a></li>
                        </ul>

                    </div>
                </li>
            </ul>

        </div>
    </div>

</aside>

<div class="main-content">

    <div class="menu">

        <section id="content">

        </section>

    </div>

</div>

3 个答案:

答案 0 :(得分:1)

如果我理解正确,你想修改lambda中的方法变量,但Java不允许(变量必须是final或者有效的最终)。

我过去使用的解决方案是使用AtomicInteger。在你的情况下:

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

    final int[] score = { 0 };
    final AtomicInteger post = new AtomicInteger(0); // this object will be final, but its contents can be modified inside the lambda

    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.incrementAndGet(); }
        });

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

答案 1 :(得分:1)

您可以通过分组和计数来计算流中不同代码的出现次数,如下所示:

Collector<Integer, ?, Map<Integer, Long>> counter = 
    Collectors.groupingBy(
        Function.identity(), 
        TreeMap::new,
        Collectors.counting());
Map<Integer, Long> countByCode = lines
    .map(l -> analysis.parse(l).getCode())
    .collect(counter);
System.out.println(countByCode);

答案 2 :(得分:1)

如果要使用Stream API解决问题,则应停止使用forEach搜索解决方案。 Stream还有很多其他方法可供选择。如果它们都没有帮助达到你的目标,你应该考虑保持一个普通的循环。

但您也可以阅读package documentationthe tutorial,找出摆脱循环/ forEach思维的方法。

public static void main(String args[]) throws IOException {
    long pos, neu, neg;
    try(FileOutputStream fos = new FileOutputStream("e:\\out.txt");
        PrintStream ps = new PrintStream(fos)) {

        try(Stream<String> stream = Files.lines(Paths.get("e:\\in.txt"))) {
            Map<Integer, Long> m = stream
                .map(line -> analysis.parse(line).getCode())
                .collect(Collectors.groupingBy(Function.identity(),Collectors.counting()));

            pos = m.get(+1);
            neu = m.get( 0);
            neg = m.get(-1);
        }
        ps.println("pos="+pos);
        ps.println("neu="+neu);
        ps.println("neg="+neg);
    }
}