Java 在txt文件中查找整数
我正在尝试从txt文件中读取信息。文本文件的格式如下:Java 在txt文件中查找整数,java,integer,Java,Integer,我正在尝试从txt文件中读取信息。文本文件的格式如下: 1 I like programming. 2 "but I am new at it" 3 -so I need to go 4 on web sites, 5 to get 6 help. 7 I appreciate any help you could give me. 8 Thanks. 我想如下显示它,同时在每个数字之前添加“Chap”,作为章节 Chap 1 I like programming. Chap 2
1 I like programming. 2 "but I am new at it" 3 -so I need to go
4 on web sites, 5 to get 6 help.
7 I appreciate any help you could give me. 8 Thanks.
我想如下显示它,同时在每个数字之前添加“Chap”,作为章节
Chap 1 I like programming.
Chap 2 "but I am new at it"
Chap 3 -so I need to go
Chap 4 on web sites,
Chap 5 to get
Chap 6 help.
Chap 7 I appreciate any help you could give me.
Chap 8 Thanks.
这就是我到目前为止所做的:
public class ReadAFile {
public static void main(String[] args) throws IOException {
// Get bible book names, chapters and verses
File fileIn = new File("t.txt");
Scanner inputFile = new Scanner(fileIn);
while(inputFile.hasNext())
{
// Read all lines from the file.
String line = inputFile.nextLine();
System.out.println(line);
String[] tokens = line.split(" ");
for(int i = 0; i < tokens.length; i++)
{
if(tokens[i].matches("^-?\\d+$")) // I'mm using this to try to find the numbers in the txt file
{
for(int p = 0; p < tokens.length; p++)
{
System.out.println(tokens[p]);
}
}
}
}
inputFile.close();
}
}
公共类可读文件{
公共静态void main(字符串[]args)引发IOException{
//获取圣经书名、章节和经文
File fileIn=新文件(“t.txt”);
扫描仪输入文件=新扫描仪(文件输入);
while(inputFile.hasNext())
{
//读取文件中的所有行。
String line=inputFile.nextLine();
系统输出打印项次(行);
String[]tokens=line.split(“”);
for(int i=0;i
输出不是我想要的,我不知道为什么。我要么把每个单词放在自己的一行,要么把同一个句子的多行上。您是否尝试过将for循环中的
println
切换到print
,然后在打印章节标题时插入新行
这是您自己的代码的一部分,已修改
if(tokens[i].matches("^-?\\d+$")) // I'mm using this to try to find the numbers in the txt file
{
for(int p = 0; p < tokens.length; p++)
{
System.out.print(tokens[p]+ " "); //print
}
System.out.println()//Empty line
}
if(tokens[i].matches(“^-?\\d+$”)//i'mm使用此函数尝试在txt文件中查找数字
{
for(int p=0;p
从1开始,数字是否总是连续的?如果是这样的话,为什么不利用这个优势,把扫描器的分隔符改成数字,然后用计数器代替呢?大概是这样的:
public static void main(String[] args) throws IOException {
// Get bible book names, chapters and verses
File fileIn = new File("t.txt");
Scanner inputFile = new Scanner(fileIn);
inputFile.useDelimiter("\\s*-?\\d+\\s*");
for (int num = 1; inputFile.hasNext(); num++)
{
System.out.println("Chap " + num + " " + inputFile.next());
}
inputFile.close();
}
输出:
Chap 1 I like programming.
Chap 2 "but I am new at it"
Chap 3 -so I need to go
Chap 4 on web sites,
Chap 5 to get
Chap 6 help.
Chap 7 I appreciate any help you could give me.
Chap 8 Thanks.
如果没有,请告诉我,我会做一些事情来解析并保存它:)欢迎使用StackOverflow。我已经编辑了您的答案,删除了序言,并提前表示感谢,因为它们在StackOverflow上被认为是不必要的。我还添加了一个标签,以帮助其他人查找/分类您的问题。如果我对Java的猜测不正确,请相应地修改它。非常感谢。这些数字并不总是连续的。txt文件将是:1 bla 2 bla 3 bla。。。2 bla 2 bla 3 bla 4 bla。。。3布拉2布拉3布拉。。。这会一直持续到下一章,所以听起来它也应该在这一章中寻找诗句?这将需要某种额外的一致性标记来用于章节,而不是作为分隔符的诗句。您将能够使用该分隔符嵌套循环。如果你所有的都是数字,无法区分章节和韵文的数字,那么就没有办法分割或描绘它们。章节号之间可以有一个双行分隔符吗?然后,您可以使用两个扫描仪,外部为“\\n\\n\\s*-?\\d+\\s*”,内部为“\\s*-?\\d+\\s*”,如果从1开始,则仍然计数。