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

Thread: Return an array of Pairs that have the same length as the input array of Strings.

  1. #1
    Member
    Join Date
    Jan 2013
    Posts
    31
    My Mood
    Confused
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Exclamation Return an array of Pairs that have the same length as the input array of Strings.



    Hey everyone,
    I want to create a class that would return an array of pairs that have the same length as the input array of strings.
    In addition, the pair should have the first letter of the string and length of the string.

    for example;
    create(new String[] {"clue", "yay", "neon", "halala"})
    should return the array of Pairs
    {[’c’,4],[’y’,3],[’n’,4],['h',6]}

    So, my input and output would both be arrays. But the output has to be in the form of a pair. Here's what i tried:

    import java.util.Arrays;
     
     
    public class Couple {
     
     
    public static Couple[] create(String[] source){
     
    		for (int i = 0; i < source.length; i++) {
     
    			System.out.print("["+","+source.length+"]") ;
    			 }
    		return null;
     
    	}			 
     
    	public static void main(String[] args) {
    		System.out.println(Arrays.toString(create(new String[] {"clue", "yay", "neon", "halala"})));
     
    	}
     
    }

    as it's obvious there are a few errors+ i dont want it to return null. But just for the sake of testing this code, i had to do it.
    Any ideas?


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Return an array of Pairs that have the same length as the input array of Strings.

    @MartinEnergy Welcome to the forum.
    Please see: The problem with spoonfeeding

  3. #3
    Member
    Join Date
    Jul 2013
    Location
    Franklin, TN
    Posts
    47
    Thanks
    3
    Thanked 4 Times in 4 Posts

    Default Re: Return an array of Pairs that have the same length as the input array of Strings.

    Hi. Well first lets take a look at what you're doing here... No matter what your method is going to return null because that's what you've told it to do. You basically just said to return this method with a null value.

    So think of it this way.. I will give you a poorly thought of metaphor. Think of the array "source" as a piece of paper, and the strings you input to the array as words on the piece of paper. If you wanted to see how long one of the words on the sheet of paper was, would you measure the paper it's on? or the word? Think about that and read over these lines:

    for (int i = 0; i < source.length; i++) {
     
    			System.out.print("["+","+source.length+"]") ;

  4. #4
    Member
    Join Date
    Jul 2013
    Location
    Franklin, TN
    Posts
    47
    Thanks
    3
    Thanked 4 Times in 4 Posts

    Default Re: Return an array of Pairs that have the same length as the input array of Strings.

    What....? Please don't listen to Martin. There is no reason to make an object for your task is much simpler than that.

    Hemla if you are not looking to find a value, but rather do a certain sequence of things to have them get done, ex println's, then you may utilize a void type method. That way you will not have to return a value at all, but just execute the code block written within your method.

    As for the length of the string, you are simply accessing their value wrong. But I'm sure you could pull it from the heaping spoon of code Martin gave you...

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

    hemla (August 8th, 2013)

Similar Threads

  1. 2D Array with Strings
    By xionyus in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 3rd, 2012, 07:35 AM
  2. Replies: 6
    Last Post: October 7th, 2011, 07:50 AM
  3. Doubling The Array Size And Randomizing Array Return
    By Pingu00 in forum What's Wrong With My Code?
    Replies: 18
    Last Post: June 27th, 2011, 10:50 AM
  4. max length of an array?
    By qsbladey in forum Collections and Generics
    Replies: 4
    Last Post: April 3rd, 2011, 11:17 AM
  5. Finding the length of a two dimensional array
    By petemyster in forum Java Theory & Questions
    Replies: 2
    Last Post: December 12th, 2010, 10:21 PM