Home > front end >  Max limit on number of input in vs code java
Max limit on number of input in vs code java

Time:11-04

I am trying some algorithm which requires large no of input samples for testing but at a time I cannot take input more than certain number of times.

N = sc.nextInt();
...
int[] arr = new int[N];

for(int i=0; i<N; i  ){
  arr[i] = sc.nextInt();
}

for(int elem: arr){
  System.out.println(elem " "); 
}

Input format is

N 
//HERE GOES ARRAY ELEMENTS

where N- no of element in array

I'm using this user input test_case_1, but I can only input fraction of the given values.
I wanted to know what is restricting the number of input in vscode

CodePudding user response:

Usually, using a scanner is perfectly alright. But with input samples of up to 90 000, which seems to be test case 1, it might be very slow due to excessive flushing.

Something like this might be more effective:

BufferedReader br = new BufferedReader(new FileReader("temp_code_input.txt"));
...
int N = Integer.parseInt(br.readLine());
...
StringTokenizer st = new StringTokenizer(br.readLine());
/* 
Assumes every input is on the same line. If not, create a new StingTokenizer
for each new line of input.
*/
int[] arr = new int[N];
for (int i = 0; i < N; i  ) {
  arr[i] = Integer.parseInt(st.nextToken());
}

for (int elem : arr) {
  System.out.println(elem)
}

CodePudding user response:

I'm just manually inputting the values by pasting it

It would be easier for you to just read this input from a file with the Scanner class.

      String s = Files.readString("PATH_TO_YOUR_FILE", StandardCharsets.US_ASCII);

      // create a new scanner with the specified String Object
      Scanner scanner = new Scanner(s);

      // find the next int token and print it
      // loop for the whole scanner
      while (scanner.hasNext()) {

         // if the next is a int, print found and the int
         if (scanner.hasNextInt()) {
            //Store the data here ...
         }
         // if no int is found, print "Not Found:" and the token
         System.out.println("Not Found :"   scanner.next());
      }
  • Related