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

StringTokenizer Class | Calculating Total Salary From Salary And Bonus Txt Files Using StringTokenizer Class
Calculating Total Salary From Salary And Bonus Txt Files Using StringTokenizer ClassHello readers in this article you are going to learn how to calculate total salary from 2 text file i.e. salary.txt and bonus.txt files using StringTokenizer class in Java. Both salary.txt and bonus.txt are on local-host server so we need to use URL class to interact with these two files and we will use BufferedReader  class to read lines present in files.

Files : salary.txt and bonus.txt
Salary.txt contains.

bonus.txt contains.

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.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="";
URL salaryurl=new URL(salary_url_path);
InputStream salaryin=salaryurl.openStream();
BufferedReader br=new BufferedReader(new InputStreamReader(salaryin));
StringTokenizer token=new StringTokenizer(line, "=");
salname=token.nextToken();           salary=Integer.parseInt(token.nextToken());
salarymap.put(salname, salary);

String bonusname=null;
Integer bonus=0;
String bonusline=null;
Map<String,Integer> bonusymap=new HashMap<>();

String bonus_url_path="";
URL bonusurl=new URL(bonus_url_path);
InputStream bonusin=bonusurl.openStream();
BufferedReader bonusbr=new BufferedReader(new

StringTokenizer bonustoken=new StringTokenizer(bonusline, "=");
bonusymap.put(bonusname, bonus);
//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++)
totalsalary= salary+ bonus;
System.out.println(salname+" Total Salary: "+totalsalary );
}//end of try 
catch (MalformedURLException e)
catch (IOException e)
    }//end of main

}//end of class

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

You May Like Also

Programming Knowledge

Java JDK
Reversing String in Java
Java Generic Concept With Simple Example
Java Generic Objects Behavior In Non-Generic Area
Creating Connection With MySql Databse Using Java Application
Performing Database Operations Java MySql Insert Update Delete Select
Java Inserting Data in MySql Databse Table
Java Basic Login System Using MySql Database Table
JSP Login System Using MySql Databse
Jsp Servlet Login And Logout System Using Mysql Database Table
What is jdk in Java?
Exaplain inheritance in Java with example?
Exaplain polymorphism in Java with example?
Exaplain abstraction in Java with example?
Exaplain encapsulation in Java with example?
What is the use of this keyword in Java?
Explain ArrayList With Example
Explain LinkedList With Example
Explain Vector Class With Example
Explain Vector Class With Example
Generics in java with example
Behavior of generic objects
How can i pass generic class to a method in java
How to create a login system in jsp using mysql database?
How to create a login and logout system in jsp & servlet using mysql database?
How to connection a simple Java application with mysql database?
How to insert values in mysql databse usiong Java applications?
Perform insertion, retrieval, updatation and delete operations?
How to create a simple login system in java using mysql database?

PHP Login System Using MySql Database
PHP Complete Login System With Session And Logout Using MySql Database