Reputation:
I have a method that prints out the elements of a String array that are not null. Desired output:
1. cook
2. chef
3. baker
4. butcher
5. distiller
Output getting:
1. cook
3. chef
4. baker
7. butcher
9. distiller
The numbers aren't consecutive like they are in the first example. Obviously it's because it's only printing 'i' when 'i' is not null. Is there anyway I can make it look like the first example? I've tried different solutions but none of them seem to work.
public class Main {
public void testMethod() {
String myArray[] = new String [] { "cook", null, "chef", "baker", null, null, "butcher", null, "distiller" };
for (int i = 0; i < myArray.length; i++) {
if (myArray[i] != null)
System.out.println((i + 1) + ". " + myArray[i]);
}
}
public static void main(String[] args) {
Main main = new Main();
main.testMethod();
}
}
Upvotes: 2
Views: 61
Reputation: 2006
Please try this.
public class Main {
public void testMethod() {
String myArray[] = new String [] { "cook", null, "chef", "baker", null, null, "butcher", null, "distiller" };
int count = 0;
for (int i = 0; i < myArray.length; i++) {
if (myArray[i] != null)
System.out.println(((count++) + 1) + ". " + myArray[i]);
}
}
public static void main(String[] args) {
Main main = new Main();
main.testMethod();
}
}
Upvotes: 0
Reputation: 26067
Have one more variable to count
for non-null values
public void testMethod() {
String myArray[] = new String[] { "cook", null, "chef", "baker", null,
null, "butcher", null, "distiller" };
int j=0;
for (int i = 0; i < myArray.length; i++) {
if (myArray[i] != null){
System.out.println((j + 1) + ". " + myArray[i]);
j++;
}
}
}
output
1. cook
2. chef
3. baker
4. butcher
5. distiller
Upvotes: 0
Reputation: 570
You will need a new integer variable for counting. Try this -
public class Main {
public void testMethod() {
int iter = 1;
String myArray[] = new String [] { "cook", null, "chef", "baker", null, null, "butcher", null, "distiller" };
for (int i = 0; i < myArray.length; i++) {
if (myArray[i] != null) {
System.out.println((iter++) + ". " + myArray[i]);
}
}
}
public static void main(String[] args) {
Main main = new Main();
main.testMethod();
}
}
Upvotes: 0
Reputation: 3688
Yes, simply keep a counter of how many you've printed, not just the indices:
public void testMethod() {
String myArray[] = new String [] { "cook", null, "chef", "baker", null, null, "butcher", null, "distiller" };
for (int i = 0, count = 0; i < myArray.length; i++) {
if (myArray[i] != null)
System.out.println((++count) + ". " + myArray[i]);
}
}
Upvotes: 0
Reputation: 5213
Just keep a counter:
public void testMethod() {
String myArray[] = new String [] { "cook", null, "chef", "baker", null, null, "butcher", null, "distiller" };
int j = 0;
for (int i = 0; i < myArray.length; i++) {
if (myArray[i] != null) {
System.out.println(++j + ". " + myArray[i]);
}
}
}
Upvotes: 4
Reputation: 393831
Since you can't use i
as a counter, you'll need a separate counter :
public void testMethod() {
String myArray[] = new String [] { "cook", null, "chef", "baker", null, null, "butcher", null, "distiller" };
int counter = 1;
for (int i = 0; i < myArray.length; i++) {
if (myArray[i] != null)
System.out.println((counter++) + ". " + myArray[i]);
}
}
Upvotes: 1