How to put first char of some string in the middle of string [duplicate]












-4















This question already has an answer here:




  • Swapping Characters in a String in Java

    3 answers




Example: chars to ahcrs



Here is my code with little modification:



String word = JOptionPane.showInputDialog(null, "Input something: ");
int check = word.length();
if (check < 3) {
JOptionPane.showMessageDialog(null, "Your input is invalid!");









share|improve this question













marked as duplicate by Community Nov 19 '18 at 19:35


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • I guess you mean chars to hacrs?
    – MWB
    Nov 19 '18 at 16:40










  • Nope i mean, chars to ahcrs. Edit: c is first a is in the middle
    – Football Highlight-Goals-News
    Nov 19 '18 at 16:41








  • 1




    Then your header is incorrect. It says that you want to put the first char, 'c' in this case in the middle, which is in between 'ha' and 'rs'. What you are trying to ask is: How to swap the first and middle char.
    – MWB
    Nov 19 '18 at 16:43












  • Seems you want to reverse first half of string, not just putting first char in the middle. Please show what you've tried.
    – Andreas
    Nov 19 '18 at 16:43












  • Okey then, how i can type that in java, never mind what is correct, i need just to put first char in middle, java will do it 100% correct if i have a good code.
    – Football Highlight-Goals-News
    Nov 19 '18 at 16:44
















-4















This question already has an answer here:




  • Swapping Characters in a String in Java

    3 answers




Example: chars to ahcrs



Here is my code with little modification:



String word = JOptionPane.showInputDialog(null, "Input something: ");
int check = word.length();
if (check < 3) {
JOptionPane.showMessageDialog(null, "Your input is invalid!");









share|improve this question













marked as duplicate by Community Nov 19 '18 at 19:35


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • I guess you mean chars to hacrs?
    – MWB
    Nov 19 '18 at 16:40










  • Nope i mean, chars to ahcrs. Edit: c is first a is in the middle
    – Football Highlight-Goals-News
    Nov 19 '18 at 16:41








  • 1




    Then your header is incorrect. It says that you want to put the first char, 'c' in this case in the middle, which is in between 'ha' and 'rs'. What you are trying to ask is: How to swap the first and middle char.
    – MWB
    Nov 19 '18 at 16:43












  • Seems you want to reverse first half of string, not just putting first char in the middle. Please show what you've tried.
    – Andreas
    Nov 19 '18 at 16:43












  • Okey then, how i can type that in java, never mind what is correct, i need just to put first char in middle, java will do it 100% correct if i have a good code.
    – Football Highlight-Goals-News
    Nov 19 '18 at 16:44














-4












-4








-4








This question already has an answer here:




  • Swapping Characters in a String in Java

    3 answers




Example: chars to ahcrs



Here is my code with little modification:



String word = JOptionPane.showInputDialog(null, "Input something: ");
int check = word.length();
if (check < 3) {
JOptionPane.showMessageDialog(null, "Your input is invalid!");









share|improve this question














This question already has an answer here:




  • Swapping Characters in a String in Java

    3 answers




Example: chars to ahcrs



Here is my code with little modification:



String word = JOptionPane.showInputDialog(null, "Input something: ");
int check = word.length();
if (check < 3) {
JOptionPane.showMessageDialog(null, "Your input is invalid!");




This question already has an answer here:




  • Swapping Characters in a String in Java

    3 answers








java






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 19 '18 at 16:38









Football Highlight-Goals-News

11




11




marked as duplicate by Community Nov 19 '18 at 19:35


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by Community Nov 19 '18 at 19:35


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • I guess you mean chars to hacrs?
    – MWB
    Nov 19 '18 at 16:40










  • Nope i mean, chars to ahcrs. Edit: c is first a is in the middle
    – Football Highlight-Goals-News
    Nov 19 '18 at 16:41








  • 1




    Then your header is incorrect. It says that you want to put the first char, 'c' in this case in the middle, which is in between 'ha' and 'rs'. What you are trying to ask is: How to swap the first and middle char.
    – MWB
    Nov 19 '18 at 16:43












  • Seems you want to reverse first half of string, not just putting first char in the middle. Please show what you've tried.
    – Andreas
    Nov 19 '18 at 16:43












  • Okey then, how i can type that in java, never mind what is correct, i need just to put first char in middle, java will do it 100% correct if i have a good code.
    – Football Highlight-Goals-News
    Nov 19 '18 at 16:44


















  • I guess you mean chars to hacrs?
    – MWB
    Nov 19 '18 at 16:40










  • Nope i mean, chars to ahcrs. Edit: c is first a is in the middle
    – Football Highlight-Goals-News
    Nov 19 '18 at 16:41








  • 1




    Then your header is incorrect. It says that you want to put the first char, 'c' in this case in the middle, which is in between 'ha' and 'rs'. What you are trying to ask is: How to swap the first and middle char.
    – MWB
    Nov 19 '18 at 16:43












  • Seems you want to reverse first half of string, not just putting first char in the middle. Please show what you've tried.
    – Andreas
    Nov 19 '18 at 16:43












  • Okey then, how i can type that in java, never mind what is correct, i need just to put first char in middle, java will do it 100% correct if i have a good code.
    – Football Highlight-Goals-News
    Nov 19 '18 at 16:44
















I guess you mean chars to hacrs?
– MWB
Nov 19 '18 at 16:40




I guess you mean chars to hacrs?
– MWB
Nov 19 '18 at 16:40












Nope i mean, chars to ahcrs. Edit: c is first a is in the middle
– Football Highlight-Goals-News
Nov 19 '18 at 16:41






Nope i mean, chars to ahcrs. Edit: c is first a is in the middle
– Football Highlight-Goals-News
Nov 19 '18 at 16:41






1




1




Then your header is incorrect. It says that you want to put the first char, 'c' in this case in the middle, which is in between 'ha' and 'rs'. What you are trying to ask is: How to swap the first and middle char.
– MWB
Nov 19 '18 at 16:43






Then your header is incorrect. It says that you want to put the first char, 'c' in this case in the middle, which is in between 'ha' and 'rs'. What you are trying to ask is: How to swap the first and middle char.
– MWB
Nov 19 '18 at 16:43














Seems you want to reverse first half of string, not just putting first char in the middle. Please show what you've tried.
– Andreas
Nov 19 '18 at 16:43






Seems you want to reverse first half of string, not just putting first char in the middle. Please show what you've tried.
– Andreas
Nov 19 '18 at 16:43














Okey then, how i can type that in java, never mind what is correct, i need just to put first char in middle, java will do it 100% correct if i have a good code.
– Football Highlight-Goals-News
Nov 19 '18 at 16:44




Okey then, how i can type that in java, never mind what is correct, i need just to put first char in middle, java will do it 100% correct if i have a good code.
– Football Highlight-Goals-News
Nov 19 '18 at 16:44












1 Answer
1






active

oldest

votes


















0














Save the char values in temporary variables.



char firstChar = word.charAt(0); // In your case this will be 'c'
// chars (length is 5; subtract 1 to make it 0 indexed)
char middleChar = word.charAt((word.length-1) / 2); // In your case this will be 'a'

// StringBuilder provides many functions for string manipulation
StringBuilder newWord = new StringBuilder(word);
// Replacing 'c' with the middleChar 'a'
newWord.setCharAt(0, middleChar);
// Replacing 'a' with the firstChar 'c'
newWord.setCharAt((word.length-1) / 2, firstChar);





share|improve this answer





















  • that code give me just last char, JAVA - result is A
    – Football Highlight-Goals-News
    Nov 19 '18 at 20:18










  • @FootballHighlight-Goals-News you asked to swap the first and middle chars. You did not ask to be able to swap any 2 chars in the string. You are correct in saying it gave you A because the division of an even number in this case will result in a decimal. It will automatically round up or down depending on the value. This solution, and question, will only work with odd length words.
    – Dom
    Nov 19 '18 at 22:28










  • @FootballHighlight-Goals-News considering it is impossible to swap the first and middle chars of a string if the string is even; consider marking my answer as correct. My answer does solve the issue you brought forward. Thank you.
    – Dom
    Nov 19 '18 at 22:31


















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Save the char values in temporary variables.



char firstChar = word.charAt(0); // In your case this will be 'c'
// chars (length is 5; subtract 1 to make it 0 indexed)
char middleChar = word.charAt((word.length-1) / 2); // In your case this will be 'a'

// StringBuilder provides many functions for string manipulation
StringBuilder newWord = new StringBuilder(word);
// Replacing 'c' with the middleChar 'a'
newWord.setCharAt(0, middleChar);
// Replacing 'a' with the firstChar 'c'
newWord.setCharAt((word.length-1) / 2, firstChar);





share|improve this answer





















  • that code give me just last char, JAVA - result is A
    – Football Highlight-Goals-News
    Nov 19 '18 at 20:18










  • @FootballHighlight-Goals-News you asked to swap the first and middle chars. You did not ask to be able to swap any 2 chars in the string. You are correct in saying it gave you A because the division of an even number in this case will result in a decimal. It will automatically round up or down depending on the value. This solution, and question, will only work with odd length words.
    – Dom
    Nov 19 '18 at 22:28










  • @FootballHighlight-Goals-News considering it is impossible to swap the first and middle chars of a string if the string is even; consider marking my answer as correct. My answer does solve the issue you brought forward. Thank you.
    – Dom
    Nov 19 '18 at 22:31
















0














Save the char values in temporary variables.



char firstChar = word.charAt(0); // In your case this will be 'c'
// chars (length is 5; subtract 1 to make it 0 indexed)
char middleChar = word.charAt((word.length-1) / 2); // In your case this will be 'a'

// StringBuilder provides many functions for string manipulation
StringBuilder newWord = new StringBuilder(word);
// Replacing 'c' with the middleChar 'a'
newWord.setCharAt(0, middleChar);
// Replacing 'a' with the firstChar 'c'
newWord.setCharAt((word.length-1) / 2, firstChar);





share|improve this answer





















  • that code give me just last char, JAVA - result is A
    – Football Highlight-Goals-News
    Nov 19 '18 at 20:18










  • @FootballHighlight-Goals-News you asked to swap the first and middle chars. You did not ask to be able to swap any 2 chars in the string. You are correct in saying it gave you A because the division of an even number in this case will result in a decimal. It will automatically round up or down depending on the value. This solution, and question, will only work with odd length words.
    – Dom
    Nov 19 '18 at 22:28










  • @FootballHighlight-Goals-News considering it is impossible to swap the first and middle chars of a string if the string is even; consider marking my answer as correct. My answer does solve the issue you brought forward. Thank you.
    – Dom
    Nov 19 '18 at 22:31














0












0








0






Save the char values in temporary variables.



char firstChar = word.charAt(0); // In your case this will be 'c'
// chars (length is 5; subtract 1 to make it 0 indexed)
char middleChar = word.charAt((word.length-1) / 2); // In your case this will be 'a'

// StringBuilder provides many functions for string manipulation
StringBuilder newWord = new StringBuilder(word);
// Replacing 'c' with the middleChar 'a'
newWord.setCharAt(0, middleChar);
// Replacing 'a' with the firstChar 'c'
newWord.setCharAt((word.length-1) / 2, firstChar);





share|improve this answer












Save the char values in temporary variables.



char firstChar = word.charAt(0); // In your case this will be 'c'
// chars (length is 5; subtract 1 to make it 0 indexed)
char middleChar = word.charAt((word.length-1) / 2); // In your case this will be 'a'

// StringBuilder provides many functions for string manipulation
StringBuilder newWord = new StringBuilder(word);
// Replacing 'c' with the middleChar 'a'
newWord.setCharAt(0, middleChar);
// Replacing 'a' with the firstChar 'c'
newWord.setCharAt((word.length-1) / 2, firstChar);






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 19 '18 at 16:52









Dom

111214




111214












  • that code give me just last char, JAVA - result is A
    – Football Highlight-Goals-News
    Nov 19 '18 at 20:18










  • @FootballHighlight-Goals-News you asked to swap the first and middle chars. You did not ask to be able to swap any 2 chars in the string. You are correct in saying it gave you A because the division of an even number in this case will result in a decimal. It will automatically round up or down depending on the value. This solution, and question, will only work with odd length words.
    – Dom
    Nov 19 '18 at 22:28










  • @FootballHighlight-Goals-News considering it is impossible to swap the first and middle chars of a string if the string is even; consider marking my answer as correct. My answer does solve the issue you brought forward. Thank you.
    – Dom
    Nov 19 '18 at 22:31


















  • that code give me just last char, JAVA - result is A
    – Football Highlight-Goals-News
    Nov 19 '18 at 20:18










  • @FootballHighlight-Goals-News you asked to swap the first and middle chars. You did not ask to be able to swap any 2 chars in the string. You are correct in saying it gave you A because the division of an even number in this case will result in a decimal. It will automatically round up or down depending on the value. This solution, and question, will only work with odd length words.
    – Dom
    Nov 19 '18 at 22:28










  • @FootballHighlight-Goals-News considering it is impossible to swap the first and middle chars of a string if the string is even; consider marking my answer as correct. My answer does solve the issue you brought forward. Thank you.
    – Dom
    Nov 19 '18 at 22:31
















that code give me just last char, JAVA - result is A
– Football Highlight-Goals-News
Nov 19 '18 at 20:18




that code give me just last char, JAVA - result is A
– Football Highlight-Goals-News
Nov 19 '18 at 20:18












@FootballHighlight-Goals-News you asked to swap the first and middle chars. You did not ask to be able to swap any 2 chars in the string. You are correct in saying it gave you A because the division of an even number in this case will result in a decimal. It will automatically round up or down depending on the value. This solution, and question, will only work with odd length words.
– Dom
Nov 19 '18 at 22:28




@FootballHighlight-Goals-News you asked to swap the first and middle chars. You did not ask to be able to swap any 2 chars in the string. You are correct in saying it gave you A because the division of an even number in this case will result in a decimal. It will automatically round up or down depending on the value. This solution, and question, will only work with odd length words.
– Dom
Nov 19 '18 at 22:28












@FootballHighlight-Goals-News considering it is impossible to swap the first and middle chars of a string if the string is even; consider marking my answer as correct. My answer does solve the issue you brought forward. Thank you.
– Dom
Nov 19 '18 at 22:31




@FootballHighlight-Goals-News considering it is impossible to swap the first and middle chars of a string if the string is even; consider marking my answer as correct. My answer does solve the issue you brought forward. Thank you.
– Dom
Nov 19 '18 at 22:31



Popular posts from this blog

'app-layout' is not a known element: how to share Component with different Modules

android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

WPF add header to Image with URL pettitions [duplicate]