Reputation: 18198
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++;
}
}
Upvotes: 0
Views: 25903
Reputation: 21664
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);
Upvotes: 0
Reputation: 2671
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;
}
Upvotes: 0
Reputation: 2167
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++;
}
Upvotes: 2
Reputation: 16962
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++
}
Upvotes: 0
Reputation: 11986
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
}
}
Upvotes: 0