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

标签 java if-statement

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

最佳答案

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

改变

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

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

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

关于java - 如何在读取文件时使用 if 语句?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52133428/

相关文章:

java - Web 服务和 Log4J 输出到文件

java - 将 xbasej 与 NTX 索引结合使用

java - Hadoop 命令不从 java prcoessbuilder 执行

java - 使用 DisplayTag 时如何自定义 paging.banner.some_items_found

mysql - 如果语句为 1,则运行特定的 SQL 语句

android - if else 语句设置不同的listadaptor

java - 是否可以提前退出构造函数?

java - 默认 switch case 之后的 if 语句 - Java

R: dplyr 管道条件超前/滞后使用 ifelse 具有意外行为

C:只有if语句中的第一个条件有效