Java Code to read 3 NUMBERS from each line from a text file into a 2 dimensional
ID: 3574978 • Letter: J
Question
Java Code to read 3 NUMBERS from each line from a text file into a 2 dimensional array and output the multipication value of each line into a new text file
EXAMPLE
INPUT TEXT FILE
1 2 5
3 1 5
...........(Text File should be theoretically infinite)
OUTPUT TEXT FILE
Area for Line 1 is = 10
Area for Line 2 is = 15
.........(Until the Area of the Last line in the text file has been determined)
This should be implemented in a menu driven system LOOP eg
1) Prompt User To Enter Filename
2) Exit Loop
............
Explanation / Answer
Java Program:
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Scanner;
public class Multiplier {
public static void main(String args[]) throws IOException {
int choice;
// Creating a Scanner instance to take inputs from the user
Scanner scanner = new Scanner(System.in);
System.out.println("1) Prompt User To Enter Filename 2) Exit Loop ");
choice = scanner.nextInt();
do {
System.out.println("Provide the path of the input file: ");
// Input the filename from user
String filename = scanner.next();
FileInputStream fis = null;
BufferedReader reader = null;
try {
fis = new FileInputStream(filename);
reader = new BufferedReader(new InputStreamReader(fis));
} catch (FileNotFoundException e) {
throw new RuntimeException(
"Did not find the input file. Please provide a valid path name for the input file");
}
PrintWriter out;
System.out.println("Provide the path of the output file: ");
// Input the output filename from user
filename = scanner.next();
out = new PrintWriter(filename);
String line = null;
String[] values;
// Reading each line of file
while ((line = reader.readLine()) != null) {
// Split each line to get values
values = line.split(" ");
int sum = 1;
for (int i = 0; i < values.length; i++) {
// Convert string to int and perform multiplication
sum = sum * Integer.parseInt(values[i]);
}
// Write sum to the output file
out.println(sum);
}
fis.close();
reader.close();
out.close();
System.out
.println("1) Prompt User To Enter Filename 2) Exit Loop ");
choice = scanner.nextInt();
} while (choice != 2);
scanner.close();
}
}
--------------------------------------------------------------------------------------------------------------------
Sample Output:
1) Prompt User To Enter Filename
2) Exit Loop
1
Provide the path of the input file:
C:\test\input.txt
Provide the path of the output file:
C:\test\output.txt
1) Prompt User To Enter Filename
2) Exit Loop
2
----------------------------------------------------------------------------------------------------
input.txt:
55 2 33
1 2 3
4 6 10
10 10 10
1 2 -3
8 2 0
output.txt:
3630
6
240
1000
-6
0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.