StringTokenizer Class | Calculating Total
Salary From Salary And Bonus Txt Files Using StringTokenizer Class

Files : salary.txt
and bonus.txt
Salary.txt contains.
dilshad=1201
matin=2001
abcd=1001
bonus.txt contains.
dilshad=1200
abcd=1000
matin=1300
So let’s begin…
Create a simple Java class with main method and follow the steps,
here I have not used any user defined method all logic and steps are performed
in main method only. See example
import java.io.*;
import java.net.*;
import java.util.*;
public class Salary_Bonus {
public static void main(String[] args){
try {
String salname=null;
Integer salary=0;
String line=null;
Map<String,Integer>
salarymap=new HashMap<>();
String salary_url_path="http://127.0.0.1:81/TxtFiles/salary.txt";
URL salaryurl=new URL(salary_url_path);
InputStream
salaryin=salaryurl.openStream();
BufferedReader
br=new BufferedReader(new InputStreamReader(salaryin));
line=br.readLine();
while(line!=null)
{
StringTokenizer
token=new StringTokenizer(line, "=");
while(token.hasMoreTokens())
{
salname=token.nextToken(); salary=Integer.parseInt(token.nextToken());
salarymap.put(salname, salary);
}
line=br.readLine();
}
br.close();
//=============================================================
String bonusname=null;
Integer bonus=0;
String bonusline=null;
Map<String,Integer>
bonusymap=new HashMap<>();
String bonus_url_path="http://127.0.0.1:81/TxtFiles/bonus.txt";
URL bonusurl=new URL(bonus_url_path);
InputStream
bonusin=bonusurl.openStream();
BufferedReader
bonusbr=new BufferedReader(new
InputStreamReader(bonusin));
bonusline=bonusbr.readLine();
while(bonusline!=null)
{
StringTokenizer
bonustoken=new StringTokenizer(bonusline, "=");
while(bonustoken.hasMoreTokens())
{
bonusname=bonustoken.nextToken();
bonus=Integer.parseInt(bonustoken.nextToken());
bonusymap.put(bonusname, bonus);
}
bonusline=bonusbr.readLine();
}
bonusbr.close();
//just
printing salary and bonus
System.out.println("Salary : "+salarymap);
System.out.println("Bonus : "+bonusymap);
//converting
to Array
String[]
salarykeyset=salarymap.keySet().toArray(new String[salarymap.size()]);
String[]
bonuskeyset=bonusymap.keySet().toArray(new String[bonusymap.size()]);
Integer totalsalary=0;
for(int i=0;i<salarykeyset.length;i++)
{
salary=salarymap.get(salarykeyset[i]);
bonus=bonusymap.get(bonuskeyset[i]);
salname=salarykeyset[i];
totalsalary= salary+ bonus;
System.out.println(salname+" Total
Salary: "+totalsalary );
}
System.out.println("-----------------");
}//end
of try
catch (MalformedURLException e)
{
System.out.println(e.getMessage());
}
catch (IOException e)
{
System.out.println(e.getMessage());
}
}//end of main
}//end of class
OutPut
Salary : {matin=2001, dilshad=1201, abcd=1001}
Bonus : {matin=1300, dilshad=1200, abcd=1000}
matin Total Salary: 3301
dilshad Total Salary: 2401
abcd Total Salary: 2001
-----------------
I know this is worst kind of programming practice but I just
wanted to show you how you can calculate total salary from two different text
file i.e., here in this example salary.txt and bonus.txt.
In this example we used URL class, BufferedReader class, StringTokenizer class and HashMap.
I hope this article will help you. For any suggestion please use comment box.
No comments