views:

289

answers:

6

OK so here is some logic thinking... What I am trying to do is loop through strings until I hit a null/empty string.. then STOP. All while putting those strings inside a string array...

Here is a sample of the code. I know it's wrong but hopefully to give you an idea of what I am trying to achieve:

int i;
wepN = new String[100];
int wepQty = 0;
boolean anyLeft = true;
while (anyLeft == true) {
for(i = 0;i < 100;i++) {
    if (data.getItems().get(i).getName() == null) {
        anyLeft = false;
        System.out.println(data.getItems().get(i).getName() + " -NO MOARE");
    }
        wepN[i] = data.getItems().get(i).getName();
        wepQty++;

}
}
A: 
wepN = new String[100];
int wepQty = 0;
String name;
boolean anyLeft = true;

for (; wepQty < 100 && anyLeft; wepQty++) {
    name = data.getItems().get(wepQty).getName();
    if (name == null || name.isEmpty()) {
        anyLeft = false;
        System.out.println(name + " -NO MOARE");
    }
    wepN[wepQty] = name;
}

Alternatively you could use a break statement, but some people are very religious when it comes to having their loop control in one place:

wepN = new String[100];
int wepQty = 0;
String name;

for (; wepQty < 100; wepQty++) {
    name = data.getItems().get(wepQty).getName();
    if (name == null || name.isEmpty()) {
         System.out.println(name + " -NO MOARE");
         break;
    }
    wepN[wepQty] = name;
}

Just please don't tell me data.getItems() returns a Java List.

NullUserException
You should replace the second 100 with `wepN.length`. Also, getting a local copy of `data.getItems()` is required since most methods with array/Collection returns will generate and return a new object on each call. (they can't return references to their own copies since they don't want you modifying them)
Gunslinger47
@Guns Well, you don't know for sure `data.getItems()` returns a Collection
NullUserException
Also, now that I look at it... you're incrementing `wepQty` twice per loop and, in your first code sample, you add `null` to your array after you detect it. And not testing for OP's empty string condition. :|
Gunslinger47
@Guns hmmm... I could swear I removed the second increment... And I didn't test for empty string because it wasn't in the original code
NullUserException
A: 

Something like this is what you are after:

Collection<String> original = new LinkedList<String>();
original.add("String1");
original.add("String2");
original.add("");
original.add(null);
original.add("String 3");

Collection<String> tested = new LinkedList<String>();

for(String string: original) {
  if(null != string && !string.isEmpty()) {
    tested.add(string);
  }
}

String[] stringArray = tested.toArray(new String[tested.size()]);

I would argue not to use array at all and just stick to the Collection type however.

If you want to stop on the first occurance of a null or empty string just do:

if(null != string && !string.isEmpty()) {
    tested.add(string);
  } else {
    break;
}
S73417H
I wouldn't recommend jumping straight to a Collection - you could have an interface that requires String[], or have some other reason for preferring it.
jayshao
A: 

Numerous ways:

String currentName;
for(i=0;i<100;++i) {
  currentName=data.getItems().get(i).getName();
  if(currentName == null || currentName.length() ==0) {
    break;
  }
  // continue with old code here
}

If you don't like explicit breaks:

String currentName;
while(anyLeft) {
  currentName=data.getItems().get(i).getName();
  anyLeft= currentName != null && currentName.length() > 0;
  if(anyLeft) {
     // continue with old code here
  }
}
A: 

why you need to use while here?

how about:

for (int i = 0; i < 100 && data.getItems().get(i).getName() != null; i++ {
        wepN[i] = data.getItems().get(i).getName();
        wepQty++;
}

or

int i = 0;
while (data.getItems().get(i).getName() != null && i < 100) {
            wepN[i] = data.getItems().get(i).getName();
            wepQty++;
            i++
}
mohammad shamsi
check would also need to check for empty...
jayshao
`wepQty` and `i` are always equal. Thus, `i` is unnecessary.
Gunslinger47
My comment on NullUserException's answer also applies here.http://stackoverflow.com/questions/3296165/java-do-while-string-is-not-null-or-empty/3296174#3296174
Gunslinger47
+1  A: 

You can use break to exit a for loop, same as you would a switch statement:

String[] wepN = new String[100];                        
int wepQty = 0;

for (int i=0; i < wepN.length; i++) {
    if (data.getItems().get(i).getName() == null || "".equals(data.getItems().get(i).getName())) {
        System.out.println(data.getItems().get(i).getName() + " -NO MOARE");
        break;
    }
    wepN[i] = data.getItems().get(i).getName();
    wepQty++;
}              
jayshao
You can test if a String is equal to "" by saying `str.isEmpty()`.
Gunslinger47
@Gunslinger47 while you can call str.isEmpty() and in this case since we do a null check first it's safe, it's still a good habit in a lot of cases to prefer the older "".equals() since it's always null-safe and String interning means it's not really any less efficient.
jayshao
A: 

There are a few considerations depending on whether the original was a collection.

If you had an array Data[] data,

String[] dataCopy = new String[data.length];
int i = 0;
for (Data datum: data){
 if (datum==null)break;
 dataCopy[i++] = datum;
}

But that is not optimal because you would be assigning more array cells than necessary if the original data had 100 cells but the 50th cell is where the empty string is found.

Using an ArrayList would let the JVM manage the expansion of cells, so that at the end of it you just convert the ArrayList to an array using toArray(). It's not a conversion really, but toArray withdraws the internally managed array from the ArrayList.

ArrayList<String> dataList = new ArrayList<String>(data.length);
for (Data datum: data){
 if (datum==null)break;
 dataList.add(datum);
}
String[] dataCopy = {};
dataCopy = datalist.toArray(dataCopy);

Or if the array you are processing is a member of data:

ArrayList<String> dataList = new ArrayList<String>(data.length);
for (Data datum: data.getItems()){
 String name = datum.getName();
 if (name==null)break;
 dataList.add(name);
}
String[] dataCopy = {};
dataCopy = datalist.toArray(dataCopy);

Or if the original data structure implements Iterable. Let's say the class of items is Item.

Iterator<Item> itemit = data.getItems().iterator();
ArrayList<String> dataList = new ArrayList<String>(data.length);
while(itemit.hasNext()){
  String name = itemit.next().getName;
  if (name==null)break;
  dataList.add(name);
}
String[] dataCopy = {};
dataCopy = datalist.toArray(dataCopy);
Blessed Geek
If it implements Iterable, it can be used in a for each loop.
Gunslinger47