Java file reading problem

I have a problem with java. I am trying to read a txt file that has a variable number of integers per line and for each line I need to sum every second integer! I am using a scanner to read integers but cannot work when a string is executed. Can anyone pls help?

+2


source to share


4 answers


look at BufferedReader class for reading text file and StringTokenizer for splitting each line into lines.



String input;
BufferedReader br = new BufferedReader(new FileReader("foo.txt"));
while ((input = br.readLine()) != null) {
  input = input.trim();
  StringTokenizer str = new StringTokenizer(input);
  String text = str.nextToken(); //get your integers from this string
}

      

+2


source


If I were you, I would use the FileUtils class from Apache Commons IO . The method readLines(File file)

returns a list of strings, one for each string. Then you can just process one line at a time.

Something like that:

    File file = new File("test.txt");
    List<String> lines = FileUtils.readLines(file);
    for (String line : lines) {
        // handle one line
    }

      



(Unfortunately Commons IO does not support generics, so there would be an unchecked task warning when assigning List <String>. To fix this, use either @SuppressWarnings or just an untyped list and cast for strings.) Sub>

This is perhaps an example of a situation in which you can apply " know and use libraries " and skip the lower-level code altogether.

0


source


or to clear out common things to learn a good technique and skip the can:

import java.io.*;
import java.util.*;

class Test
{
    public static void main(final String[] args) 
    {

       File file = new File("Test.java"); 

       BufferedReader buffreader = null;
       String line = "";

        ArrayList<String> list = new ArrayList<String>(); 

        try 
            {
                buffreader = new BufferedReader( new FileReader(file) );
                line = buffreader.readLine();
                while (line != null) 
                {
                   line = buffreader.readLine();
                   //do something with line or:
                   list.add(line);
                }
             } catch (IOException ioe) 
             {
                    // ignore
             } finally 
             {
               try 
               {
                  if (buffreader != null) 
                  {
                     buffreader.close();
                  }
               } catch (IOException ioe) 
               {
                    // ignore
               }

            }

           //do something with list
           for (String text : list) 
           {       
               // handle one line
               System.out.println(text);       
           }

   }
}   

      

0


source


This is the solution I would use.

import java.util.ArrayList;
import java.util.Scanner;
public class Solution1 {

    public static void main(String[] args) throws IOException{
        String nameFile;
        File file;
        Scanner keyboard = new Scanner(System.in);
        int total = 0;

        System.out.println("What is the name of the file");

        nameFile = keyboard.nextLine();
        file = new File(nameFile);

        if(!file.exists()){
            System.out.println("File does not exit");   
            System.exit(0);
        }


        Scanner reader = new Scanner(file);
        while(reader.hasNext()){
            String fileData = reader.nextLine();
            for(int i = 0; i < fileData.length(); i++){
                if(Character.isDigit(fileData.charAt(i))){
                    total = total + Integer.parseInt(fileData.charAt(i)+"");
                }

            }
            System.out.println(total + " \n");
        }

    }

}

      

0


source







All Articles