Search code examples
textjava.util.scannerbufferedreader

Can I multiply the numbers from a text file?


I have a text file that looks like this
1 2 3 4
5 6 7 8
Is there a way that I can multiply all these numbers together as is? Or will I have to move them all onto separate lines? I know the basics of BufferedReader and Scanner but I'm not sure if this is possible. What I'm trying to do is multiply a list of prime numbers that I downloaded from online. Here's what I have now (updated)

import java.io.*;    
public class BufferReader {   
public static void main(String[] args) {

   String delete = "[ ]+";
   BufferedReader br = null;

   try{ 
       br = new BufferedReader(new FileReader("test.txt"));     

       System.out.println("Reading the file using readLine() method:");
   String contentLine = br.readLine();
   while (contentLine != null) {
      System.out.println(contentLine);
      contentLine = br.readLine();
      String show [] = contentLine.split(delete);
   }



   } 
   catch (IOException ioe) 
   {
   ioe.printStackTrace();
   } 
   finally 
   {
   try {
      if (br != null)
     br.close();

   } 
   catch (IOException ioe) 
       {
    System.out.println("Error in closing the BufferedReader");
   }
}
 }
}

Solution

  • Try the following code.

    package eu.webfarmr;
    
    import java.io.ByteArrayOutputStream;
    import java.io.FileInputStream;
    
    public class NumberReader {
        public static void main(String[] args) throws Exception {
            // 1. Determine the input location
            FileInputStream fis = new FileInputStream("c:/temp/file.txt");
    
            // 2. Read the contents of the file into a single string
            byte[] contentBytes = new byte[255];
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            while (fis.read(contentBytes)!=-1){
                bos.write(contentBytes);
            }
            fis.close();
            String contents = bos.toString();
            System.out.println(contents);
    
            // 3. Parse the string and split according to the system's endline
            String[] lines = contents.split(System.getProperty("line.separator"));
            int product = 1;
            for (String line : lines){
                String[] numbers = line.trim().split(" ");
                for (String number : numbers){
                    System.out.println("number read: "+number);
                    Integer convertedNumber = Integer.parseInt(number.trim());
                    product = product * convertedNumber;
                    System.out.println("new product: "+product);
                }
            }
    
        }
    }
    

    I ran the code on your input and got:

    1 2 3 4
    5 6 7 8
    number read: 1
    new product: 1
    number read: 2
    new product: 2
    number read: 3
    new product: 6
    number read: 4
    new product: 24
    number read: 5
    new product: 120
    number read: 6
    new product: 720
    number read: 7
    new product: 5040
    number read: 8
    new product: 40320