4 Replies - 221 Views - Last Post: 13 May 2014 - 02:38 PM Rate Topic: -----

#1 smalld01  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 25-February 14

Returning a String array

Posted 13 May 2014 - 02:06 PM

These are my instructions for a final study guide question: Write a method that accepts an array of Strings.  The method must return an array containing all strings that 
start with the letter 'A'.  There must be no 'null' values in the resulting array.  The resulting array may be of 
size zero.

I don't think I am returning ALL the strings that start with A, just the last one of the for loop. Does this look somewhat correct?

public String[] startsWithA(String[] strings) {
    String startsWithA = strings[0];
    for(int i = 0; i < strings.length; i++) {
        if(strings[i].charAt(0) == 'A') {
            startsWithA = strings[i];
        }
        return startsWithA;
    }
}



Thanks

This post has been edited by Martyr2: 13 May 2014 - 02:30 PM
Reason for edit:: Fixed code tags.. [code]some code[/code] :)


Is This A Good Question/Topic? 0
  • +

Replies To: Returning a String array

#2 x68zeppelin80x  Icon User is offline

  • D.I.C Addict

Reputation: 130
  • View blog
  • Posts: 576
  • Joined: 07-March 09

Re: Returning a String array

Posted 13 May 2014 - 02:29 PM

View Postsmalld01, on 13 May 2014 - 05:06 PM, said:

Does this look somewhat correct?


No, your method returns String[], but you are returning a String. You need to create an local array and return that.
Was This Post Helpful? 1
  • +
  • -

#3 x68zeppelin80x  Icon User is offline

  • D.I.C Addict

Reputation: 130
  • View blog
  • Posts: 576
  • Joined: 07-March 09

Re: Returning a String array

Posted 13 May 2014 - 02:35 PM

Your first step is the filter out the candidate strings, then compact the array to remove null values. The example below builds a new array of values that start with 'A', but the ones that are not present are null. You will have to devise a way to determine the final size of the array, then add a counter to keep track of your insertions in the new array.

[Apple, null, null, Avocado]

import java.util.Arrays;

class FilterArray {
	public static void main (String[] args) {
		String[] foo = { "Apple", "Orange", "Grape", "Avocado" };
		String[] bar = startsWithA(foo);
		
		System.out.println(Arrays.toString(bar));
	}
	
	public static String[] startsWithA(String[] strings) {
		String[] startsWithA = new String[strings.length];
		for (int i = 0; i < strings.length; i++) {
			if (strings[i].charAt(0) == 'A') {
				startsWithA[i] = strings[i];
			}
		}
		return startsWithA;
	}
}

Was This Post Helpful? 1
  • +
  • -

#4 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4337
  • View blog
  • Posts: 12,137
  • Joined: 18-April 07

Re: Returning a String array

Posted 13 May 2014 - 02:36 PM

Your array through the first iteration is going to check if the string at index 0 begins with 'A' and then it hits that return statement and leaves the function before it has a chance of going around the for loop again.

In other words, your return statement should not be in the for loop, it should be after it.

Second thing is that you are going to have to build a new array inside your function which you will add to as you find matching strings. Ideally you will want to use something like an ArrayList object for this, but I don't know how much you have been taught to know if you can use that or not.

ArrayList newStrArray = new ArrayList();

for (int i = 0; i < strings.length; i++) {
   // Test for 'A'
   // If match, add string to newStrArray
}

return newStrArray.toArray();



That is the general idea. :)

This post has been edited by Martyr2: 13 May 2014 - 02:54 PM

Was This Post Helpful? 0
  • +
  • -

#5 smalld01  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 25-February 14

Re: Returning a String array

Posted 13 May 2014 - 02:38 PM

Thanks guys!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1