GamerBhai02

9. Multiply Numbers

Jan 16th, 2025
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.07 KB | Source Code | 0 0
  1. import java.io.*;
  2. public class MultiplyNumbersInFile {
  3.     public static void main(String[] args) {
  4.         String InputFilePath = "C:\\Users\\Abu Talha\\Desktop\\input.txt";
  5.         String OutputFilePath = "C:\\Users\\Abu Talha\\Desktop\\output.txt";
  6.         try (BufferedReader reader = new BufferedReader(new FileReader(InputFilePath));BufferedWriter writer = new BufferedWriter(new FileWriter(OutputFilePath))){
  7.             String line;
  8.             while((line=reader.readLine())!=null){
  9.                 String[] words = line.split("\\s+");
  10.                 for(String value : words){
  11.                     try{
  12.                         double number = Double.parseDouble(value);
  13.                         double multipliedValue = number*2;
  14.                         writer.write(multipliedValue+" ");
  15.                     }catch(NumberFormatException e){
  16.                         writer.write(value+" ");
  17.                     }
  18.                 }
  19.                 writer.newLine();
  20.             }
  21.         }catch(IOException e){
  22.             e.printStackTrace();
  23.         }
  24.     }
  25. }
Tags: multiply
Add Comment
Please, Sign In to add comment