3

I have to:

  • create this java array;
  • loop through it using a while loop;
  • terminate the program if the sum adds up to 100;
  • and print the sum and the numbers that I did put into the array.

I can't figure out how to do that, here is my code so far, any help would be appreciated.

public class december2012 {
    public static void main(String[] args) {  

        int sum=0;            

        Scanner input = new Scanner(System.in);            

        int i=1;

        int [] array = new int[i];

        while( i > array.length || sum <= 100) {

          System.out.println("Write in the " + i + " number") ; 
          array[i]=input.nextInt();
          sum=+array[i];
           System.out.println("sum is " + sum); 

        }            

        }
    }
2
  • Customarily, Java classes begin with a capital letter. Commented Oct 20, 2013 at 17:10
  • you mean sum = sum + array[i], note that i is 1 all the time, and so you get an ArrayIndexOutOf BoundException. Commented Oct 20, 2013 at 17:13

5 Answers 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

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

0

You need to increment the i variable; currently its value is always 0, so you're only setting the first element in the array.

Comments

0
public class December2012 {
    public static void main(String[] args) {
        int[] array = new int[100];
        int sum = 0;
        int i = 0;
        Scanner input = new Scanner(System.in);
        while (sum <= 100 && i < array.length) {
            System.out.print("Write in the " + (i + 1) + "th number: ");
            array[i] = input.nextInt();
            sum += array[i];
            i++;
        }
        System.out.print("You added: ");
        for (Integer i : array) System.out.print(i + " ");
        System.out.println("\nsum is " + sum);
    }
}

Comments

-1

How about like this, simple way of doing:

int sum = 0;
while(true) {
   //Do calculations
   if(sum >= 100) {
      break;
   }
}

System.out.println("Sum is : " + sum);

Comments

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.