arraylist - Distinguishing between the different data types in Java -


i have text file contains following:

hello 1 2 3 4 5.6 lol 23.5 34.6 23 456 rofl. 

i wrote down code in java read contents of text file , distinguish between 3 data types.i used try catch statements , code works(kinda). problem converts whole numbers doubles well. example following code outputting:

list of integers in textfile: [1, 2, 3, 4, 23, 456]  list of doubles in textfile: [1.0, 2.0, 3.0, 4.0, 5.6, 23.5, 34.6, 23.0, 456.0]  list of strings in textfile: [hello, 5.6, lol, 23.5, 34.6, rofl] 

i want prevent happening. suggestions appreciated.

    arraylist<integer> data_int=new arraylist<integer>();     arraylist<string> data_string=new arraylist<string>();     arraylist<double> data_double=new arraylist<double>();       while(file.hasnext())     {         string s=file.next();         system.out.println(s);          try         {             integer.parseint(s);             data_int.add(integer.parseint(s));          }         catch(numberformatexception e)         {           data_string.add(s);         }          try         {               double.parsedouble(s);               data_double.add(double.parsedouble(s));           }         catch(numberformatexception e)         {          }      }     system.out.println("list of integers in textfile: "+data_int);     system.out.println("list of doubles in textfile: "+data_double);     system.out.println("list of strings in textfile: "+data_string); 

put double check in catch block integer check

try {   integer.parseint(s);   data_int.add(integer.parseint(s)); } catch(numberformatexception e) {   try {     double.parsedouble(s);     data_double.add(double.parsedouble(s));   } catch(numberformatexception e) {     data_string.add(s);   } } 

Comments

Popular posts from this blog

Java 3D LWJGL collision -

spring - SubProtocolWebSocketHandler - No handlers -

methods - python can't use function in submodule -