Basic Salary Array Converted to Total Salary Array [closed] - java

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I've created this Array which holds Basic Salary
static String[] BASICSALARY = {"$8096","$7661","$2427","$8467","$9122"," "," "," "};
And I'm willing to calculate the Total Salary in a method and then print the Total Salary Beside the Basic.
private static void CalTotalSalary()
{
System.out.println(" ID\tEmployee Name\t\t Years of Expirence\tBasic Salary\t\tTotal Salary");
int size = ID.length;
for (int i=0; i<size; i++)
{
System.out.println(" " + ID[i] + " " + FN[i] + " " + LN[i] + "\t\t " +
YEARSOFEXP[i] + "\t\t" +BASICSALARY[i] );
}
}
I have thought of simply making an Array and enter the values manually, But in fact, I need to do the Calculation inside the program. Otherwise this's not programming.
Any Suggestions?

First of all I recommend you to use List instead of plain Array. Its more flexibel and useful.
get your basicsalary into a List.
Use List<String> operators =LISTENTRY.split("[0-9]+"); for each entry in your list. Parse them. Then add them together. Simple as that.
I dont want to post plain code, it doesnt help you learn. Just watch some basic List tutorials or read a beginners Javabook to get into it.
Greetings, David

Related

Java Program to add two strings values [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 months ago.
Improve this question
There are Two Strings s1 and s2, then split them and add them with one another.
Please find below snippet for more information,
Input :-
String s1 ="Sheldon Cooper";
String s2 ="Howard Wolowitz";
Output :-
Sheldon Wolowitz
Howard Cooper
Can anyone help me on this....
you can use Split String method in java
String[] arrOfS1 = s1.split(" ");
String[] arrOfS2 = s2.split(" ");
System.out.println(arrOfS1[0] + " " + arrOfS2[1]);
System.out.println(arrOfS2[0] + " " + arrOfS1[1]);

Switch the first and last letter of multiple words in a string [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I am not sure how to approach this problem. I've seen people using arrays but after trying it, it only worked for the whole string and not each words individually.
Thanks for the help!
You can use the split method to split the string into multiple words, saved in a String array. Then, get the first and last letter using the charAt method, and then concatenate them in the correct order to get the modified word.
String a = "This is a string with multiple words";
String[] arr = a.split(" "); //splits string into an array of strings, by separating with spaces
for (int i = 0; i < arr.length; i++) //looping through each word
{
if (arr[i].length() == 1) //don't change word if it only has 1 letter
{
System.out.print(arr[i] + " ");
continue;
}
//using charAt to obtain first and last letter of each word
char firstLetter = arr[i].charAt(0);
char lastLetter = arr[i].charAt(arr[i].length()-1);
String middle = arr[i].substring(1, arr[i].length()-1); //all letters of word except first and last
arr[i] = lastLetter + middle + firstLetter; //concatenating together to create new word
System.out.print(arr[i] + " "); //printing each word after switching letters
}
You seem like a beginner to Java, so I highly recommend you read the Java documentation on the String class and its methods, as Java has some pretty thorough and relatively easy to understand documentation.

Use one String variable for Input [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I'm attempting to change the order of any given name and get an output of ("LastName, FirstName") by only using one string variable.
For example, if entered by the user as Winston Church it would print out
System.out.println("Church,Winston");
How would you combine the Full Name of the user into one or simply just use one string variable for the input? I don't wish to split it but rather combine it.
Will this work for you?
public class NameModifier {
public static void main(String []args) {
String unmodified_full_name = "FirstName LastName";
String[] full_name_array = unmodified_full_name.split(" ", 2);
System.out.println(full_name_array[1] + "," + full_name_array[0]);
}
}

How to find the closest element in an array [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I've tried looking for the answer to this question, but I haven't had a lot of luck. Basically I am asked to create the Fibonacci Code, and then allow a user input to look for there input in the sequence. If its in the sequence then it shows what index. If its not then it shows the two closest numbers to it.
So if a user inputs 4, the nearest elements would be 3 and 5 and the indexes would be 4 and 5.
I'm basically struggling with finding the nearest elements. I'm not exactly sure how to do it.
****update****
So I did figure it out thank you
1.Store the previous Fibonacci number in a buffer (you can initialize with -1)
2.update the buffer after every new number is calculated.
3.if the current number is not equal to the new number
3.A check if the number is greater than buffer and less than the new number
3.A.1)If yes, those two are your nearest numbers.
import java.util.Scanner;
public class Fibonacci {
public static void main(String[] args) {
int userInput;
Fibonacci.fibonacciAlgor(5);
}
public static void fibonacciAlgor(int userInput)
{
int i=0;
int buffer=-1;
int x=0,y=1;
System.out.println("Input: " + userInput);
while(i<1000000){
if(x==userInput){
System.out.println("Belongs to sequence: Yes "");
break;
}
else{
if(userInput>buffer&&userInput<x){
System.out.println("Belongs to sequence: No ");
System.out.println("Nearest Elements: " + buffer+","+x);
break;
}
}
buffer=x;
int temp=y;
y=x+y;
x=temp;
i++;
}
}
}

error in printf() method [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I need help to fix my code. I don't know where is the problem, but it seems in printf the compiler give me error when I run this code. Why?
Employee emp1 = new Employee();
emp1.setFname("Kim");
emp1.setLname("Yee");
emp1.setID(101);
emp1.setSalary(40000);
Employee emp2 = new Employee();
emp2.setFname("Lana");
emp2.setLname("Yum");
emp2.setID(102);
emp2.setSalary(55.000);
Employee emp3 = new Employee();
emp3.setFname("Alex");
emp3.setLname("Jhone");
emp3.setID(103);
emp3.setSalary(55.500);
Employee emp4 = new Employee();
emp4.setFname("joe");
emp4.setLname("mac");
emp4.setID(104);
emp4.setSalary(74.000);
System.out.println("Employee Name \t Employee ID \t Employee salary");
System.out.printf("%s %s\t%d\t$%f\n", emp1.getFname() + "" + emp1.getLname(), emp1.getID(), emp1.getSalary());
System.out.printf("%s %s\t%d\t$%f\n", emp2.getFname() + "" + emp2.getLname(), emp2.getID(), emp2.getSalary());
System.out.printf("%s %s\t%d\t$%f\n", emp3.getFname() + "" + emp3.getLname(), emp3.getID(), emp3.getSalary());
System.out.printf("%s %s\t%d\t$%f\n", emp4.getFname() + "" + emp4.getLname(), emp4.getID(), emp4.getSalary());
#Nishant Shreshth and #hexafraction are right. However, you have not thought the problem through. Any time you have repetitious code like this, you're missing an opportunity. Why not write a method on Employee to return the full name of the employee? You will certainly find more places to use that in a real program.
public String getFullName() {
return fname + " " + lname;
}
One should avoid drudgery. Remember Larry Wall's three virtues of programming: hubris, impatience, and laziness. You should do extra work now so you can be lazy later.
Now, a person's first and last names are more associated with each other than with his id and salary. Also, people have names even if they aren't employees. So, you should really create a Name class and use it in Employee (or Person, or Student, or Customer...) instead of fname and lname. You can then make changes to it when requirements change. How difficult would it be to add middleInitial to all the classes that keep name components? What of title? What of suffix? You may not have the requirement to do that now, but it's a matter of judgement how much to prepare for the future.
If you become a Java programmer, you may also want to have your class implement java.util.Formattable. That is an advanced topic.
System.out.printf("%s %s\t%d\t$%f\n", emp1.getFname()+""+emp1.getLname(), emp1.getID(),emp1.getSalary());
This is the culprit:
emp1.getFname()+""+emp1.getLname()
That expression right above is ONE string. Use:
System.out.printf("%s\t%d\t$%f\n", emp1.getFname()+""+emp1.getLname(), emp1.getID(),emp1.getSalary());
or
System.out.printf("%s %s\t%d\t$%f\n", emp1.getFname(), emp1.getLname(), emp1.getID(),emp1.getSalary());
so you have a matching number of parameters and format specifiers in the format string.
I think +""+ should be just a ,

Categories