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

Thread: Need help with String Trimming.. and displaying Keys on maps.

  1. #1
    Junior Member
    Join Date
    Oct 2009
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Need help with String Trimming.. and displaying Keys on maps.

    orking on learning recursion - and i have finally solved the issue.

    The last issue I need to figure out are two minor issues that I can't seem to find out the problems on. After 2 hours of testing and no success... i'm over here.. seeking help and assistance on some pointers.



    I have a method testR that accepts a string = symbol.

    The method will split the String, if it has a '|', or white space.

    But if, for example, the string symbol of this is passed int:

    " T | W J Y P "

    The method below fails to eliminate the trailing and starting white spaces between the letters. Wondering where I should be running it.
    	public String testR(String symbol){
    			String s = storeString.get(symbol);
    			String[] parts = s.split("[|]");
    			Random rand = new Random();
    			int pick = rand.nextInt(parts.length);		
    			String x = parts[pick];
    			String[] space = x.split("[ \t]");
     
    			String rString = "";
     
    			for( int i=0; i < space.length; i++){
    				if(!grammarContains(space[i])){ 
    					rString = rString.concat(space[i].trim()+" ");
    				}else{			
    					rString = rString.concat(testR(space[i].trim()));
    				}
    			}
    			return rString;
    	}

    Then I have a method showKeys. Assume that I already have a map and keys/values stored.

    I'm just simply trying to access the keys and display it out.

    But i'm having no luck in that either.

     
    	public String showKeys(){		 
    		String s = "";
    		for (String nonTerminal : storeString.keySet() ) {
          	s.concat(nonTerminal+", ");
        	}
    	//	s = "["+s+"]";
    		return s;
    	}

    Thanks.
    Last edited by helloworld922; November 9th, 2009 at 03:30 AM.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Need help with String Trimming.. and displaying Keys on maps.

    For the first problem (if I understand the problem) you may want to call trim after your first split on '|' eg String x = parts[pick].trim();
    For the second problem you might have to elaborate to exactly what the problem is, but just looking at the code, you might want to use StringBuilder or StringBuffer as opposed to concat.

Similar Threads

  1. List of shortcuts key for Eclipse
    By Flash in forum Java JDK & IDE Tutorials
    Replies: 3
    Last Post: July 18th, 2013, 08:40 AM
  2. Convert Maps of String to Map of Maps
    By abhay8nitt in forum Collections and Generics
    Replies: 1
    Last Post: October 27th, 2009, 07:27 AM
  3. combining keys
    By subhvi in forum Java SE APIs
    Replies: 10
    Last Post: August 29th, 2009, 04:35 PM
  4. [SOLVED] displaying sum of harmonic series
    By sriraj.kundan in forum Algorithms & Recursion
    Replies: 2
    Last Post: June 15th, 2009, 11:42 PM
  5. Creating and displaying a JPanel inside another JPanel
    By JayDuck in forum AWT / Java Swing
    Replies: 1
    Last Post: April 7th, 2009, 08:02 AM