从两个单独的文本文件中读取并写入一个

时间:2018-03-05 15:58:36

标签: java

需要将女孩名字和男孩名字的两个文本文件合并到一个文本文件中。新文件必须将男孩名字和女孩名字分成两个性别列表,我们不知道每个文件有多少名字。程序运行但陷入无限循环

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.PrintWriter;
import java.util.Scanner;

public class NameTester 
{
    public static void main(String args[]) throws FileNotFoundException
    {
        Scanner userIn = new Scanner(System.in);
        System.out.println("Enter file name for boys name file: ");
        String boyFile = userIn.next();
        System.out.println("Enter file name for girls name file: ");
        String girlFile = userIn.next();
        userIn.close();

        Scanner boyIn = new Scanner(new FileReader(boyFile));
        Scanner girlIn = new Scanner(new FileReader(girlFile));

        PrintWriter out = new PrintWriter("names.txt");
        out.print("Boys Names:                  Girls Names: ");
        int count = 1;
        while(boyIn.hasNextLine() || girlIn.hasNextLine());
        {
            String b = boyIn.next();
            String g = girlIn.next();
            out.print(count + " " + b + "                   " + count + " " + g);
            count++;
        }

        boyIn.close();
        girlIn.close();
        out.close();
   }
}

1 个答案:

答案 0 :(得分:2)

这一行是一个永远运行的空while(boyIn.hasNextLine() || girlIn.hasNextLine()); 循环:

while(boyIn.hasNextLine() || girlIn.hasNextLine()) // <- NO SEMICOLON
{
    ....
}

最后摆脱分号:

function (user, context, callback) {
    if (context.connection !== 'linkedin') {
        callback(null, user, context);
    }

    var request = require('request');
    var options = {
        url: 'https://api.linkedin.com/v1/people/~/picture-urls::(original)?format=json',
        headers: {
            Authorization: 'Bearer ' + user.identities[0].access_token
        }
    };

    request(options, function (error, response) {
        if (!error && response.statusCode === 200) {
            var json = JSON.parse(response.body);
            if (json !== null && json.values && json.values.length >= 1) {
                context.idToken.picture = json.values[0];
            }
        }
        callback(null, user, context);
    });
}

我还没有检查过程序中的其他逻辑错误,但这应该摆脱无限循环。