Use one String variable for Input [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 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]);
}
}

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

How do I put a list in if statement in Java [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 1 year ago.
Improve this question
import java.util.*
public class text_baes_adventure_game {
public static void main(String[] args){
ArrayList<String> list1 = new ArrayList<String>()
list1.add("Yes")
Scanner input = new Scanner(System.in)
String name = ""
String y_N1 = ""
System.out.println("Hi what is your name: ")
name = input.nextLine()
System.out.println("Hi "+name+" whould you like to play a dungeon game?: ")
y_N1 = input.nextLine()
if (y_N1 == list1){
System.out.println("Ok lets start!")
}
else{
System.out.println("Ok bye!")
}
}
}
1.How do I add a list to the if statement
2.I'm a beginner in java so can someone help me
if (y_N1 == list1){
This doesn't check whether or not the list contains the String y_N1, it checks whether both objects point to the same Object in memory, which isn't possible.
If you mean to check whether or not the list contains the String, use this:
if ( list1.contains(y_N1))
instead.

Java How to display string like this [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
My program will take a string from user via a text field
when he presses the button the string should be formatted as shown in the example
An Example will help you understand better
If String Entered is "hello"
Output should be
hello
elloh
llohe
lohel
ohell
hello
The first character must shift to last until the initial word is formed again.
This must work for any length of string
Displaystr =newStr.charAt(newStr.length() - 1) + newStr.substring(0, newStr.length - 1);
I tried this code but It didn't help
Edited - Please don't put question on hold now.
try this:
public class Main {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
String text = sc.nextLine();
System.out.println(text);
for(int j=0;j<text.length();j++) {
char firstLetter = text.charAt(0); //get the first letter
text = text.substring(1); //remove the first letter from the input string
text = text + firstLetter;
System.out.println(text);
}
}
}

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

How to extract the substring from the String using StringTokenizer in Java? [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 7 years ago.
Improve this question
Following is the source string. And I need to extract the values from this string.
String str = "sid=2096349073&name=Sam_Michaels";
For example, I should get the token value as "2096349073" and next token should be "Sam_Michaels"
You don't need StringTokenizer. This will work same
public class NewClass {
public static void main(String[] args) {
String str = "sid=2096349073&name=Sam_Michaels";
String id=str.substring(1+str.indexOf("="),str.indexOf("&"));
String name=str.substring(1+str.lastIndexOf("="));
System.out.println("Id is : "+id);
System.out.println("name is : "+name);
}
}

Categories