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:
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.