简体   繁体   English

Java-通过从文本文件中选择某些数字来创建字符串

[英]Java - Creating a string by selecting certain numbers from within a text file

I have a .txt file that has numbers 1-31 in it called numbers . 我有一个.txt文件,其中包含数字1-31,称为numbers I've set up a scanner like this: 我已经设置了这样的扫描仪:

    cardCreator = new Scanner (new File ("numbers"));

After this, I'm kind of confused on which methods I could use. 在此之后,我对可以使用的方法感到困惑。

I want to set up a conditional statement that will go through numbers, evaluate each line (or numbers 1-31), and include each number a string called numbersonCard , if they meet certain criteria. 我想建立一个条件语句,该条件语句将遍历numbers,评估每一行(或数字1-31),并在每个数字中包含一个称为numbersonCard的字符串(如果它们满足某些条件)。

I can kind of picture how this would work (maybe using hasNext() , or nextLine() or something), but I'm still a little overwhelmed by the API... nextLine()一下它是如何工作的(也许使用hasNext()nextLine()东西),但是我仍然对API有点不知所措...

Any suggestions as to which methods I might use? 关于我可能使用哪种方法的任何建议?

Computer science is all about decomposition. 计算机科学是关于分解的。 Break your problem up into smaller ones. 将您的问题分解成较小的问题。

First things first: can you read that file and echo it back? 首先,您可以读取该文件并将其回显吗?

Next, can you break each line into its number pieces? 接下来,您可以将每一行分解成数字吗?

Then, can you process each number piece as you wish? 然后,您可以根据需要处理每个数字吗?

You'll make faster progress if you can break the problem down. 如果可以解决问题,则可以更快地取得进展。

I think Java Almanac is one of the best sources of example snippets around. 我认为Java Almanac是周围示例代码的最佳来源之一。 Maybe it can help you with some of the basics. 也许它可以帮助您了解一些基础知识。

Building upon duffymo's excellent advice, you might want to break your problem down into high-level pseudocode. 在duffymo的出色建议的基础上,您可能希望将问题分解为高级伪代码。 The great thing about this is, you can actually write these steps as comments in your code, and then solve each bit as a separate problem. 这样做的好处是,您实际上可以将这些步骤作为注释写在代码中,然后将每个问题作为一个单独的问题来解决。 Better still, if you break down the problem right, you can actually place each piece into a separate method. 更好的是,如果您正确地解决了问题,则实际上可以将每个部分放入单独的方法中。

For example: 例如:

// Open the file
// Read in all the numbers
// For each number, does it meet our criteria?
// If so, add it to the list

Now you can address each part of the problem in a somewhat isolated fashion. 现在,您可以以某种孤立的方式解决问题的每个部分。 Furthermore, you can start to see that you might break this down into methods which can open the file (and deal with any errors thrown by the API), read in all the numbers from the file, determine whether a given number meets your criteria, etc. A little clever method naming, and your code will literally read like the pseudocode, making it more maintainable in the future. 此外,您开始发现可以将其分解为一些方法,这些方法可以打开文件(并处理API引发的任何错误),从文件中读取所有数字,确定给定数字是否符合您的条件,等等。一个稍微聪明的方法命名,您的代码将像伪代码一样从字面上读取,从而使其在将来更易于维护。

To give a little more specific help than the excellent answers duffymo and Rob have given, your instincts are right. 要提供比duffymo和Rob给出的出色答案还要具体的帮助,您的直觉是正确的。 You probably want something like this: 您可能想要这样的东西:

cardCreator = new Scanner (new File ("numbers"));
while (cardCreator.hasNextInt()) {
    int number = cardCreator.nextInt();
    // do something with number
}
cardCreator.close();

hasNext() and nextInt() will save you from getting a String from the Scanner and having to parse it yourself. hasNext()和nextInt()可以避免您从扫描程序获取字符串,而不必自己解析它。 I'm not sure if Scanner's default delimiter will interpret a Windows end-of-line CRLF to be one or two delimiters, though. 我不确定Scanner的默认定界符是否会将Windows行尾CRLF解释为一个或两个定界符。

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

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