2

I have a java problem. I am trying to read a txt file which has a variable number of integers per line, and for each line I need to sum every second integer! I am using scanner to read integers, but can't work out when a line is done. Can anyone help pls?

skaffman
  • 398,947
  • 96
  • 818
  • 769
SB1
  • 21
  • 2

4 Answers4

2

have a look at the BufferedReader class for reading a textfile and at the StringTokenizer class for splitting each line into strings.

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
}
clamp
  • 33,000
  • 75
  • 203
  • 299
0

If I were you, I'd probably use FileUtils class from Apache Commons IO. The method readLines(File file) returns a List of Strings, one for each line. Then you can simply handle one line at a time.

Something like this:

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

(Unfortunately Commons IO doesn't support generics, so the there would be an unchecked assignment warning when assigning to List<String>. To remedy that use either @SuppressWarnings, or just an untyped List and casting to Strings.)

This is, perhaps, an example of a situation where one can apply "know and use the libraries" and skip writing some lower-level boilerplate code altogether.

Community
  • 1
  • 1
Jonik
  • 80,077
  • 70
  • 264
  • 372
  • this is at the expense of incurring the cost of including the jar lib... though i do agree if you can you should try to use public domains libraries. – zeroin23 Oct 25 '09 at 14:17
  • Yes, if this was the *only* thing you'd need from Commons IO, perhaps it wouldn't be worth it. But typically Apache Commons libs (and e.g. Google Collections) have lots of stuff useful for your project. – Jonik Oct 25 '09 at 20:19
0

or scrape from commons the essentials to both learn good technique and skip the jar:

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);       
           }

   }
}   
alain
  • 1
0

This is the solution that 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");
        }

    }

}
Adrian S.
  • 1
  • 1