Need help on getting highest salary and second highest salary from emp.csv using java - arraylist

I have one csv file which has 3 entries of empname,emp id,designation and salary
Robert,33,Manager,12000
Duval,23,Associate,6000
Kierron,33,AD,20000
Using Java program, I like to achieve the highest salary and second highest salary
public class HighSalary {
public static void highSalary() throws IOException {
String record;
BufferedReader br = new BufferedReader(new FileReader("/Users/ak/Documents/emp.csv"));
System.out.println("\t\t Max Salary Record\n");
System.out.println(" ------------------------------------------------------------- ");
System.out.println("| Name Age Desig Salary |");
System.out.println(" ------------------------------------------------------------- ");
List<List<String>> arlist = new ArrayList<>();
// List<String> list = Arrays.asList(a);
int maxSal = 0;
while ((record = br.readLine()) != null) {
String[] words = record.split(",");
arlist.add(Arrays.asList(words));
for (int i = 0; i < words.length; i++) {
if (maxSal <= Integer.parseInt(words[3])) {
maxSal = Integer.parseInt(words[3]);
} else {
maxSal = maxSal;
}
}
System.out.println("From The Salary Largest Number is:" + maxSal);
}
br.close();
}
}
the problem with this code is printing like this as below:
From The Salary Largest Number is:12000
From The Salary Largest Number is:12000
From The Salary Largest Number is:20000

You need to keep track of two separate variables, one for the highest salary, and the other for the second highest salary. When assigning the highest salary, the previous highest salary should be clobbering the second highest. When assigning a new second highest, only the previous second highest should be overwritten.
Integer highest = null;
Integer second = null;
while ((record = br.readLine()) != null) {
String[] words = record.split(",");
arlist.add(Arrays.asList(words));
int salary = Integer.parseInt(words[3]);
if (highest == null) {
highest = salary;
second = salary;
}
else if (salary > highest) {
second = highest;
highest = salary;
}
else if (salary > second) {
second = salary;
}
}
System.out.println("highest salary: " + highest);
System.out.println("second highest salary: " + second);

System.out.println("From The Salary Largest Number is:" + maxSal); Keep this outside while loop.

Related

Array list not obtaining smallest age

I've setup 2 different array lists that coincide and I have code to find the biggest age and smallest age, However my youngest age isn't outputting. I am getting the biggest age as outputted
import java.util.Scanner;
import java.util.ArrayList;
public class Database1{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
ArrayList<Integer> age = new ArrayList<>();
ArrayList<String> name = new ArrayList<>();
int count = 0;
while (count < 10) {
System.out.println("Please enter a first name");
String inputName = in.nextLine();
if(inputName.contains("done")){
break;
}
name.add(inputName);
System.out.println("Please enter an age");
age.add(in.nextInt());
in.nextLine();
count++;
}
int smallest = age.get(0);
int biggest = age.get(0);
String youngestName = "" ;
String oldestName = "";
for (int i = 0; i < age.size(); i++) {
if(age.get(i) > biggest){
biggest = age.get(i);
oldestName = name.get(i);
}if (age.get(i) < smallest){
smallest = age.get(i);
youngestName = name.get(i);
}
}
System.out.println("The oldest person was " + oldestName);
System.out.println("The youngest person was " + youngestName);
System.out.println(name);
System.out.println(age);
}
}
I am receiving this as an output
The oldest person was john
The youngest person was
[Luke, Chris, Neal, john]
[18, 19, 44, 66]
Modify to the following:
public static void main(String[] args){
Scanner in = new Scanner(System.in);
ArrayList<Integer> age = new ArrayList<>();
ArrayList<String> name = new ArrayList<>();
int count = 0;
while (count < 10) {
System.out.println("Please enter a first name");
String inputName = in.nextLine();
if(inputName.contains("done")){
break;
}
name.add(inputName);
System.out.println("Please enter an age");
age.add(in.nextInt());
in.nextLine();
count++;
}
int smallest = age.get(0);
int biggest = age.get(0);
String youngestName = name.get(0); ;
String oldestName = name.get(0);
for (int i = 0; i < age.size(); i++) {
if(age.get(i) > biggest){
biggest = age.get(i);
oldestName = name.get(i);
} else if (age.get(i) < smallest){
smallest = age.get(i);
youngestName = name.get(i);
}
}
System.out.println("The oldest person was " + oldestName);
System.out.println("The youngest person was " + youngestName);
System.out.println(name);
System.out.println(age);
}

I would like add GPA in my code but unfortunately not working

