0

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++;

}
}
3
  • 1
    "No moare"! Was that a deliberate typo? Do you speak Maineglish? In other words, are/were you from Maine. e.g., close the doare. That's a pretty flowah. Commented Jul 21, 2010 at 4:06
  • It's meant to say "No more". Sorry for my English. Commented Jul 22, 2010 at 16:30
  • You likely want to store the value of data.getItems() so you're only calling it once. It's likely to be a very expensive operation. Commented Jul 22, 2010 at 22:44

5 Answers 5

2

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++;
}              
Sign up to request clarification or add additional context in comments.

2 Comments

You can test if a String is equal to "" by saying str.isEmpty().
@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.
0

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
  }
}

Comments

0

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++
}

2 Comments

wepQty and i are always equal. Thus, i is unnecessary.
My comment on NullUserException's answer also applies here. stackoverflow.com/questions/3296165/…
0

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;
}

1 Comment

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.
0

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);

1 Comment

If it implements Iterable, it can be used in a for each loop.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.