java - 逐行阅读字母
问题描述
我现在有这段代码可以读取 txt 文档的行。我正在尝试添加一些代码,以便程序只打印以字母 C 和 A 开头的文本行。有人可以帮我吗?
import java.io.*;
import java.util.*;
public class FileTester
{
public static void main(String args[]) throws IOException
{
Scanner sf = new Scanner(new File("amazinggrace.txt"));
int maxIndx = -1;
String text[] = new String[1000];
while(sf.hasNext( ))
{
maxIndx++;
text[maxIndx] = sf.nextLine( ) ;
}
sf.close( );
{
System.out.println( text[j] );
}
}
}
解决方案
您可以使用String.startsWith()
while 循环内部的方法来检查该行是否以特定字符/字符开头,然后System.out.println()
像稍后在代码中那样使用来打印该行。
您可以在此处找到文档String.startsWith()
:https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#startsWith(java.lang.String)
推荐阅读
- memory-management - 如果所有内存都直接映射到内核虚拟地址,用户虚拟地址如何访问物理内存
- regex - 检查单元格是否包含来自另一个单元格的关键字 - Google 表格
- c# - 奇怪的缩放问题
- c# - 数据已发送后服务器不发送数据
- sql-server - 如果是上半月或下半月,则返回函数
- ruby-on-rails - Rails ActionMailer
- amazon-web-services - 有什么方法可以使用 AWS Lambda 将本地存储照片/视频上传到 s3 存储桶中
- javascript - 当使用 rxjs 6 在 angular 6 中有新请求时如何取消正在进行的 HTTP 请求
- php - Laravel 首先或创建重复项
- node.js - Node.js 上的多个服务器