title: java.lang.NumberFormatException: For input string: "23 "
body: <p>I gave 23 (looks like a proper string) as input, but stil get NumberFormatException. Please point out where I went wrong.</p>
  
  <p>PS I was trying to solve "chef and strings problem" on codechef </p>
  
  <p>Relevent code:</p>
  
  <pre><code>Scanner cin=new Scanner(System.in);
        cin.useDelimiter("\n");
        String data=cin.next();
        System.out.println(data);
  
        /*
         * @param Q no. of chef's requests
         */
        String tempStr=cin.next();
        System.out.println(tempStr);;
        int Q = Integer.parseInt(tempStr);
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>sdfgsdg
  sdfgsdg
  
  23
  23
  
  Exception in thread "main" java.lang.NumberFormatException: For input string: "23
  "
      at java.lang.NumberFormatException.forInputString(Unknown Source)
      at java.lang.Integer.parseInt(Unknown Source)
      at Chef.RegexTestHarness.main(RegexTestHarness.java:24)
  </code></pre>
  
  <p>Complete program:</p>
  
  <pre><code>package Chef;
  //
  //TODO codechef constraints
  import java.util.Scanner;
  import java.lang.*;
  import java.io.*;
  
  //TODO RETURN TYPE
  class RegexTestHarness
  {
    public static void main(String args[])
    { 
  
        Scanner cin=new Scanner(System.in);
        cin.useDelimiter("\n");
        String data=cin.next();
        System.out.println(data);
  
        /*
         * @param Q no. of chef's requests
         */
        String tempStr=cin.next();
        System.out.println(tempStr);;
        int Q = Integer.parseInt(tempStr);
  
        for(int i=0; i&lt;Q; i++)
        {
            /*
             * @param s chef's request
             */
             String s= cin.next();//request
  
  //void getParam() (returning multiple parameters problem)
     //{a b L R
     //where a:start letter
     //b: end lettert
      //L: minStartIndex
      //L&lt;=S[i]&lt;=E[i]&lt;=R
             //R is the maxEndIndex
  
  //TODO transfer to main
  
      char a=s.charAt(0);
      char b=s.charAt(3);
      int L=0, R=0;
      /*
       * @param indexOfR in the request string s, we separate R (which is maxEndIndex of chef's
       * good string inside data string)
       * . To do that, we first need the index of R itself in request string s 
       */
      int indexOfR= s.indexOf(" ", 5) +1;
      System.out.println("indexOfR is:" + s.indexOf(" ", 5));
  
      L= Integer.parseInt( s.substring(5, indexOfR - 2) );
      //TODO check if R,L&lt;10^6
  
      R=Integer.parseInt( s.substring(indexOfR) );
  
      //}  ( end getparam() )
    //-----------------------------------
      //now we have a b L R
  
      //String good="";
      //TODO add other constraints (like L&lt;si.....) here
      if(a !=b)
      {   int startInd=data.indexOf(a, L), endInd=data.lastIndexOf(b, R);
      int output=0, temp;
  
      while((startInd&lt;endInd)&amp;&amp;(startInd != (-1) ) &amp;&amp; ( endInd != (-1) ))
          {
  
            temp = endInd;
              while((startInd&lt;endInd))
              {
  
  
              //good= good+ s.substring(startInd, endInd);
              output++;
  
  
              endInd=data.lastIndexOf(b, endInd);
              }
              startInd=data.indexOf(a, startInd);
              //TODO if i comment the line below, eclipse says tat the variable temp 
              //(declared at line 68) is not used. Whereas it is used at 68 
              //(and 83, the line below) 
              endInd=temp;
  
          }
      System.out.println(output);
  
      }
  
  
  
  
  
        }//end for
  
  
    cin.close();
    }
  
  
  }
  </code></pre>