我的代码有什么问题总是打印答案:null + word,什么是null?

时间:2018-02-07 22:16:52

标签: java arrays performance file javahelp

我们有几个单词的文件,尝试用单词安全单词在数组中有2,4,6或8个字母但是然后保存在屏幕中写入null和null +好词。

我写错了什么,为什么它显示为空?

public static void lyginis () throws IOException {
    Path path = Paths.get("words.txt");
    Scanner scanner = new Scanner(path);
    int kiek = 0;
    while (scanner.hasNext()) {
        scanner.next();
        kiek++;
    }
    Scanner scanner1 = new Scanner(path);
    String[] atrinkti = new String[kiek];
    String scan = "";
    for (int i = 0; i < kiek; i++) {
        scan = scanner1.next();
        if (scan.length() % 2 == 0) {
            atrinkti[i] += scan ;
        }
        System.out.println(atrinkti[i]);
    }
}

1 个答案:

答案 0 :(得分:0)

<style type="text/css">
 body { background: lime !important; }
.logo_section {
    background-color: white;
    padding:0 30px;
}
.content-section {  
    position: relative;
    margin-top: -30px;
    z-index: 9999;


    padding:0 10px;
    background : white;
    border-color : black;        
    border-width: 5 px;
    border-style: solid;
}

.import-notes { 
    background : yellow;        
    margin-left: -10px;
    margin-right: -10px;
}
    .wrapper{
        display: flex;
        justify-content: space-between;
        margin-bottom: 50px;
    }
    </style>   

输出

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
    <div class="wrapper">
        <div class="logo_section">
              <img class="img-responsive" src="https://i.stack.imgur.com/ymxaR.png">
            </div>

            <nav class="navbar navbar-default">
             <div class="navbar-header">
              <a class="navbar-brand" href="#">XYZ123</a>
             </div>
            </nav>
        </div>
    </div>
    
<div class="container">

 <div class="content-section">
  <p >(1)This is a test this is a test. This is a test this is a test.This is a test this is a test.This is a test this is a test.This is a test this is a test.This is a test this is a test.This is a test this is a test.This is a test this is a test.This is a test this is a test.This is a test this is a test.This is a test this is a test.This is a test this is a test.This is a test this is a test.This is a test this is a test.This is a test this is a test.This is a test this is a test.This is a test this is a test.This is a test this is a test.This is a test this is a test.This is a test this is a test.This is a test this is a test.This is a test this is a test.This is a test this is a test.This is a test this is a test.This is a test this is a test.This is a test this is a test.This is a test this is a test.This is a test this is a test.This is a test this is a test.This is a test this is a test.</p>
  <p >(2)This is a test </p>
  <p >(3)This is a test </p>
  <p >(4)This is a test </p>
  <p >(5)This is a test </p>
  <p >(6)This is a test </p>
  <p >(7)This is a test </p>
  <p >(8)This is a test </p>
  <p >(12)This is a test </p>
  <p class="import-notes">This is full width no padding</p>
  </div>
 </div>

问题是:

  1. import java.io.File; import java.io.IOException; import java.util.Scanner; public class Hello { public static void main(String[] args) throws IOException { File file = new File("words.txt"); Scanner scanner = new Scanner(file); int kiek = 0; while (scanner.hasNext()) { scanner.next(); kiek++; } Scanner scanner2 = new Scanner(file); String[] atrinkti = new String[kiek]; String word = ""; for (int i = 0; i < kiek; i++) { word = scanner2.next(); if (word.length() % 2 == 0) { atrinkti[i] = word; System.out.println(atrinkti[i]); } } } } 代替$ cat words.txt hi hello whats up chicken duck goose $ javac Hello.java; java Hello hi up duck
  2. Path声明中使用File而非+=
  3. if语句之外调用=函数,因此当单词的长度不能被2整除时,当前数组元素将打印{{1}数组的默认初始化值}