I would ask if someone can help me the GPA in my code not working as per required.please find below it.
Scanner Student=new Scanner(System.in);
System.out.println("Please Enter Three Marks for Student");
int sum=0;
double avg;
int[]x=new int[3];
for (int i = 0; i < x.length; i++) {
System.out.println("The mark : "+ ( i +1));
x[i]=Student.nextInt();
sum=sum+x[i];
}
char grade;
if (sum >= 90 )
{
grade = 'A';
}
else if (sum >= 80 )
{
grade = 'B';
}
else if (sum >= 70)
{
grade = 'C';
}
else if (sum >= 50)
{
grade = 'D';
}
else
{
grade = 'F';
}
// Display the grade.
System.out.println("The grade is " + grade);
avg=sum/x.length;
System.out.println("The total of three marks are :"+sum+"\nThe Average is :"+avg);
}

how to Print out the employee details having highest and second highest salary?

csv file which has 3 entries of empname,emp id,designation and salary Robert,33,Manager,12000 Duval,23,Associate,6000 Kierron,33,AD,20000
From below code how to Print out the employee details having the highest and second highest salary?
enter code here
public class HighSalary {
public static void highSalary() throws IOException {
String record;
BufferedReader br = new BufferedReader(new FileReader("/Users/ak/Documents/emp.csv"));
System.out.println("\t\t Printing Employee Details of Highest & Second Highest Salary \n");
System.out.println(" ------------------------------------------------------------- ");
System.out.println("| ID Name Age Address |");
System.out.println(" ------------------------------------------------------------- ");
List<List<String>> arlist = new ArrayList<>();
int highestSal = 0;
int secondSal = 0;
while ((record = br.readLine()) != null) {
String[] words = record.split(",");
arlist.add(Arrays.asList(words));
int salary = Integer.parseInt(words[3]);
if (highestSal == 0) {
highestSal = salary;
secondSal = salary;
}
else if (salary > highestSal) {
secondSal = highestSal;
highestSal = salary;
}
else if (salary > secondSal || secondSal == 0) {
secondSal = salary;
}
}
System.out.println("highest salary: " + highestSal);
System.out.println("second highest salary: " + secondSal);
br.close();
}
}
enter code here
You may iterate the file, keeping track of the highest and second highest salary. When assigning a new highest salary, the second highest should be assigned to the previous highest salary. Also, you may store two strings for the metadata associated with the highest and second highest paid employees.
Integer highest = null;
Integer second = null;
String highEmp = "";
String secondEmp = "";
while ((record = br.readLine()) != null) {
String[] words = record.split(",");
arlist.add(Arrays.asList(words));
int salary = Integer.parseInt(words[3]);
if (highest == null) {
highest = salary;
second = salary;
highEmp = record;
secondEmp = record;
}
else if (salary > highest) {
second = highest;
secondEmp = highEmp;
highest = salary;
highEmp = record;
}
else if (salary > second) {
second = salary;
secondEmp = record;
}
}
System.out.println("highest salary: " + highest);
String[] parts = highEmp.split(",");
System.out.println("Name: " + parts[0] + ", Age: " + parts[1] + ", Desig: " + parts[2]);
System.out.println("second highest salary: " + second);
parts = secondEmp.split(",");
System.out.println("Name: " + parts[0] + ", Age: " + parts[1] + ", Desig: " + parts[2]);

Matching partial words in two different columns

