如何在从文件中读取时使用if语句? [重复]

问题描述 投票:0回答:1

这个问题在这里已有答案:

我几乎是编程的新手。我在我的第二个大学课程。我花了好几个小时在网上寻找帮助。我必须按年计算工资并将它们加在一起。我以为我可以制作一个if语句来将工资分成不同的数组,但它似乎没有起作用。正如您在代码输出中看到的那样,它显示无效年份,但它也会显示年份是2014年。我正在阅读的文件遵循以下格式:2014员工史密斯,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]);
            }
            else if(year[count] == "2015"){
            employeeType[count][1] = input[1];
            name[count][1] = input[2];
            monthlySalary[count][1] = input[3];
            micellanious[count][1] = input[4];
            System.out.print(year[count] + " ");
            System.out.print(employeeType[count][1] + " ");
            System.out.print(name[count][1] + " ");
            System.out.print(monthlySalary[count][1] + " ");
            System.out.println(micellanious[count][1]);
        }
            else{
                System.out.println("Invalid year");
            }
            count++;

        }
    } 
    catch (IOException io) {
        System.out.println("File IO exception" + io.getMessage());
        System.exit(0);
    } finally {
        try {
            if (inputStream != null) {
                inputStream.close();
            }
        } catch (IOException io) {
            System.out.println("Issue closing the Files" + io.getMessage());
        }
    }
    for(int i = 0; i <year.length; i++){
     System.out.println("Year " + year[i] + " Employee type = " +
       employeeType[i][0] + " Name: " + name[i] + " Monthly count = " +
             monthlySalary[i][0] + "extras: " + micellanious[i][0]);
    }
}

} It shows invalid year, but below it shows that the year is in fact 2014

java if-statement
1个回答
1
投票

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

更改

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

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

以及基于此示例的其他if语句

© www.soinside.com 2019 - 2024. All rights reserved.