Java Program to add two strings values [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 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]);

Related

How to find words that are different between two Strings? [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 hope you are fine.
I am searching for a simple way to compare two Strings and print out the words which are unique between the two, for example I have :
String one = edittext1.getText().toString();
String two = editText2.getText().toString();
Then the output should be what is the words that exists in edittext1 and not exist in edittext2.
if it's only words you can split the strings to string[] that each cell will contain 1 word only and then compare those words. Goes as follows:
String one = "this is first text example";
String two = "this is next text example";
String[] oneVals = one.split("\\ ");
String[] twoVals = two.split("\\ ");
int i = oneVals.length;
if(oneVals.length != twoVals.length)
{
// determine what to do
}
String wordsNotMatching = "";
for(int j=0; j<i; j++)
{
if((!oneVals[j].equals(twoVals[j])))
wordsNotMatching += oneVals[j] + " ";
}
// wordNotMatching will contain all different words.

"startsWith" 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 3 years ago.
Improve this question
It's showing unknown method "startsWith"
If it's the incorrect way then please tell the correct way
EditText editTxt = (EditText) findViewById(R.id.a);
WebView webv = (WebView) findViewById(R.id.b);
if (editTxt.startsWith("http://"){
webv.loadUrl(editTxt.getText().toString());
}else{
webv.loadUrl("http://" + editTxt.getText().toString());
}
startsWith is method of String, you need to convert to String first:
String editTxtString = editTxt.getText().toString();
if (editTxtString .startsWith("http://")) {
webv.loadUrl(editTxtString);
} else {
webv.loadUrl("http://" + editTxtString);
}
Or one liner:
webv.loadUrl(editTxtString .startsWith("http://")? editTxtString: "http://" + editTxtString);
Is editText is string class? You post to little code but try thisif editText is not String class:
if (editTxt.getText().toString().startsWith("http://"){
webv.loadUrl(editTxt.getText().toString());
}else{
webv.loadUrl("http://" + editTxt.getText().toString());
}

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]);
}
}

Basic Salary Array Converted to Total Salary Array [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 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

How to split a string before a specific character (so that the delimiter character is in the second part of the result) [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 6 years ago.
Improve this question
if I have
String a = "abc,,,";
what should I do to get
result[0].equals("abc");
result[1].equals(",,,");
The most elementary way is this:
final int pos = a.indexOf(',');
if( pos == -1 ) { // character not found, handle this case somehow }
String [] result = new String [2];
result[0]=a.substring(0, pos);
result[1]=a.substring(pos);

Categories