java - Same code giving runtime error if i am using bufferedreader instead of scanner -


class ideone {     public static void main (string[] args) throws java.lang.exception     {//biginteger bi1, bi2, bi3;       long t,j,n;     int i,x;      biginteger u,sum,temp,m;     biginteger[] a=new biginteger[100009];  long[] b=new long[100009];     long mm=1000000007,f;         scanner har=new scanner(system.in);         bufferedreader br = new bufferedreader(new inputstreamreader(system.in));                     t=har.nextint();         for(f=0;f<t;f++)         {   temp = biginteger.valueof(1);   sum = biginteger.valueof(0);    u = biginteger.valueof(0);                                n=har.nextint();    x=har.nextint(); m=har.nextbiginteger();//string line = br.readline();     //string line = br.readline(); // read multiple integers line         //string[] strs = line.trim().split("\\s+");  //string[] s1 = br.readline().split(" ");  //stringtokenizer st = new stringtokenizer(br.readline());                 for(i=1;i<=n;i++)                 {b[i]=har.nextint();                  // b[i] = integer.parseint(st.nexttoken()); //b[i] = long.parselong(system.console().readline());     //b[i]=long.parselong(s1[i-1]);                 }         }     } }  input of form  t n x m a[1] a[2] a[3] .......a[n] 

this code running correctly scanner if try use buffered reader or string tokenizer giving runtime error . new java , need use big integer further part of question.

there nothing wrong code. defining bufferreader bufferedreader br = new bufferedreader(new inputstreamreader(system.in)); , reading string br.readline();.

this snippet works absolutely fine , prints input string console:

public static void main (string[] args) {     bufferedreader br = new bufferedreader(new inputstreamreader(system.in));     system.out.println(br.readline()); } 

some of possibilities can errors:

  1. stringtokenizer

    stringtokenizer st = new stringtokenizer(br.readline()); 

    before passing stringtokenizer make sure string br.readline().

    string text = br.readline(); if(!stringutils.isempty(text)) {     stringtokenizer st = new stringtokenizer(br.readline());     /* rest of code */ } 
  2. loop condition variable

    for(i=1;i<=n;i++) 

    i not see bufferedreader initializes n. make sure initialize correctly.

here quick code snippet:

public static void main (string[] args) throws exception {     long j, n, f, mm = 1000000007;     int i, x;     biginteger u, sum, temp, m;     biginteger[] = new biginteger[100009];  long[] b=new long[100009];      scanner har = new scanner(system.in);      long t = har.nextint();     for(f=0;f<t;f++)     {            temp = biginteger.valueof(1);            sum = biginteger.valueof(0);             u = biginteger.valueof(0);         n = har.nextint();             x = har.nextint();          m = har.nextbiginteger();          /* goto next line */         har.nextline();          /* start reading line */         string text = har.nextline();         system.out.println("string value: " + text);         if(null != text) {             stringtokenizer st = new stringtokenizer(text);             for(i = 1; <= n; i++)             {                 b[i] = integer.parseint(st.nexttoken());                 system.out.println("value of b[" + + "] = " + b[i]);             }         }     } } 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -