为了账号安全,请及时绑定邮箱和手机立即绑定

如何在读取文件时使用 if 语句?

如何在读取文件时使用 if 语句?

互换的青春 2021-08-25 16:34:33
我对编程几乎是全新的。我正在上我的第二个大学课程。我花了几个小时在网上和在这里寻找帮助。我必须按年份对工资进行排序并将它们加在一起。我想我可以做一个 if 语句来将薪水分类到不同的数组中,但它似乎不起作用。正如你在代码输出中看到的,它显示了无效的年份,但它也弹出年份是 2014 年。我正在阅读的文件遵循这种格式:2014 Employee Smith,John 2000 0(这是一行和那里有 9 个更喜欢它)。感谢您的任何帮助和建议。public class JohnSummersProject1 {public static void main(String[] args) {    int count = 0;    String[] year = new String[10];    String[][] employeeType = new String[10][2];    String[][] name = new String [10][2];    String[][] monthlySalary = new String [10][2];    String[][] micellanious = new String[10][2];    String line = "";    String splitBy = " ";    BufferedReader inputStream = null;    String[] input = new String[5];    String year2014 = "2014";    try {        String x = (args[0]);        inputStream = new BufferedReader(new FileReader(x));        while ((line = inputStream.readLine()) != null && line.length()!=0) {            input = line.split(splitBy);            year[count] = input[0];            employeeType[count][0] = input[1];            if(year[count] == year2014){            name[count][0] = input[2];            monthlySalary[count][0] = input[3];            micellanious[count][0] = input[4];            System.out.print(year[count] + " ");            System.out.print(employeeType[count][0] + " ");            System.out.print(name[count][0] + " ");            System.out.print(monthlySalary[count][0] + " ");            System.out.println(micellanious[count][0]);            }        }    } 
查看完整描述

1 回答

?
DIEA

TA贡献1820条经验 获得超2个赞

您需要使用方法来比较字符串,您不能使用 == 来比较它们

改变

if(year[count] == "2015")

if(year[count].equalsIgnoreCase("2015"))

和其他基于此示例的 if 语句


查看完整回答
反对 回复 2021-08-25
  • 1 回答
  • 0 关注
  • 139 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信