0% found this document useful (0 votes)
4 views7 pages

KNA-7 InputFromFile

The document outlines a Java program named CalculatorApp that reads two numbers from a file and performs basic arithmetic operations. It includes steps for package declaration, file handling, reading numbers, creating a Calculator object, and performing calculations while handling potential errors like file not found and division by zero. The Calculator class contains methods for addition, subtraction, multiplication, and division.

Uploaded by

shravanmb163
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views7 pages

KNA-7 InputFromFile

The document outlines a Java program named CalculatorApp that reads two numbers from a file and performs basic arithmetic operations. It includes steps for package declaration, file handling, reading numbers, creating a Calculator object, and performing calculations while handling potential errors like file not found and division by zero. The Calculator class contains methods for addition, subtraction, multiplication, and division.

Uploaded by

shravanmb163
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

7.

Java Program to read Numbers from a File and Perform Arithmetic Operations

Name: Praveen M Biradar


ID: KODD8KS8I
CalculatorApp.java
Calculator.java
Output:

Step-by-Step Explanation- CalculatorApp.java

1. Package Declaration

package UserGiven;

Defines the package UserGiven for organizing related Java files.

2. Import Statements

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

• File: Works with files.


• FileNotFoundException: Handles missing files.
• Scanner: Reads file content.

3. Main Class & Method

public class CalculatorApp {


public static void main(String[] args) {

• CalculatorApp is the main class.


• main() method is the entry point of execution.
4. File Handling

String filePath = "input_files/input.txt";

• Specifies the location of input.txt.

try {
File file = new File(filePath);
Scanner scanner = new Scanner(file);

• Opens and reads the file using Scanner.

5. Reading Numbers from File

if (!scanner.hasNextInt()) {
System.out.println("Error: input.txt is empty or missing numbers!");
scanner.close();
return;
}
int num1 = scanner.nextInt();

• Checks if the file has numbers before reading.

if (!scanner.hasNextInt()) {
System.out.println("Error: input.txt does not contain a second
number!");
scanner.close();
return;
}
int num2 = scanner.nextInt();
scanner.close();

• Ensures there are two numbers in the file.


6. Creating Calculator Object & Assigning Values

Calculator calculator = new Calculator();


calculator.num1 = num1;
calculator.num2 = num2;

• Initializes a Calculator object with num1 and num2.

7. Performing Calculations

System.out.println("Numbers: " + calculator.num1 + ", " +


calculator.num2);
System.out.println("Addition: " + calculator.add());
System.out.println("Subtraction: " + calculator.sub());
System.out.println("Multiplication: " + calculator.mul());

• Calls methods from Calculator for basic operations.

8. Handling Division

if (calculator.num1 != 0) {
System.out.println("Division: " + calculator.div());
} else {
System.out.println("Error: Division by zero is not allowed.");
}

• Prevents division by zero errors.

9. Handling File Not Found Errors

} catch (FileNotFoundException e) {
System.out.println("Error: File not found - " + filePath);
e.printStackTrace();
}

• Displays an error if input.txt is missing.

Explanation- Calculator.java

• num1 and num2: Stores the two numbers.


• add(): Returns the sum of num1 and num2.
• sub(): Returns the difference (num2 - num1).
• mul(): Returns the product of num1 and num2.
• div(): Returns num2 / num1 (assuming num1 is not zero).

You might also like