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 Overflow Top answer 1 of 5
5
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
2 of 5
0
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;
}
}
W3Schools
w3schools.com โบ java โบ java_arrays_loop.asp
Java Loop Through an Array
Java Examples Java Videos Java ... Java Certificate ... You can loop through the array elements with the for loop, and use the length property to specify how many times the loop should run....
Videos
01:43:13
2) For Loop, While Loop, Array, String in java - YouTube
06:54
The Do While Loop in Java - YouTube
04:45
Java Tutorial - 06 - Using Enhanced For Loop with Arrays - YouTube
06:47
Java WHILE Loop as Iterator with example. Java Tutorial for Beginners ...
07:17
Beginner Java Tutorial #16: Nested Loops in Java with Arrays - YouTube
12:18
Java Tutorial - 02 - Using a Loop to Access an Array - YouTube
Educative
educative.io โบ answers โบ how-do-you-execute-a-while-loop-in-java
How do you execute a 'while' loop in Java?
Then we define an index variable to iterate over the array. The while loop, from lines 6 to 9, is run till index becomes equal to len indicating that the entire array is traversed.
Instructables
instructables.com โบ design โบ software
How to Use a While Loop to Iterate an Array in Java : 9 Steps - Instructables
July 22, 2022 - How to Use a While Loop to Iterate an Array in Java: Today I will be showing you how to use Java to create a While loop that can be used to iterate through a list of numbers or words. This concept is for entry-level programmers and anyone who wants to get a quick brush-up on Java Loops and arrays.
BeginnersBook
beginnersbook.com โบ 2015 โบ 03 โบ while-loop-in-java-with-examples
While loop in Java with examples
Fourth iteration: value of i is 3, fourth element of the array represented by arr[3] is printed. After fourth iteration: value of i is 4, the condition i<4 returns false so the loop ends and the code inside body of while loop doesnโt execute. Practice the following java programs related to ...
W3Schools
w3schools.com โบ java โบ java_while_loop.asp
Java While Loop
How Tos Add Two Numbers Swap Two ... Number ArrayList Loop HashMap Loop Loop Through an Enum ... assert abstract boolean break byte case catch char class continue default do double else enum exports extends final finally float for if implements import instanceof int interface long module native new package private protected public return requires short static super switch synchronized this throw throws transient try var void volatile while Java String ...
TutorialKart
tutorialkart.com โบ java โบ java-array โบ java-array-while-loop
Java Array - While Loop
November 23, 2020 - Java Array While Loop - To access elements of an array using while loop, use index and traverse the loop from start to end or end to start by incrementing or decrementing the index respectively.
Runestone Academy
runestone.academy โบ ns โบ books โบ published โบ csjava โบ Unit7-Arrays โบ topic-7-3-arrays-with-foreach.html
7.3. Enhanced For-Loop (For-Each) for Arrays โ CS Java
To set up a for-each loop, use for (type variable : arrayname) where the type is the type for elements in the array, and read it as โfor each variable value in arraynameโ. for (type item: array) { //statements using item; } See the examples below in Java that loop through an int and a String ...
CodingBat
codingbat.com โบ doc โบ java-array-loops.html
CodingBat Java Arrays and Loops
In contrast, Java Lists can grow and shrink over time -- this is a big feature that Lists have that arrays do not. The length of an array can be accessed as a special ".length" attribute. For example, with the above "values" array, we can access the size of the array as "values.length". It is common to use a 0...length-1 for-loop to iterate over all the elements in array:
Tutorialspoint
tutorialspoint.com โบ java โบ java_while_loop.htm
Java - while Loop
In this example, we're showing the use of a while loop to print contents of an array. Here we're creating an array of integers as numbers and initialized it some values. We've created a variable named index to represent index of the array while iterating it. In while loop we're checking the index to be less than size of the array and printed the element of the array using index notation.
Runestone Academy
runestone.academy โบ ns โบ books โบ published โบ csjava โบ Unit7-Arrays โบ topic-7-2-traversing-arrays.html
7.2. Traversing Arrays with For Loops โ CS Java
This is called traversing the array. Just start the index at 0 and loop while the index is less than the length of the array.
CodingTechRoom
codingtechroom.com โบ question โบ -java-while-loop-array
How to Iterate Through a Java Array Using a While Loop - CodingTechRoom
Use a while loop to traverse the array, ensuring to increment the index variable within the loop body.
Medium
medium.com โบ @AlexanderObregon โบ java-and-array-iteration-what-beginners-need-to-know-22a44dd32afb
Java and Array Iteration โ What Beginners Need to Know
June 17, 2024 - The condition i < numbers.length ensures that the loop continues as long as i is less than the length of the array. The index i is incremented by 1 in each iteration. ... Flexibility: The while loop is useful when the number of iterations is not known beforehand or when the loop needs to be controlled by a more complex condition.
GeeksforGeeks
geeksforgeeks.org โบ java โบ java-while-loop-with-examples
Java while Loop - GeeksforGeeks
Explanation: In the above example, the while loop runs until "i" is less than 6 and prints "Hello World" 5 times. Example: Calculating the Sum of Numbers from 1 to 10 with Java while Loop
Published ย November 11, 2024
GeeksforGeeks
geeksforgeeks.org โบ java โบ loops-in-java
Java Loops - GeeksforGeeks
This is one of the most common mistakes while implementing any sort of looping is that it may not ever exit, that is the loop runs for infinite time. This happens when the condition fails for some reason. ... Example: Here, both the examples demonstrates the infinite loops. ... // Java program to demonstrate // the infinite for loop import java.io.*; class Geeks { public static void main(String[] args) { for (int i = 0; i < 5; i--) { System.out.println( "This loop will run forever"); } } }
Published ย August 10, 2025