猿问

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

我对编程几乎是全新的。我正在上我的第二个大学课程。我花了几个小时在网上和在这里寻找帮助。我必须按年份对工资进行排序并将它们加在一起。我想我可以做一个 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]);

            }

        }

    } 



互换的青春
浏览 140回答 1
1回答

DIEA

您需要使用方法来比较字符串,您不能使用 == 来比较它们改变if(year[count] == "2015")到if(year[count].equalsIgnoreCase("2015"))和其他基于此示例的 if 语句
随时随地看视频慕课网APP

相关分类

Java
我要回答