How can I use an if statement to while reading from a file? [duplicate]

How can I use an if statement to while reading from a file? [duplicate]



This question already has an answer here:



I am almost brand new to programming. I am in my second college course. I have spent hours looking online and on here looking for help. I have to sort the salaries by year and add them together. I thought I could make an if statement to sort the salaries into different arrays, but it does not appear to be working. As you can see by the code output, it shows invalid year, but it also pops up that the year is 2014. The file that I am reading from follows this format:
2014 Employee Smith,John 2000 0 (this is one line and there are 9 more like it).
Thanks for any help and advice.


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



This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.





year[count] == "2015" is not how you compare Strings in Java, use String#equals
– MadProgrammer
Sep 2 at 2:04


year[count] == "2015"


String


String#equals




1 Answer
1



You need to use method for comparing strings, you can't compare them using ==



Change


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



to


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



and other if statements based on this example

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

How do I collapse sections of code in Visual Studio Code for Windows?

Node.js puppeteer - Use values from array in a loop to cycle through pages