java programming language The attached A12txt file which has

java programming language

The attached A12.txt file which has 2 columns with the first column being the name of the employee and the second column holding their salary. Read the data into a 2 dimensional array of size 1,000 rows. Once the data is inside the 2-D array print out the unsorted contents to the screen only of the rows that are currently in-use. Sort the data using selection sort from lowest salary to highest salary ensuring the name continues to match up with the corresponding salary. For only the rows that are currently in-use, print the sorted array to the screen and save the sorted data to an output file named \"Sorted.txt\". Turn in A12.txt, .java file, and Sorted.txt all in 1 zip file into blackboard.

Solution

// Employee.java
import java.io.*;
import java.util.*;
import java.io.PrintWriter;

public class Employee
{

public static String[][] selectionSort(String[][] employeeDetails, int size)
{

for (int i = 0; i < size - 1; i++)
{
//System.out.println( Integer.parseInt(employeeDetails[i][1]));
int index = i;
for (int j = i + 1; j < size; j++)
if ( Integer.parseInt(employeeDetails[j][1]) < Integer.parseInt(employeeDetails[index][1]) )
index = j;
  
String temp = employeeDetails[index][1];
employeeDetails[index][1] = employeeDetails[i][1];
employeeDetails[i][1] = temp;

temp = employeeDetails[index][0];
employeeDetails[index][0] = employeeDetails[i][0];
employeeDetails[i][0] = temp;
}

return employeeDetails;
}


   public static void main(String[] args)
{

File inputFile = new File(\"A12.txt\");
// Check if file exists
       if (!inputFile.exists())
{
           System.out.println(\"File does not exist!\");
           System.exit(1);
       }

String[][] employeeDetails = new String[1000][2];
int size = 0;
BufferedWriter output = null;

try(BufferedReader bf = new BufferedReader(new InputStreamReader(new FileInputStream(inputFile))))
{
String str;
while ((str = bf.readLine()) != null)
{
String[] tokens = str.split(\" \");
employeeDetails[size][0] = tokens[0];
employeeDetails[size][1] = tokens[1];
size++;
}

// output file
File file = new File(\"Sorted.txt\");
output = new BufferedWriter(new FileWriter(file));

output.write(\"Employee details\ Name\\tSalary\ \");
for (int i = 0; i < size ;i++ )
{
output.write(employeeDetails[i][0] + \"\\t\" + employeeDetails[i][1] + \"\ \");
}

employeeDetails = selectionSort(employeeDetails,size);

output.write(\"\ \ Employee details\ Name\\tSalary\ \");
for (int i = 0; i < size ;i++ )
{
output.write(employeeDetails[i][0] + \"\\t\" + employeeDetails[i][1] + \"\ \");
}
output.close();
}
catch (FileNotFoundException exception)
{
System.out.println(\"File not found.\");
exception.printStackTrace();
}
catch (IOException exception)
{
System.out.println(\"IO error.\");
exception.printStackTrace();
}
  

}
}

/*
A12.txt
Ayush 34531
John 2341
Eoin 345321


Sorted.txt
Employee details
Name Salary
Ayush 34531
John 2341
Eoin 345321


Employee details
Name Salary
John 2341
Ayush 34531
Eoin 345321
  
*/

java programming language The attached A12.txt file which has 2 columns with the first column being the name of the employee and the second column holding their
java programming language The attached A12.txt file which has 2 columns with the first column being the name of the employee and the second column holding their

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site