1

hi guys i've had some help with this program today, basically what i want is for an array of 1 - 200 to be held, then the user inputs a number between 1 and 200. The ramaning numbers are then added together and the answer outputted.

e.g. user enters 100, numbers from 100-200 are then added together and answer is output.

With the code i have so far it is always outputting 0 as the answer. Any ideas? thanks.

//Importing scanner
import java.util.Scanner;
//Class name
class programfive{

    //Main method
    public static void main (String[]args){
        //declaring and inizialising scanner
        Scanner input = new Scanner (System.in);    
        //Declaring and inizialising variables 
        int userInput = 0;
        int sum = 0;
        //Array initializer
        int array[] = new int [201];
        //Prompt for user input
        System.out.print("Please enter a value between 1 and 200: ");
        userInput = input.nextInt();
        //For loop - starts at number user inputted, stops when reaches 200, increments by 1.
        for (int i = userInput; i<=200; i++)
        {
            sum += array[i];
        }
        System.out.println(sum);
    }//End of main method
}//End of class
3
  • Do you want to add the numbers from 100-200 together or the numbers contained in the array on the indexes 100-200? Commented Jan 20, 2016 at 14:30
  • do you use the user input somewhere else or just to do the sum? If it is just the sum, I would advise to not even use an array. Commented Jan 20, 2016 at 14:31
  • Just a general comment regarding the amount of comments in your code: in the future try to only comment what is not self-explanatory, otherwise the important comments will be missed. Of the 10 comments, only the third last would be relevant to explain the loop condition. Commented Jan 20, 2016 at 14:49

5 Answers 5

3

Because you haven't put anything in your array, it contains the default int value at each index, and that is 0 .

You have to fill it with the values you want, so that array[0] contains 0, array[1] contains 1, etc..

int array[] = new int [201];

for(int i=0; i<array.length;i++)
    array[i] = i;

Also, you could get rid of the array and get the same result :

for (int i = userInput; i<=200; i++)
    {
        sum += i;
    }
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! it works and i understand the conditions of the loop but just for my own understanding what is the sum +=i; doing?
sum +=i means sum = sum+i , so it adds i to the sum.
1

you need to initialize the array first, or changing the sum loop to:

for (int i = userInput; i<=200; i++)
{
    sum += i;
}

Comments

0

Untested but should work:

public static void main(String[]args) {
    Scanner input = new Scanner(System.in);    
    int userInput = 0;
    int sum = 0;
    System.out.print("Please enter a value between 1 and 200: ");
    userInput = input.nextInt();
    for (int i = userInput; i<=200; i++)
        sum += i
    userInput.close();
    System.out.println(sum);
}

1 Comment

It won't: you forgot semicolon after sum += i :)
0

you forgot to populate your array with numbers currently all of your array elements are pointing to the default value of 0.

add this line of code after the array declaration and your good to go:

 for(int i=0;i<array.length;i++)
            array[i]=i;

Comments

0
System.out.println("Enter five numbers: ");
Scanner scanner = new Scanner(System.in);

int[] array = new int [5] ;
for (int i =0; i < array.length; i++){
    array[i] = scanner.nextInt();
}
int sum = 0;
for (int i : array){
    sum = sum +i;
}
System.out.println(sum);

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

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.