Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: String Manipulation.

  1. #1
    Member
    Join Date
    Feb 2013
    Posts
    38
    Thanks
    4
    Thanked 1 Time in 1 Post

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


  2. #2
    Member Ganeprog's Avatar
    Join Date
    Jan 2014
    Location
    Chennai,India
    Posts
    80
    My Mood
    Love
    Thanks
    3
    Thanked 6 Times in 6 Posts

    Default Re: String Manipulation.

    Hi,

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

    Thanks,

  3. #3
    Member
    Join Date
    Feb 2013
    Posts
    38
    Thanks
    4
    Thanked 1 Time in 1 Post

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

  4. #4
    Member Ganeprog's Avatar
    Join Date
    Jan 2014
    Location
    Chennai,India
    Posts
    80
    My Mood
    Love
    Thanks
    3
    Thanked 6 Times in 6 Posts

    Default Re: String Manipulation.

    Its just simple if you use replace() method. For example

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

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

    Thanks

  5. The Following User Says Thank You to Ganeprog For This Useful Post:

    Praetorian (February 19th, 2014)

  6. #5
    Member
    Join Date
    Feb 2013
    Posts
    38
    Thanks
    4
    Thanked 1 Time in 1 Post

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

  7. #6
    Member
    Join Date
    Dec 2013
    Posts
    40
    My Mood
    Amazed
    Thanks
    2
    Thanked 11 Times in 11 Posts

    Default 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.
    Who holds the KEY to all knowledge?

  8. The Following User Says Thank You to Mugambbo For This Useful Post:

    Praetorian (February 19th, 2014)

  9. #7
    Member
    Join Date
    Feb 2013
    Posts
    38
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default 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!

  10. The Following User Says Thank You to Praetorian For This Useful Post:

    Mugambbo (February 19th, 2014)

Similar Threads

  1. Image manipulation program help
    By noobiecode in forum What's Wrong With My Code?
    Replies: 10
    Last Post: March 24th, 2013, 06:30 AM
  2. string manipulation
    By nhiap6 in forum Java Theory & Questions
    Replies: 2
    Last Post: November 17th, 2012, 06:32 AM
  3. string manipulation
    By nhiap6 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 16th, 2012, 12:28 PM
  4. String Manipulation.
    By Nemus in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 21st, 2011, 03:02 PM
  5. String manipulation help
    By Duff in forum Loops & Control Statements
    Replies: 7
    Last Post: March 19th, 2010, 04:02 AM