gpt4 book ai didi

java - 如何从文件中的一行中分别读取整数和字符串并将它们存储到变量中?

转载 作者:行者123 更新时间:2023-12-01 17:42:49 24 4
gpt4 key购买 nike

我有一个看起来像这样的文本文件:

1 Song one
2 Song two
3 Song three
...

如何逐行读取并从一行中分别提取数字和字符串,假设将它们打印到控制台:

String title; // extracted from current line
int num; // extracted from current line
System.out.println("Number: " + num + "Title: " + title);

最佳答案

How do I read a line by line

使用 BufferedReader及其 readLine()方法。

How do I extract number and String separately

使用line.indexOf(' ')然后substring(...)获得 2 个零件。

然后使用 Integer.parseInt(...)在第一部分获取号码。

关于java - 如何从文件中的一行中分别读取整数和字符串并将它们存储到变量中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60920849/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com