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"
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
year[count] == "2015"is not how you compareStrings in Java, useString#equals– MadProgrammer
Sep 2 at 2:04