简体   繁体   English

从1个文件中读取以生成2个文件

[英]Read from 1 file to produce 2 files

I've been trying to do this problem for my schoolwork and for the life of me I cannot figure it out. 我一直在努力为我的学业和我的生活做这个问题,我无法理解。 The problem is: Write a program that reads in "worked_example_1/babynames.txt" and produces two files, boynames.txt and girlnames.txt, separating the data for the boys and girls. 问题是:编写一个读入“working_example_1 / babynames.txt”的程序,并生成两个文件boynames.txt和girlnames.txt,分隔男孩和女孩的数据。

This is the code from Worked Example 11 from wiley.com/go/javaexamples for this problem: 这是来自wiley.com/go/javaexamples的Worked Example 11中的代码,用于解决此问题:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class BabyNames
{
public static final double LIMIT = 50;

public static void main(String[] args) throws FileNotFoundException
{
    Scanner in = new Scanner(new File("babynames.txt"));

    RecordReader boys = new RecordReader(LIMIT);
    RecordReader girls = new RecordReader(LIMIT);

    while (boys.hasMore() || girls.hasMore())
    {
        int rank = in.nextInt();
        System.out.print(rank + " ");
        boys.process(in);
        girls.process(in);
        System.out.println();
    }

    in.close();
}
}

And this: 和这个:

import java.util.Scanner;

public class RecordReader
{
private double total;
private double limit;

public RecordReader(double aLimit)
{
    total = 0;
    limit = aLimit;
}

public void process(Scanner in)
{
    String name = in.next();
    int count = in.nextInt();
    double percent = in.nextDouble();

    if (total < limit)
    {
        System.out.print(name + " ");
    }

    total = total + percent;
}

public boolean hasMore()
{
    return total < limit;
}
}

I created the babynames.txt and put in the same src folder as the .java file, it comes up with file not found, if I give the direct path to the file with 我创建了babynames.txt并放入与.java文件相同的src文件夹,如果我给出了文件的直接路径,它会找到找不到的文件

Scanner in = new Scanner(new File("C:/Users/me/SkyDrive/Schoolwork/CIS150AB/Chapter 11.1/src/babynames.txt"));

it finds the file but gives me this error: 它找到该文件,但给我这个错误:

Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at BabyNames.main(BabyNames.java:20)

To me it looks like this error is because of Line 20 where int rank = in.nextInt(); 对我来说,看起来这个错误是因为第20行,其中int rank = in.nextInt(); and its looking for a string when its looking for an int, but this is directly from Wiley website so I am not sure. 当它寻找一个int时它寻找一个字符串,但这是直接来自Wiley网站所以我不确定。

Any help with this would be appreciated, apparently I am not meant for online classes for Java. 任何有关这方面的帮助将不胜感激,显然我不适合Java的在线课程。 Next class I am taking in school once there is one that works around my schedule. 下一堂课,一旦有一个按照我的日程安排工作,我就上学了。

If this is the format of your babynames.txt, this is what'll happen (I think, didn't try it). 如果是你的babynames.txt的格式,这就是会发生的事情(我想,没试过)。

  • The scanner "in" reads the first int into "rank" (OK) 扫描仪“in”将第一个int读入“rank”(OK)
  • Enter into boys.process() 进入boys.process()
    • "in" reads the first name (Jacob) “in”读取名字(雅各布)
    • "in" tries to read the first int (1), but finds 1.0013, which is not an int “in”尝试读取第一个int(1),但找到1.0013,这不是int

This scenario doesn't match your error (you'd get a stacktrace from RecordReader rather than from BabyNames) but maybe the line of thinking will work for you. 这种情况与您的错误不匹配(您从RecordReader而不是BabyNames获得堆栈跟踪)但是思路可能对您有用。

Try it with a debugger. 尝试使用调试器。 If you can't, use in.next() rather than in.nextInt() or in.nextDouble(). 如果不能,请使用in.next()而不是in.nextInt()或in.nextDouble()。 You can than print the value and see why it's not what you think it is. 您可以打印该值,看看为什么它不是您认为的那样。

Your question actually raises two questions. 你的问题实际上提出了两个问题。 The answer to the hidden one may be: 隐藏的答案可能是:

try (InputStream inputStream = BabyNames.class.getResourceAsStream("/babynames.txt");
     Scanner in = new Scanner(inputStream))
{
    // do stuff with in
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM