1

When I input a string of 4500 characters followed by a newline feed in Java console, it displays input line is too long runtime error in the terminal.

I have tried using both Scanner and BufferedReader, but no success.

Suhaib Janjua
  • 3,538
  • 16
  • 59
  • 73

1 Answers1

1

My advice when working with big strings is to use StringBuilder Class, here is an example:

import java.util.*;
public class Main {
    public static void main(String[] args) {
        String someString;
        Scanner scanner = new Scanner(System.in);

        StringBuilder builder = new StringBuilder();
        builder.append(scanner.nextLine());
        someString = builder.toString();
        System.out.println(someString);

    }
}
Shota
  • 6,910
  • 9
  • 37
  • 67
  • What if input contains continuous spaces or tabulators? `next()` will ignore them. – Pshemo Apr 11 '15 at 17:26
  • Sorry I didn't get your question firstly, if you want not to ignore them you should change scanner.next() with scanner.nextLine() – Shota Apr 11 '15 at 17:38
  • StringBuilder builder = new StringBuilder(); //sumit = ab.nextLine(); for (int jc=0;jc –  Apr 11 '15 at 17:38
  • i dont have any spaces in between –  Apr 11 '15 at 17:39