-1

i m writing a code as homework, the task is to create a code, where the user give a number (for example 4 ) and the output will be all the number from 4 ( the users input ) till the length of array (10), the task is to do just with a for-Statement. literally i m trying as much i can, but every time the output will be 10 times "0", or the users will have to give as input 10 times the numbers. here what i wrote the last time before i asked here hel; i ve tried to look for others similar things here or on other sites, but i didn´ find soemthing related specificately to it... thank you

here the last tentative i ve tried

import java.util.Scanner;
import java.util.Arrays;

public class Arrayspopulate {
    public static void main(String[] args) {
        Scanner firstnumber = new Scanner(System.in);
        int [] array = new int [10];



        System.out.println(" Please give the first number");
        for (int i = 0; i < array.length; i++){
            array[i] = firstnumber.nextInt();
            System.out.println(array[i]);
            }
        }
        }
1
  • 1): Ask the User for a number (signed or unsigned) before the for loop. Don't ask within the loop. 2): Although you can, don't display the array contents until the array is completed. Do that after the for loop using: System.out.println(Arrays.toString(array));. 3): All you should have within the for loop block is: array[i] = firstNumber + i;. Commented May 12, 2024 at 10:12

1 Answer 1

1

You need to take a single positive integer from the user, and populate the array with consecutive values starting from the value the user entered. Therefore, you have to iterate through all positions of the array and initialize them to a value that equals the current position of the array plus the value entered by the user:

import java.util.Scanner;
import java.util.Arrays;

public class ArraysPopulate {
    public static void main(String[] args) {
        Scanner firstNumber = new Scanner(System.in);
        int [] array = new int [10];
        int value = -1;
        while (value < 0) {
            System.out.println(" Please give the first number");
            value = firstNumber.nextInt();
        }    
        for (int i = 0; i < array.length; i++){
            array[i] = i + value;
        }
        System.out.println(Arrays.toString(array));
    }
}

Input: 4

Output: [4,5,6,7,8,9,10,11,12,13]

Asking the user for the size of the array and creating it to have that size is left as an exercise for the reader.

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

4 Comments

thank you so much! can I ask you why the int value was setted as " - 1"?
Good question @Reffert. It's done this way (in a while loop) to act as a form of numerical validation so to ensure a unsigned (positive) integer value is supplied. If it's not then the User is asked for the value again. But in reality, it shouldn't matter if the supplied value is either negative (signed) or positive (unsigned).
@Reffert Setting value to -1 was based on my assumption that the range of values that the user can enter that would be considered valid goes from zero to positive infinity. It was not explicitly stated in your question that that was the case, but it seemed reasonable to me. Under that assumption, I need to initialize it to a negative value if I want to keep asking the user for input until they give a valid value, because all positive values are valid. Therefore if I initialized it to a positive value (including zero), it would not enter the loop as it is written.
And whether it is either or neither of those things matters not as far as the presented problem and the proposed solution are concerned. So I see no need to split hairs like that over such a trivial thing. I think we all understand my use of "positive" to mean "doesn't start with a minus sign" even if a math nerd would give me a look of disapproval for using it that way.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.