4 Replies Latest reply: Feb 25, 2008 8:45 AM by 807601 RSS

    How can i cast array of string to int ?

    807601
                         while(st.hasMoreTokens()){
                                 
                                    test[count] = st.nextToken();
                                    count++;
      
                            }
      Since token deal with string, i have to pass them into String of array, but how can i convert all of them to int to perform arithmetic operation?
        • 1. Re: How can i cast array of string to int ?
          807601
          http://stuff.mit.edu/people/ele/Javaconversion.htm
          • 2. Re: How can i cast array of string to int ?
            807601
                 public static void main (String[] args) throws IOException{ 
                      
                       DataInputStream dis = null; 
                         String dbRecord = null;
                 
                         int tokenCount = 0;
                         int numOfQuestion = 0;
                         
                         int questionnAireNum = 0;
                         int postCode = 0;
                         int age = 0;
                         int gender = 0;
                         
                         String [] response = new String[10];
                      
            
                            File f = new File("polldata.txt"); 
                            FileInputStream fis = new FileInputStream(f);  
                            BufferedInputStream bis = new BufferedInputStream(fis);  
                            dis = new DataInputStream(bis);
            
                            // read the first record of the database 
                            while ( (dbRecord = dis.readLine()) != null) {
                                 
                               StringTokenizer st = new StringTokenizer(dbRecord, ","); 
                               
                               tokenCount = st.countTokens();
                               
                               numOfQuestion = tokenCount-4;
                               
                               String rquestionNum = st.nextToken(); 
                               questionnAireNum = Integer.parseInt(rquestionNum);
                               
                               String rpostCode = st.nextToken(); 
                               postCode = Integer.parseInt(rpostCode);
                              
                               String rAge  = st.nextToken(); 
                               age = Integer.parseInt(rAge);
                               
                               String rGender = st.nextToken();
                               gender = Integer.parseInt(rGender);
                               
                               for(int i=0; i<numOfQuestion;i++){
                                    response[i] = st.nextToken();
                               }
            hi how come when i cast the string as int it prompt me error as shown below ? I wonder what causes this because this is normally how i cast string to int, somehow it won work this way with token.

            Exception in thread "main" java.lang.NumberFormatException: For input string: " 3"
                 at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
                 at java.lang.Integer.parseInt(Integer.java:447)
                 at java.lang.Integer.valueOf(Integer.java:553)
                 at test.main(test.java:43)
            • 3. Re: How can i cast array of string to int ?
              807601
              You are parsing a String into an int, not casting it. Please don't confuse those two terms.

              Look at the exception. The input is " 3" (notice the leading space). You might want to use .trim() on your String before parsing it.
              • 4. Re: How can i cast array of string to int ?
                807601
                JoachimSauer wrote:
                You are parsing a String into an int, not casting it. Please don't confuse those two terms.

                Look at the exception. The input is " 3" (notice the leading space). You might want to use .trim() on your String before parsing it.
                ok i got it thak you