Results 1 to 4 of 4

Thread: Little help

  1. #1
    spinter is offline Member
    Join Date
    Oct 2011
    Posts
    18
    Rep Power
    0

    Default Little help

    well, i have that, my request is to read only the numbers of a file, but i stuck in the exception, i can manage the exc but i dont know how to do when in the reading of the file there are a word and excp work, because i want that program dont finish and continue reading until the end.

    Eg:

    " 5 March of 2012" i want to obtain only 5 and 12 but when i try to pass March whtth Integer.valueOf ( i need onl numbers because i must use them for things after)the exception go and dont read 2012 i want read 2012 too. but only 5 an 2012


    Java Code:
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.StringTokenizer;
    
    
    public class Open {
        private static ArrayList<String> cadFile = new ArrayList<String>(); 
        private static ArrayList<Integer> numInt = new ArrayList<Integer>(); 
        /**
         * @param args the command line arguments
         */
         public static void main(String[] args)  {
            // TODO code application logic here
            File f = null;
    	javax.swing.JFileChooser cuadro = new javax.swing.JFileChooser();
            cuadro.showOpenDialog(cuadro);
            String path = cuadro.getSelectedFile().getAbsolutePath();
            FileReader fileReader = null;
            BufferedReader bufferedReader = null;
            f= new File(path);
            try{
                fileReader = new FileReader(f);
                bufferedReader = new BufferedReader(fileReader);
                String L;
                while((L = bufferedReader.readLine()) != null) {
                    cadFile.add(L); 
                }
                for(int j = 0; j<cadFile.size(); j++) {
                    String lineaArchivo = cadFile.get(j);
                    StringTokenizer line = new StringTokenizer(lineaArchivo); 
                    int numeroElementosLinea = line.countTokens(); 
                    for(int k = 0; k<numeroElementosLinea; k++) {
                        numInt.add(Integer.parseInt(line.nextToken())); 
                    }
                }
                System.out.println(numInt); 
            }
            catch(Exception e){
            }
            finally{
                try{
                    if(fileReader != null) {
                        fileReader.close();
                    }
                }
                catch(Exception e2){
                }
            }
        }
    }

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: Little help

    " 5 March of 2012" i want to obtain only 5 and 12 but when i try to pass March whtth Integer.valueOf ( i need onl numbers because i must use them for things after)the exception go and dont read 2012 i want read 2012 too.
    I'm not exactly sure what you are saying. Do you mean that you want to read all of the ints on each line and ignore things that are not ints?

    In that case you could put a try/catch around the parseInt() call.

    Java Code:
    int numeroElementosLinea = line.countTokens(); 
    for(int k = 0; k<numeroElementosLinea; k++) {
        try {
            numInt.add(Integer.parseInt(line.nextToken())); 
        } catch(NumberFormatException nfe) {
            // ignore tokens that are not ints
        } 
    }
    An alternative would be to test the tokens to see if they were ints.

    -----

    Maybe I'm not interpreting your question properly. If so it would be a good idea to construct a small example that does not have all the files in it. Hard code the data (say as a String, then you can use a StringReader) and say exactly what output you expect to get for that data.

  3. #3
    spinter is offline Member
    Join Date
    Oct 2011
    Posts
    18
    Rep Power
    0

    Default Re: Little help

    thxxxx its that all works now thx a lot, exceptions are one of my weakest points in java

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •