Box
Box

Reputation: 113

How to separate data types from a text file?

This is the code I have Implemented but I am getting multiple arrays of data in a incremental order but I need single arrays of data. How do I approach that ? Also is it ok to have try catch inside catch block ?

  Text File:
  The 1493 fox 0.4 -6.382 jumped -832722 0 1 9E-21 162 over the dog!

Eg: Double values are printed each time data is added to it.

   @SuppressWarnings("resource")
    Scanner s= new Scanner(new File("src/inputs.txt")).useDelimiter("\\s+");

    ArrayList<Long> itr= new ArrayList<Long>();
    ArrayList<Double> dub = new ArrayList<Double>();
    ArrayList<String> str = new ArrayList<String>();

    while(s.hasNext())
    {
        String str1=s.next();
    try
        {   
        long b=Long.parseLong(str1);
        itr.add(b);
        System.out.println("Integer values are ::"+itr);    
        }
    catch(NumberFormatException e)
     {
      try
        {   
        double b1=Double.parseDouble(str1);
        dub.add(b1);
        System.out.println("Double values are ::"+dub); 
        }
       catch(NumberFormatException e1)
        {
           String b2 = (String) str1;
           str.add(b2);
           System.out.println("String Values are"+str);
        }
     }
   }
 }}

Expected output :

  Integer values are ::[1493, -832722, 0, 1]
  Double values are ::[0.4, -6.382, 9.0E-21]
  String Values are[The, fox, jumped, over, the, dog!]

Upvotes: 1

Views: 238

Answers (2)

Aniket Paul
Aniket Paul

Reputation: 233

As @RubioRic answered, move the SOP statements outside the while loop to get the desired output.

As to other ways to get the data types, I feel your implementation is good enough and widely used. But if you want to do it in an another way then try using regex patterns to validate the string and determine the data type (not reliable) OR use the Scanner class API to determine the data type like below.

 @SuppressWarnings("resource")
        Scanner s= new Scanner(new File("src/inputs.txt")).useDelimiter("\\s+");

        ArrayList<Long> itr= new ArrayList<Long>();
        ArrayList<Double> dub = new ArrayList<Double>();
        ArrayList<String> str = new ArrayList<String>();

        while(s.hasNext())
        {
            if(s.hasNextLong()){
                 itr.add(s.nextLong());
            }
            else if(s.hasNextDouble()){
                 dub.add(s.nextDouble());
            }
            else{
                 str.add(s.next());
             }  
        }
        s.close();
 System.out.println("Long values are ::" + itr);
 System.out.println("Double values are ::" + dub);     
 System.out.println("String Values are" + str);

Upvotes: 4

RubioRic
RubioRic

Reputation: 2468

Just move the printing outside the loop. Got no better solution without using try-catch, sorry.

@SuppressWarnings("resource")
Scanner s= new Scanner(new File("src/inputs.txt")).useDelimiter("\\s+");

ArrayList<Long> itr   = new ArrayList<Long>();
ArrayList<Double> dub = new ArrayList<Double>();
ArrayList<String> str = new ArrayList<String>();

while(s.hasNext()) {
    String str1=s.next();

    try {   
        long b=Long.parseLong(str1);
        itr.add(b);

    } catch(NumberFormatException e) {

      try {   
         double b1=Double.parseDouble(str1);
         dub.add(b1);
      } catch(NumberFormatException e1) {
        String b2 = (String) str1;
        str.add(b2);
      }
    }
 }

 System.out.println("Integer values are ::" + itr);
 System.out.println("Double values are ::" + dub);     
 System.out.println("String Values are" + str);

Upvotes: 1

Related Questions