# String Manipulation.

• February 18th, 2014, 11:52 PM
Praetorian
String Manipulation.
Hi all. I hope you all are doing OK. I'm OK but I do have a quick question about string manipulation. You see I've been given a simple exercise that involves asking the user to input a number between 1,000 and 999,999 and displaying the result. Simple enough, but the caveat is that if the user keys in the comma, say 24,000 instead of 24000 for example, the program is not to display the comma. I don't see how to do this without an 'if' statement. The book says the 'if' is not necessary but does offer this hint: "Read the input as a string. Measure the length of the string. Suppose it contains n characters. Then extract the substrings consisting of the first n -4 characters and the last three characters."

What good is n-4 going to do if the string's lengths varies?

Here's what I have written thus far:
Code java:

```import java.util.Scanner;   public class P13 { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Please enter a number between 1,000 and 999,999: "); String input = in.next();   int n = input.length();   /*String outputOne = input.substring(0, 3); String outputTwo = input.substring(4, 7); System.out.print(outputOne + outputTwo);*/   System.out.print(input); }   }```

If anyone has time to chime in, I would really be grateful.
• February 19th, 2014, 12:11 AM
Ganeprog
Re: String Manipulation.
Hi,

Sorry i didn't get your question. Please explain clearly what is your expectation result with some example.

Thanks,:o
• February 19th, 2014, 12:36 AM
Praetorian
Re: String Manipulation.
Thanks for replying Ganeprog. I'm sorry that I wasn't very clear. The output is NOT to include the comma punctuation mark. So if the user keys in 24,000 for example, the program is to output 24000. Notice there is no "," in the output.
• February 19th, 2014, 12:47 AM
Ganeprog
Re: String Manipulation.
Its just simple if you use replace() method. For example

Code java:

` String output=input.replace(",","");`

If you entered number with comma(,) it will remove.

Thanks:o
• February 19th, 2014, 11:37 AM
Praetorian
Re: String Manipulation.
Oh gosh. That is so simple. That's what I'm going to use. Thank you so much. That method is not mentioned anywhere in the section of the text I was reading.
• February 19th, 2014, 11:39 AM
Mugambbo
Re: String Manipulation.
int numLength = input.length();
int commaPos = input.indexOf(",");
String firstPart = input.substring(0,commaPos);
String secondPart = input.substring(commaPos +1, numLength);

String newNum = firstPart + secondPart;

A quite longer method compared to Ganeprog's.
• February 19th, 2014, 11:52 AM
Praetorian
Re: String Manipulation.
Hey, it works. I was trying to think of a way to incorporate the indexOf() method. Couldn't think of how. I really like that Mugambbo. Thanks!