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 10 of 10

Thread: Help needed for writing a code

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Location
    Dhaka,bangladesh
    Posts
    7
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Question Help needed for writing a code

    Write a function trim() that will take an array of characters as an input and remove multiple consecutive spaces from the array (You cannot use the String class in java)..

    Can anyone help me out?
    i dont know how to use trim() method in a char array[]..
    Thanks..


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Help needed for writing a code

    What have you tried? Where are you stuck?

    You're supposed to *write* the trim() method, not use a preexisting one.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Mar 2012
    Location
    Dhaka,bangladesh
    Posts
    7
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Help needed for writing a code

    ^Yes i tried.. I have took an array of characters.. But im not understanding how to remove the multiple spaces from that array.. Can you please explain how to do that?

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Help needed for writing a code

    Can you please explain what you've tried and where you're stuck? It's best to post an SSCCE that demonstrates what you've done.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help needed for writing a code

    Here's an idea: Take a piece of paper and pen and draw some slots representing an array with some letters and some empty spaces. Represent array indexes by putting arrows under the slots in the array.
    Then look at finding the first slot on the left where there are multiple spaces and scan over that to the first slot where there is a letter. Now think about how to move the letters and change the indexes to move the letters left to cover the spaces.
    If you don't understand my answer, don't ignore it, ask a question.

  6. The Following User Says Thank You to Norm For This Useful Post:

    ahanf (June 28th, 2012)

  7. #6
    Junior Member
    Join Date
    Mar 2012
    Location
    Dhaka,bangladesh
    Posts
    7
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Help needed for writing a code

    Quote Originally Posted by Norm View Post
    Here's an idea: Take a piece of paper and pen and draw some slots representing an array with some letters and some empty spaces. Represent array indexes by putting arrows under the slots in the array.
    Then look at finding the first slot on the left where there are multiple spaces and scan over that to the first slot where there is a letter. Now think about how to move the letters and change the indexes to move the letters left to cover the spaces.
    thanks.. I think i got what you have tried to told.. Will let you know if i again find any further problem..

  8. #7
    Junior Member
    Join Date
    Mar 2012
    Location
    Dhaka,bangladesh
    Posts
    7
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Help needed for writing a code

    @kevin I tried till this point..
    public class TrimTest{
    public static void main(String[]args){
    char[] data={some characters here including spaces};
    for(int c=0;c<data.length;c++){
    System.out.print(data[c];
    }
    got stuck there... I thought there is a way apply trim() method to the array to remove the spaces like String..
    Last edited by ahanf; June 28th, 2012 at 01:13 PM.

  9. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help needed for writing a code

    Please use code tags not quote tags around your code.
    Edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  10. #9
    Junior Member
    Join Date
    Jul 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help needed for writing a code

    /** Trims the character array from the given character
      * Since you can't shorten the array it'll just fill the remaining end of the array with spaces
      * 
      * @param c The to-be-trimmed array
      * @param r The to-be-trimmed character(in your case space)
    **/
    public static void trim(char[] c, char r)
    {
    	for(int i = 0; i<c.length; c++)
    		if(c[i]==r){
    			for(int i2 = i+1; i2<c.length; i2++)
    				c[i-1] = c[i];
    			c[i] = ' '; 
    		}
    }

    and if you want to call this function in the main:
    public static void main(String[]args)
    {
    	char[] data={some characters here including spaces};
    	trim(data, ' ');
    	for(int c=0;c<data.length;c++){
    		System.out.print(data[c];
    	}
    }
    Last edited by dopefish; July 4th, 2012 at 01:30 PM.

  11. #10
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Help needed for writing a code

    dopefish, please read this: http://www.javaprogrammingforums.com...n-feeding.html
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. [SOLVED] Help needed writing a program using JOptionPane
    By s1mmi in forum Object Oriented Programming
    Replies: 3
    Last Post: January 30th, 2012, 05:39 PM
  2. Need Help Writing Exception code.
    By USCPaddler in forum Exceptions
    Replies: 4
    Last Post: November 10th, 2011, 06:45 PM
  3. writing java code help
    By jaisan72980 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: February 12th, 2011, 03:39 PM
  4. Trouble writing some code...help?
    By bChEos in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: February 7th, 2010, 08:54 PM
  5. Help writing some Scanner code
    By bChEos in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: February 3rd, 2010, 04:27 AM