[英]How to add the bonus?
I have done a code, which reads a file consists a number of employees, salary, and their rankings, based on their rankings how can we add the bonus percent to their salary... 我做了一个代码,它读取一个文件,包括一些员工,工资和他们的排名,根据他们的排名,我们如何将奖金百分比添加到他们的薪水......
String phrases;
int salary=0;
try {
FileReader in = new FileReader("bonus.txt");
BufferedReader readFile = new BufferedReader(in);
while ((phrases = readFile.readLine()) != null) {
System.out.println(phrases);
double bonus;
if(phrases.contains("1")){
bonus=salary/0.03;
System.out.println("Bonus: " + bonus);
}else if(phrases.contains("2")){
bonus=salary/0.08;
System.out.println("Bonus: " + bonus);
}else if(phrases.contains("3")){
bonus=salary/0.20;
System.out.println("Bonus: " + bonus);
}
// System.out.println();
}
readFile.close();
in.close();
}catch (IOException e) {
System.out.println("Problem reading file.");
System.err.println("IOException: " + e.getMessage());
}
It outputs: 它输出:
Jame 900000 1
Bonus: 0.0
Jane 60000 2
Bonus: 0.0
Don 866000 3
Bonus: 0.0
I have no idea why 我不知道为什么
You check the condition with equals method but your phrases variable contains different value rather than 1,2,3 that's why you get the bonus 0. 您使用equals方法检查条件,但您的短语变量包含不同的值而不是1,2,3,这就是您获得奖金0的原因。
if(phrases.contains("1")){
bonus=salary/0.03;
}else if(phrases.contains("2")){
bonus=salary/0.08;
}else if(phrases.contains("3")){
bonus=salary/0.20;
}
or you can get the last parameter with: 或者你可以得到最后一个参数:
phrases.substring(phrases.length()-1, phrases.length())
you can get the third parameter using contains or split method. 你可以使用contains或split方法获得第三个参数。 Please check this tutorial: https://www.tutorialspoint.com/java/java_string_split.htm 请查看本教程: https : //www.tutorialspoint.com/java/java_string_split.htm
And one more thing your salary is always zero (0) . 还有一件事你的薪水总是零(0) 。 please correct it 请纠正它
I have posted full code here: 我在这里发布了完整代码:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
class SubClass{
public static void main(String[] args) {
String phrases;
int salary=0;
try {
FileReader in = new FileReader("bonus.txt");
BufferedReader readFile = new BufferedReader(in);
while ((phrases = readFile.readLine()) != null) {
System.out.println(phrases);
phrases = phrases.trim().replaceAll("[ ]{2,}", " ");
String splitStr [] = phrases.split(" ");
double bonus;
salary = Integer.parseInt(splitStr[1]);
if(splitStr[2].contains("1")){
bonus=salary/0.03;
System.out.println("Bonus: " + bonus);
}else if(splitStr[2].contains("2")){
bonus=salary/0.08;
System.out.println("Bonus: " + bonus);
}else if(splitStr[2].contains("3")){
bonus=salary/0.20;
System.out.println("Bonus: " + bonus);
}
// System.out.println();
}
readFile.close();
in.close();
}catch (IOException e) {
System.out.println("Problem reading file.");
System.err.println("IOException: " + e.getMessage());
}
}
}
The other answers appear to not cater for the fact that your salary
variable is always 0
, thus, your bonus
calculation, which depends on your salary value will always be 0. 其他答案似乎不能满足您的salary
变量始终为0
的事实,因此,您的bonus
计算( 取决于您的工资值)将始终为0。
Assuming that this: Jame 900000 1
is a sample line from your text file, there are various issues with your code. 假设: Jame 900000 1
是文本文件中的示例行,您的代码存在各种问题。
(phrases.equals("1")
. If phrase will be equal to the text in the current line you are processing: Jame 900000 1
, this statement (and the same for the other two) will never return true, thus the bonus will never be calculated. 第一个问题是: (phrases.equals("1")
。如果短语将等于您正在处理的当前行中的文本: Jame 900000 1
,此语句(和其他两个相同)将永远不会返回是的,因此永远不会计算奖金。 salary
value. 第二个问题是你永远不会提取salary
价值。 You will need to replace this: 你需要替换它:
while ((phrases = readFile.readLine()) != null) {
System.out.println(phrases);
if(phrases.equals("1")){
With something like this: 有这样的事情:
while ((phrases = readFile.readLine()) != null) {
System.out.println(phrases);
String[] employeeData = phrases.split("\\t"); //This assumes that your data is split by tabs.
salary = Double.parse(employeeData[1]);
if("1".equals(employeeData[2])) {
bonus = salary * 0.03;
}
...
If you have an employeeBonus.txt file like below. 如果你有一个employeeBonus.txt文件,如下所示。
Jame 900000 2
Jane 60000 1
Don 866000 3
I think you will have three tokens as a string so, you can use a stringtokenizer class in order to get a salary and a grade. 我认为你将有三个标记作为字符串,所以你可以使用stringtokenizer类来获得薪水和成绩。
At the first line of file is 在文件的第一行是
Jame 900000 2
and the result of encoded string was 并且编码字符串的结果是
Jame%20%20%20%20900000%092
I've finally found the content of text file was mixed with a space and tab character by URL encoding. 我终于发现文本文件的内容通过URL编码与空格和制表符混合在一起。
So, the usage of this type is as follows, 所以,这种类型的用法如下,
StringTokenizer stTok = new StringTokenizer(phrase, " \t");
It takes a salary and an identifier of bonus value from third and second token. 它需要工资和第三和第二令牌的奖金值标识符。
name = stTok.nextToken(); //first token
salary = Integer.valueOf(stTok.nextToken()).intValue(); //second token
grade = stTok.nextToken();
[source code] [源代码]
package com.tobee.tests.inout;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.StringTokenizer;
public class CheckBounsFromFile {
public static void main(String[] args) {
String name, phrase, grade;
double bonus = 0;
int salary = 0;
BufferedReader readFile = null;
try {
readFile = new BufferedReader(new FileReader("resource/aa/employeeBonus.txt"));
while ((phrase = readFile.readLine()) != null) {
//System.out.println(phrase);
StringTokenizer stTok = new StringTokenizer(phrase, " \t");
name = stTok.nextToken();
salary = Integer.valueOf(stTok.nextToken()).intValue();
grade = stTok.nextToken();
if(grade!= null && !grade.equals(""))
{
if (grade.equals("1")) {
bonus = salary / 0.03;
} else if (grade.equals("2")) {
bonus = salary / 0.08;
} else if (grade.equals("3")) {
bonus = salary / 0.20;
}
System.out.printf("name[%s]salary[%d]Bonus[%f] \n",name, salary, bonus);
}
}
} catch (IOException e) {
System.out.println("Problem reading file.");
System.err.println("IOException: " + e.getMessage());
}
finally
{
try {
readFile.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
[result] [结果]
name[Jame]salary[900000]Bonus[30000000.000000]
name[Jane]salary[60000]Bonus[750000.000000]
name[Don]salary[866000]Bonus[4330000.000000]
Have a nice day. 祝你今天愉快。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.