A single line contains integers separated by space in javascript

Helllo,

I have easy question, but can't find the solution. Need to print all numbers from 1 to N (10) but not in a row. The output needs to bee in single line separated by spaces - 1 2 3 4 5...

let n = 10;
for (let i = 1; i <= n; i++) {
    console.log(i);
}

Can I do it without using array?

asked Dec 10, 2018 at 20:42

A single line contains integers separated by space in javascript

Николай МатевНиколай Матев

5712 gold badges9 silver badges20 bronze badges

1

Concatenate with a string in the loop instead, then console.log that string:

let n = 10;
let str = '';
for (let i = 1; i <= n; i++) {
  str += i + ' ';
}
console.log(str.trim());

answered Dec 10, 2018 at 20:43

1

A different approach could be to create an array using fill and map to create an array, then join the values to create the string.

let n = 10
let r = new Array(n).fill(null).map((val, idx) => idx + 1).join(' ')

console.log(r)

answered Dec 10, 2018 at 20:53

A single line contains integers separated by space in javascript

Get Off My LawnGet Off My Lawn

31.8k34 gold badges156 silver badges297 bronze badges

1

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    There are 2 methods to take input from the user which are separated by space which are as follows:

    1. Using BufferedReader Class and then splitting and parsing each value
    2. Using nextInt( ) method of Scanner class

    Let us discuss both the methods one by one in order to get a better understanding by implementing the same clean java programs. 

    Method 1: 

    Using BufferedReader Class and then splitting and parsing each value.

    Procedure:

    1. Using readline() method of BufferedReader and Scan the whole string.
    2. Split this String for str.split(” “)
    3. Iterate over the above array and parse each integer value using Integer.parseInt( ).

    Example 

    Java

    import java.io.BufferedReader;

    import java.io.IOException;

    import java.io.InputStreamReader;

    class GFG {

        public static void main(String[] args)

            throws IOException

        {

            BufferedReader bi = new BufferedReader(

                new InputStreamReader(System.in));

            int num[] = new int[10];

            String[] strNums;

            System.out.println("enter string of numbers");

            strNums = bi.readLine().split(" ");

            for (int i = 0; i < strNums.length; i++) {

                num[i] = Integer.parseInt(strNums[i]);

            }

            System.out.println("printing stored numbers ");

            for (int i = 0; i < strNums.length; i++) {

                System.out.println(num[i]);

            }

        }

    }

    Output:

    A single line contains integers separated by space in javascript

    FIG = OUTPUT OF METHOD 1 

    Method 2: Using nextInt() method of Scanner class.

    Procedure: 

    1. Using the nextInt() method of Scanner class and scan to scan the input.
    2. Using for loop to store input in an array.
    3. Iterate through the above array and parse each integer using sc.nextInt()

    Example 

    Java

    import java.io.IOException;

    import java.util.Scanner;

    class GFG {

        public static void main(String[] args)

            throws IOException

        {

            System.out.println("enter input ");

            Scanner sc = new Scanner(System.in);

            int[] nums = new int[10];

            int i;

            for (i = 0; i < nums.length; i++) {

                nums[i] = sc.nextInt();

            }

            System.out.println("printing stored values");

            for (i = 0; i < nums.length; i++) {

                System.out.println(nums[i] + " ");

            }

        }

    }

    Output:

    A single line contains integers separated by space in javascript

    FIG = OUTPUT OF METHOD 2

    Note: The first method using Bufferedreader class and then splitting and parsing each value is much faster than using nixing() method of Scanner class. It is nearly 2 times faster than the second one. Bel;ow we do provide in order how to calculate the time consume by both methods by using  nanotime method 

    // Initializing variables
    long startTime, endTime;
    
    // Start time
    startTime = System.nanoTime(); {
    
    // Insert code here
    // Method 1 or method 2 code
    }
    
    // End time
    endTime = System.nanoTime();

    How do I take the input of a line of integers separated by a space in Java?

    There are 2 methods to take input from the user which are separated by space which are as follows: Using BufferedReader Class and then splitting and parsing each value. Using nextInt( ) method of Scanner class.

    How do you take space separated integer inputs?

    scanf uses any whitespace as a delimiter, so if you just say scanf("%d", &var) it will skip any whitespace and then read an integer (digits up to the next non-digit) and nothing more. Note that whitespace is any whitespace -- spaces, tabs, newlines, or carriage returns.

    How do I take the input of a line of integers separated by a space in Python?

    Use an input() function to accept the list elements from a user in the format of a string separated by space. Next, use a split() function to split an input string by space. The split() method splits a string into a list.

    How do you take space separated input in for loop in Python?

    how to get user input of list of lists in python.
    lst = [ ].
    n = int(input("Enter number of elements : ")).
    for i in range(0, n):.
    ele = [input(), int(input())].
    lst. append(ele).
    print(lst).