0
public static void main(String[] args) 
{
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        while (t-->0) 
        {
            int n=sc.nextInt();
            ArrayList <Integer> arr=new ArrayList<Integer>();
            for(int i=0; i<n; i++) 
            {
                arr.add(sc.nextInt());
            }
            System.out.println(arr);
       }
}

I already know the size of the ArrayList. I also saw other questions, but they read using the hasNext(), when the size is unknown. How do I do it this way, when the array size is previously known? I just wanted to use inbuilt functions like rotate() and hence I want to create this. But this just doesn't work. it adds nothing to the list. I even tried reading as an array and using Collections.addAll to put to a new ArrayList, but that also isn't working. When I tried to convert after reading as an array to ArrayList that is giving me other errors. I also tried reading as an integer variable and adding into the list without directly inserting. That gives me just one element inserted. I don't know why.

Edit: It was an error in inputting from my part. It's solved.

6
  • You really need to learn basic debugging techniques, like stepping through your code in a debugger. There's an obvious error in the code that really needs the programmer to be able to read code and execute it in their heads, or you're just going to have too difficult of a time coding anything at all. Commented Mar 14, 2020 at 20:06
  • please tell me! Commented Mar 14, 2020 at 20:07
  • 1
    @markspace maybe I'm just tired but I don't see an obvious error (except for the missing closing brace, but that's probably a copy-paste error since OP says it runs). Commented Mar 14, 2020 at 20:10
  • Well if they're trying read one array, they're looping twice. I don't know if that's an error or if they haven't described they're problem properly. @FedericoklezCulloca Commented Mar 14, 2020 at 20:12
  • 1
    @AkhilaRajeev I'm not sure what the problem is. I ran this on my machine and it works as I expect it to (input a number for the outer loop, a number for the mid loop, n numbers for the inner loop, then it prints the n numbers). See imgur.com/Qx6SCupl.png Commented Mar 14, 2020 at 20:16

1 Answer 1

1

By using scan.nextLine() you can get multiple numbers in the same line, instead of inputting them one-by-one by calling, scan.nextInt(). This is showcased in the code below:

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

public class Main {

    public static Integer[] convertToIntArray(String[] arr) {
        int index = 0;
        Integer[] numbers = new Integer[arr.length];

        for (int i = 0; i < arr.length; ++i) {
            try {
                numbers[index] = Integer.parseInt(arr[i]);
                ++index;
            } catch (NumberFormatException nfe) {
                // This part skips invalid input, which is not a number.
                // by not saving the skipped element to the array.
            }
        }
        return numbers;
    }

    public static void main(String[] args) {
        // Get the line containing numbers from the user.
        System.out.println("Input numbers: ");
        Scanner scan = new Scanner(System.in);
        String s = scan.nextLine();
        // Split sentence into words.
        String[] arr = s.split("\\W+");
        // Convert string array of numbers to int.
        Integer[] numbers = convertToIntArray(arr);
        System.out.println(Arrays.toString(numbers));
    }
}

When run with an input of 10 numbers this outputs:

Input numbers: 
1 2 3 4 5 6 7 8 9 10
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Sign up to request clarification or add additional context in comments.

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.