String data="";
System.out.println();
BufferedReader input = new BufferedReader(new FileReader("data.txt"));
String items[]=new String [9];
boolean match = false;
int count =0;
while(data!=null)
{
data=input.readLine();
if (data==null)
break;
items=data.split(",");
if(items[6].equalsIgnoreCase(items[2] ))
{
match=true;
count++;
break;
}
}
if (match==true)
{
System.out.println(" There are " +count+ " females are there in Year 3");
}
else if (match==false)
{
System.out.println("There are not females in Year 3");
}
}
3年级有多少女性?这就是我要做的事情我是文件处理的新手,所以我真的不知道我将如何将“f”与第3年进行比较。“Belows是我老师制作的一些假数据
Amath,Brandon,522442,M,2H1,2D2,S2,970721,2893660440, ,AWQ2O0ECHC2D0ECHV2O0BENG2D0DFSF2D0AGLC2O0BMPM2D0APPL2OMBSNC2D0C
Amber,Joelle,568328,F,4X1,4Q2,S4,950606,9055342231, ,AVI4M0AMCV4U0BMDM4U0BMHF4U0CSBI4U0CSCH4U0A
Amin,Emily,235357,F,BC, ,S1,980416,6478662666, ,CHC2D0EENG2D0FICS2O0BMPM2D0BSNC2D0CTDJ2O0ATEJ2O0ATGJ2O0A
Amin,Oliver,547733,M,4X1,4F2,S4,950130,6471307561, ,BBB4M0ABOH4M0ACLN4U0BCPW4U0BENG4U0FHHS4M0AMDM4U0A
Ananthan,Kamala,437254,F,2D1,3M2,S3,960817,2892548340, ,CLU3E0AENG2P0BENG3C0DGPP3O0AMBF3C0DMFM2P0APAF3OMAPPL3OMB
Anders,Trinidad,674842,F,4M1,4H2,S4,950622,6477645846, ,ENG4U0CMCV4U0BMHF4U0ASBI4U0BSCH4U0CSPH3U0ASPH4U0B
Anderson,Estell,148811,F,1H1,1A2,S1,980317,2895001864, ,ADA1O0BBTT1O0ACGC1D0GENG1D0BFSF1D0DMPM1D0CPAI1OFASNC1D0D
Anderson,Hui,215777,M,4I1,4J2,S4,950715,6470662050, ,BAT4M0ABBB4M0BENG4C0CHZT4U0AMAP4C0BTPJ4C0A
Andresen,Kali,557484,M,4X1,4B2,S4,950826,9053465826, ,GLN4OCDGLC2OCDGLN4O0DHRT3M0AMAP4C0CSBI3C0B
Andropopogiopanatakis,Jann,386122,M,4Q1,2H2,S4,950512,6475802413, ,AVI3M0AAVI4M0ABMI3C0BENG4E0AGLE4O0BMEL4E0AOLC4OIFPPL4OMC
Angus,Rusty,486415,M,4L1,4X4,S4,950321,9051102066, ,BBB4M0ACGR4M0BCHI4U0AENG4C0HHFA4M0AMAP4C0C
Antunes,Patty,286127,F,3F1,4G2,S3,960920,9054824084, ,AVI2O7ABDI3CICENG4U0GHNB4O0AHNC3O0BHPC3OICHSP3M0BMBF3C0D
Aponte,Lacresha,221322,F,1A1,1H2,S1,980109,6476014868, ,AVI1O0ABTT1O0CCGC1D0CENG1D0DFSF1D0DMPM1D0GPAI1OFASNC1D0A
Aramburu,My,585147,F,4D1,4X4,S4,950707,9053410243, ,ENG3U0CHFA4M0BHHG4M0A
Arayaz,Merlin,148728,F,4C1,4F2,S4,950422,6478578314, ,CGR4M0ACHI4U0ACHY4U0BCPW4U0BENG4U0AEWC4U0AMAP4C0EMBF3C0B
Archer,Berna,571327,F,4D1,4L2,S4,950508,9055572668, ,GLN4OCEENG4U0DGLC2OCEGLN4O0EHFA4M0BHHG4M0A
Ashwell,Julee,844262,F,4X1,2E2,S2,970319,9052868453, ,AWQ2O0CCHC2D0DHFN2O0FMPM2D0D
Attrell,ShirlEy,153363,F,3A1,3F2,S4,950106,2891743101, ,AWQ2O0DBOG4E0ABTA3O0AENG4C0FHIR3C0AMEL3EIESBI3C0C
Ayers,Arlena,166658,M,3A1 ,3F2 ,S3,960428,6477376427, ,CHC2D0EENG2D0FICS2O0BMPM2D0BSNC2D0CTDJ2O0ATEJ2O0ATGJ2O0A
答案 0 :(得分:0)
试试这个
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class ReadFile {
public static void main(String[] args) throws IOException {
BufferedReader input = new BufferedReader(new FileReader("data.txt"));
String data = input.readLine();
int sexIndex = 3;
int yearIndex = 6;
String femaleSex = "F";
String year = "S3";
int count = 0;
while (data != null) {
String[] chunks = data.split(",");
if (chunks.length >= 6) { // to avoid IndexOutOfBoundException for invalid input
if (chunks[sexIndex].equalsIgnoreCase(femaleSex) && chunks[yearIndex].equalsIgnoreCase(year)) {
count++;
}
}
data = input.readLine();
}
if (count > 0)
{
System.out.println(" There are " +count+ " females are there in Year 3");
}
else
{
System.out.println("There are not females in Year 3");
}
}
}
输出
There are 2 females are there in Year 3
答案 1 :(得分:0)
根据评论中的讨论,if语句的正确条件应为
if(items[3].equalsIgnoreCase("F") && items[6].equalsIgnoreCase("S3") ) {
即,您希望在条件
时执行某些操作都是真的。
下一步
{
match=true;
count++;
break;
}
这里,break指示代码突破周围的循环,即while
循环。删除该中断,并且porgram将实际读取文件中的所有行。
答案 2 :(得分:0)
要明确你的意思是计算第3年的女学生吗? 如果是这样的想法(自从分配以来没有给出确切的代码)是: 阅读每一行,考虑使用更清洁的版本:
while((data=input.readline())!=null){}
在数组中拆分行和存储令牌 检查性别标记是否为女性以及年份是否等于3 如果是这样,增加你的数量 为所有行做这个 打印结果并退出