int i = 0; // array starts from 0
int [] array = new int[100]; // create larger array
while(i < array.length && sum <= 100) // i should be less then length
// && instead of ||
{
System.out.println("Write in the " + i + " number") ;
array[i] = input.nextInt();
sum += array[i]; // += instead of =+
System.out.println("sum is " + sum);
i++; // increment i
}
Ideone DEMO
Answer from Ilya on Stack Overflowint i = 0; // array starts from 0
int [] array = new int[100]; // create larger array
while(i < array.length && sum <= 100) // i should be less then length
// && instead of ||
{
System.out.println("Write in the " + i + " number") ;
array[i] = input.nextInt();
sum += array[i]; // += instead of =+
System.out.println("sum is " + sum);
i++; // increment i
}
Ideone DEMO
First of all, when setting
int i=1;
int [] array = new int[i];
you are creating an array with 1 slot, which index is 0. This is crucial to take note of, when you're doing this:
array[i]=input.nextInt();
because, as you defined it, i is not 1, which means that your assign the 2nd index, index 1 to the return value of input.nextInt().
Your while loop is also off, as i should never be bigger than the length of the array. You should use a for-loop instead, in this fasion:
for(int i = 0; i < array.length; i++) {
array[i] = input.nextInt();
sum += array[i]
if (sum > 100) {
return;
}
}
Videos
This is better done using a for each loop:
for(String x:studentArray){
// do what you want to do with x
}
To skip over values that are null, just insert an if check at the beginning of the loop and continue if x is empty.
Using enhanced for loop is simple
for(String student: studentArray)
{
if(!student.equals(""))
{
//process data
}
}
NOTE: The if condition can also be written as !"".equals(student) but it is not necessary in your scenario as you are already making sure the id's are never null by using StringUtils.defaultString
The better question might be: Why wouldn't you want to use a FOR loop to iterate through an array? There are many ways to iterate through an Array or a collection and there is no law that states you have to use the FOR loop. In a lot of cases, it's simply the best to use for speed, ease of use, and readability. And yet, in other cases it is not:
The Array:
int[] array = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
Display Array with the typical for loop:
for (int i = 0; i < array.length; i++) {
System.out.println(array[i]);
}
Display Array with the enhanced for loop:
for(Integer num : array) {
System.out.println(num);
}
Display Array with the do/while loop:
int i = 0;
do {
System.out.println(array[i++]);
} while (i < array.length);
Display Array with the while loop:
int j = 0;
while (j < array.length) {
System.out.println(array[j++]);
}
Display Array through Recursive Iteration:
iterateArray(array, 0); // 0 is the start index.
// The 'iterateArray()' method:
private static int iterateArray(int[] array, int index) {
System.out.println(array[index]);
index++;
if (index == array.length) {
return 0;
}
return iterateArray(array,index);
}
Display Array using Arrays.stream() (Java8+):
Arrays.stream(array).forEach(e->System.out.print(e + System.lineSeparator()));
Display Array using IntStream (Java8+):
IntStream.range(0, array.length).mapToObj(index -> array[index]).forEach(System.out::println);
Choose your desired weapon....
Considering you have an array like :
int[] array = {1,2,4,5,6};
You can use stream to iterate over it, apart from printing you can perform lot many thing over this array.
Arrays.stream(array).forEach(System.out::println);
Similarly you can do lot many action over collections as well:
List<String> myList = new ArrayList<>(); List
myList.add("A");
myList.add("B");
Stream.of(myList).forEach(System.out::println);
myList.forEach(System.out::println);