I am working on trying to weed out a certain customer from our database. I've noticed a trend where people fill out their first name with the same name that is partial to how they fill out their company name. So an example would look like:
business_name first_name
------------- ----------
locksmith taylorsville locksmith
locksmith roy locksmi
locksmith clinton locks
locksmith farmington locksmith
These are people I do not want being pulled in a query. They are bad eggs. I'm trying to put together a query with a WHERE statement (presumably) that isolates anyone who has a first name that contains at least a partial match to their business name, but I'm stumped and could use some help.
You can use LIKE operator:
SELECT * FROM table WHERE business_name NOT LIKE CONCAT(first_name, '%')
% stands for anything.
You can employ similarity based approach
Try code at bottom of answer
It produces result like below
business_name partial_business_name first_name similarity
locksmith taylorsville locksmith locksmith 1.0
locksmith farmington locksmith locksmith 1.0
locksmith roy locksmith locksmi 0.7777777777777778
locksmith clinton locksmith locks 0.5555555555555556
So, you will be able to control what to filter out based on similarity value
** Code **
SELECT business_name, partial_business_name, first_name, similarity FROM
JS( // input table
(
SELECT business_name, REGEXP_EXTRACT(business_name, r'^(\w+)') AS partial_business_name, first_name AS first_name FROM
(SELECT 'locksmith taylorsville' AS business_name, 'locksmith' AS first_name),
(SELECT 'locksmith roy' AS business_name, 'locksmi' AS first_name),
(SELECT 'locksmith clinton' AS business_name, 'locks' AS first_name),
(SELECT 'locksmith farmington' AS business_name, 'locksmith' AS first_name),
) ,
// input columns
business_name, partial_business_name, first_name,
// output schema
"[{name: 'business_name', type:'string'},
{name: 'partial_business_name', type:'string'},
{name: 'first_name', type:'string'},
{name: 'similarity', type:'float'}]
",
// function
"function(r, emit) {
var _extend = function(dst) {
var sources = Array.prototype.slice.call(arguments, 1);
for (var i=0; i<sources.length; ++i) {
var src = sources[i];
for (var p in src) {
if (src.hasOwnProperty(p)) dst[p] = src[p];
}
}
return dst;
};
var Levenshtein = {
/**
* Calculate levenshtein distance of the two strings.
*
* #param str1 String the first string.
* #param str2 String the second string.
* #return Integer the levenshtein distance (0 and above).
*/
get: function(str1, str2) {
// base cases
if (str1 === str2) return 0;
if (str1.length === 0) return str2.length;
if (str2.length === 0) return str1.length;
// two rows
var prevRow = new Array(str2.length + 1),
curCol, nextCol, i, j, tmp;
// initialise previous row
for (i=0; i<prevRow.length; ++i) {
prevRow[i] = i;
}
// calculate current row distance from previous row
for (i=0; i<str1.length; ++i) {
nextCol = i + 1;
for (j=0; j<str2.length; ++j) {
curCol = nextCol;
// substution
nextCol = prevRow[j] + ( (str1.charAt(i) === str2.charAt(j)) ? 0 : 1 );
// insertion
tmp = curCol + 1;
if (nextCol > tmp) {
nextCol = tmp;
}
// deletion
tmp = prevRow[j + 1] + 1;
if (nextCol > tmp) {
nextCol = tmp;
}
// copy current col value into previous (in preparation for next iteration)
prevRow[j] = curCol;
}
// copy last col value into previous (in preparation for next iteration)
prevRow[j] = nextCol;
}
return nextCol;
}
};
var the_partial_business_name;
try {
the_partial_business_name = decodeURI(r.partial_business_name).toLowerCase();
} catch (ex) {
the_partial_business_name = r.partial_business_name.toLowerCase();
}
try {
the_first_name = decodeURI(r.first_name).toLowerCase();
} catch (ex) {
the_first_name = r.first_name.toLowerCase();
}
emit({business_name: r.business_name, partial_business_name: the_partial_business_name, first_name: the_first_name,
similarity: 1 - Levenshtein.get(the_partial_business_name, the_first_name) / the_partial_business_name.length});
}"
)
ORDER BY similarity DESC
Was used in How to perform trigram operations in Google BigQuery? and based on https://storage.googleapis.com/thomaspark-sandbox/udf-examples/pataky.js by #thomaspark where Levenshtein's distance is used to measure similarity
this will do the trick,
select * from TableName where lower(business_name) contains lower(first_name)
use lower() just in case they have upper case letters. Hope it helps.

Extracting order type from sql parser

using global sql parser (gsp) for extracting column and sorting type from order sql query and extract and or from where condition
SELECT employee_id, dept, name, age, salary
FROM employee_info
WHERE dept = 'Sales' and ID=1
ORDER BY salary, age DESC,ID;
I can extracting column name but can extract order type
1- how can extract order type?
2- how can extract and , or from where sql?
If pSqlstmt is gsp_selectStatement * then you can do something like this:
if(pSqlstmt->orderbyClause != nullptr)
{
string sortType;
int colNumOrderBy = pSqlstmt->orderbyClause->items->length;
for(int i = 0; i < colNumOrderBy; i++)
{
gsp_orderByItem *field = reinterpret_cast<gsp_orderByItem *>(gsp_list_celldata(pSqlstmt->orderbyClause->items->head));
//get order by column name
char *sortCol = gsp_node_text(reinterpret_cast<gsp_node*>(field->sortKey));
if(field->sortToken== nullptr)
{
//ERROR
}
else
{
//find out sorting type (ASC/DESC)
sortType = sortType.substr(0,field->sortToken->nStrLen);
}
free(sortCol);
pSqlstmt->orderbyClause->items->head = pSqlstmt->orderbyClause->items->head->nextCell;
}
}