SlideShare a Scribd company logo
/*
* The java program that reads an input file input.txt
* and then reads the contens of file.
* the program throws file not found exception if file not found.
* The program throws DimensionMismatchException if mismatch of dimension.
* The program throws NumberFormatException if mismatch of data read.
* */
//FormatChecker.java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class FormatChecker {
public static void main(String[] args) {
String fileName="input.txt";
//calling checkFormat
checkFormat(fileName);
}
//Method checkFormat that takes string and check for
//errors otherwise reads the file and print to console
private static void checkFormat(String fileName) {
Scanner filescanner=null;
try {
//thorws FileNotFoundException error if file not found
filescanner=new Scanner(new File(fileName));
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
}
int rows=0;
int cols=0;
double[][] arr;
String dimensions[]=filescanner.nextLine().split(" ");
try
{
//thorws DimensionMismatchException error if mismatch of dimension
if(dimensions.length>2)
throw new DimensionMismatchException("Dimension mismatch");
} catch (DimensionMismatchException e1) {
e1.printStackTrace();
}
try {
//thorws NumberFormatException error if mismatch of reading values
rows=Integer.parseInt(dimensions[0]);
cols=Integer.parseInt(dimensions[1]);
arr=new double[rows][cols];
System.out.println("File contents");
for (int i = 0; i < arr.length; i++)
{
for (int j = 0; j < arr[i].length; j++)
{
arr[i][j]=filescanner.nextDouble();
System.out.printf("%6.1f",arr[i][j]);
}
System.out.println();
}
} catch (NumberFormatException e) {
System.out.println(e.getMessage());
}
}
}
------------------------------------------------------------------------
//User defined DimensionMismatchException class
//that extends the Exception class
//DimensionMismatchException.java
public class DimensionMismatchException extends Exception {
public DimensionMismatchException(String msg) {
super(msg);
}
}
------------------------------------------------------------------------
input.txt
5 6
2.5 0 1 0 0 0
0 -1 4 0 0 0
0 0 0 0 1.1 0
0 2 0 0 5 0
0 -3.14 0 0 0 0
------------------------------------------------------------------------
File contents
2.5 0.0 1.0 0.0 0.0 0.0
0.0 -1.0 4.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 1.1 0.0
0.0 2.0 0.0 0.0 5.0 0.0
0.0 -3.1 0.0 0.0 0.0 0.0
Note : DimensionMismatchException class is included
Solution
/*
* The java program that reads an input file input.txt
* and then reads the contens of file.
* the program throws file not found exception if file not found.
* The program throws DimensionMismatchException if mismatch of dimension.
* The program throws NumberFormatException if mismatch of data read.
* */
//FormatChecker.java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class FormatChecker {
public static void main(String[] args) {
String fileName="input.txt";
//calling checkFormat
checkFormat(fileName);
}
//Method checkFormat that takes string and check for
//errors otherwise reads the file and print to console
private static void checkFormat(String fileName) {
Scanner filescanner=null;
try {
//thorws FileNotFoundException error if file not found
filescanner=new Scanner(new File(fileName));
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
}
int rows=0;
int cols=0;
double[][] arr;
String dimensions[]=filescanner.nextLine().split(" ");
try
{
//thorws DimensionMismatchException error if mismatch of dimension
if(dimensions.length>2)
throw new DimensionMismatchException("Dimension mismatch");
} catch (DimensionMismatchException e1) {
e1.printStackTrace();
}
try {
//thorws NumberFormatException error if mismatch of reading values
rows=Integer.parseInt(dimensions[0]);
cols=Integer.parseInt(dimensions[1]);
arr=new double[rows][cols];
System.out.println("File contents");
for (int i = 0; i < arr.length; i++)
{
for (int j = 0; j < arr[i].length; j++)
{
arr[i][j]=filescanner.nextDouble();
System.out.printf("%6.1f",arr[i][j]);
}
System.out.println();
}
} catch (NumberFormatException e) {
System.out.println(e.getMessage());
}
}
}
------------------------------------------------------------------------
//User defined DimensionMismatchException class
//that extends the Exception class
//DimensionMismatchException.java
public class DimensionMismatchException extends Exception {
public DimensionMismatchException(String msg) {
super(msg);
}
}
------------------------------------------------------------------------
input.txt
5 6
2.5 0 1 0 0 0
0 -1 4 0 0 0
0 0 0 0 1.1 0
0 2 0 0 5 0
0 -3.14 0 0 0 0
------------------------------------------------------------------------
File contents
2.5 0.0 1.0 0.0 0.0 0.0
0.0 -1.0 4.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 1.1 0.0
0.0 2.0 0.0 0.0 5.0 0.0
0.0 -3.1 0.0 0.0 0.0 0.0
Note : DimensionMismatchException class is included

More Related Content

Similar to The java program that reads an input file input.txt an.pdf

Exception handling
Exception handlingException handling
Exception handling
Sandeep Rawat
 
Basic input-output-v.1.1
Basic input-output-v.1.1Basic input-output-v.1.1
Basic input-output-v.1.1
BG Java EE Course
 
Java 3 Computer Science.pptx
Java 3 Computer Science.pptxJava 3 Computer Science.pptx
Java 3 Computer Science.pptx
MUHAMMED MASHAHIL PUKKUNNUMMAL
 
IO and threads Java
IO and threads JavaIO and threads Java
IO and threads Java
MUHAMMED MASHAHIL PUKKUNNUMMAL
 
For the following I have finished most of this question but am havin.pdf
For the following I have finished most of this question but am havin.pdfFor the following I have finished most of this question but am havin.pdf
For the following I have finished most of this question but am havin.pdf
arihantplastictanksh
 
Change the code in Writer.java only to get it working. Must contain .pdf
Change the code in Writer.java only to get it working. Must contain .pdfChange the code in Writer.java only to get it working. Must contain .pdf
Change the code in Writer.java only to get it working. Must contain .pdf
secunderbadtirumalgi
 
Session 23 - JDBC
Session 23 - JDBCSession 23 - JDBC
Session 23 - JDBC
PawanMM
 
CORE JAVA-1
CORE JAVA-1CORE JAVA-1
import required package file import java.io.File; The Filed.pdf
 import required package file import java.io.File; The Filed.pdf import required package file import java.io.File; The Filed.pdf
import required package file import java.io.File; The Filed.pdf
anandinternational01
 
Input output files in java
Input output files in javaInput output files in java
Input output files in java
Kavitha713564
 
Description 1) Create a Lab2 folder for this project2.docx
Description       1)  Create a Lab2 folder for this project2.docxDescription       1)  Create a Lab2 folder for this project2.docx
Description 1) Create a Lab2 folder for this project2.docx
theodorelove43763
 
Use Java to program the following.1. Create public java class name.pdf
Use Java to program the following.1. Create public java class name.pdfUse Java to program the following.1. Create public java class name.pdf
Use Java to program the following.1. Create public java class name.pdf
f3apparelsonline
 
Comments added... import java.io.File; import java.io.FileNotF.pdf
Comments added... import java.io.File; import java.io.FileNotF.pdfComments added... import java.io.File; import java.io.FileNotF.pdf
Comments added... import java.io.File; import java.io.FileNotF.pdf
anwarfoot
 
Hi, I need help with a java programming project. specifically practi.pdf
Hi, I need help with a java programming project. specifically practi.pdfHi, I need help with a java programming project. specifically practi.pdf
Hi, I need help with a java programming project. specifically practi.pdf
PRATIKSINHA7304
 
Please I am trying to get this code to output in -txt file- I need you.pdf
Please I am trying to get this code to output in -txt file- I need you.pdfPlease I am trying to get this code to output in -txt file- I need you.pdf
Please I am trying to get this code to output in -txt file- I need you.pdf
asenterprisestyagi
 
The Java Program for the above given isimport java.io.File;impo.pdf
The Java Program for the above given isimport java.io.File;impo.pdfThe Java Program for the above given isimport java.io.File;impo.pdf
The Java Program for the above given isimport java.io.File;impo.pdf
anjanacottonmills
 
Understanding java streams
Understanding java streamsUnderstanding java streams
Understanding java streams
Shahjahan Samoon
 
Need help with this java code. fill in lines where needed. Also, not.pdf
Need help with this java code. fill in lines where needed. Also, not.pdfNeed help with this java code. fill in lines where needed. Also, not.pdf
Need help with this java code. fill in lines where needed. Also, not.pdf
rishabjain5053
 
JDBC
JDBCJDBC
Java căn bản - Chapter12
Java căn bản - Chapter12Java căn bản - Chapter12
Java căn bản - Chapter12
Vince Vo
 

Similar to The java program that reads an input file input.txt an.pdf (20)

Exception handling
Exception handlingException handling
Exception handling
 
Basic input-output-v.1.1
Basic input-output-v.1.1Basic input-output-v.1.1
Basic input-output-v.1.1
 
Java 3 Computer Science.pptx
Java 3 Computer Science.pptxJava 3 Computer Science.pptx
Java 3 Computer Science.pptx
 
IO and threads Java
IO and threads JavaIO and threads Java
IO and threads Java
 
For the following I have finished most of this question but am havin.pdf
For the following I have finished most of this question but am havin.pdfFor the following I have finished most of this question but am havin.pdf
For the following I have finished most of this question but am havin.pdf
 
Change the code in Writer.java only to get it working. Must contain .pdf
Change the code in Writer.java only to get it working. Must contain .pdfChange the code in Writer.java only to get it working. Must contain .pdf
Change the code in Writer.java only to get it working. Must contain .pdf
 
Session 23 - JDBC
Session 23 - JDBCSession 23 - JDBC
Session 23 - JDBC
 
CORE JAVA-1
CORE JAVA-1CORE JAVA-1
CORE JAVA-1
 
import required package file import java.io.File; The Filed.pdf
 import required package file import java.io.File; The Filed.pdf import required package file import java.io.File; The Filed.pdf
import required package file import java.io.File; The Filed.pdf
 
Input output files in java
Input output files in javaInput output files in java
Input output files in java
 
Description 1) Create a Lab2 folder for this project2.docx
Description       1)  Create a Lab2 folder for this project2.docxDescription       1)  Create a Lab2 folder for this project2.docx
Description 1) Create a Lab2 folder for this project2.docx
 
Use Java to program the following.1. Create public java class name.pdf
Use Java to program the following.1. Create public java class name.pdfUse Java to program the following.1. Create public java class name.pdf
Use Java to program the following.1. Create public java class name.pdf
 
Comments added... import java.io.File; import java.io.FileNotF.pdf
Comments added... import java.io.File; import java.io.FileNotF.pdfComments added... import java.io.File; import java.io.FileNotF.pdf
Comments added... import java.io.File; import java.io.FileNotF.pdf
 
Hi, I need help with a java programming project. specifically practi.pdf
Hi, I need help with a java programming project. specifically practi.pdfHi, I need help with a java programming project. specifically practi.pdf
Hi, I need help with a java programming project. specifically practi.pdf
 
Please I am trying to get this code to output in -txt file- I need you.pdf
Please I am trying to get this code to output in -txt file- I need you.pdfPlease I am trying to get this code to output in -txt file- I need you.pdf
Please I am trying to get this code to output in -txt file- I need you.pdf
 
The Java Program for the above given isimport java.io.File;impo.pdf
The Java Program for the above given isimport java.io.File;impo.pdfThe Java Program for the above given isimport java.io.File;impo.pdf
The Java Program for the above given isimport java.io.File;impo.pdf
 
Understanding java streams
Understanding java streamsUnderstanding java streams
Understanding java streams
 
Need help with this java code. fill in lines where needed. Also, not.pdf
Need help with this java code. fill in lines where needed. Also, not.pdfNeed help with this java code. fill in lines where needed. Also, not.pdf
Need help with this java code. fill in lines where needed. Also, not.pdf
 
JDBC
JDBCJDBC
JDBC
 
Java căn bản - Chapter12
Java căn bản - Chapter12Java căn bản - Chapter12
Java căn bản - Chapter12
 

More from vichu19891

1. Copper is above silver in the activity series. Thus Cu metal will.pdf
1. Copper is above silver in the activity series. Thus Cu metal will.pdf1. Copper is above silver in the activity series. Thus Cu metal will.pdf
1. Copper is above silver in the activity series. Thus Cu metal will.pdf
vichu19891
 
using recursive method .pdf
  using recursive method .pdf  using recursive method .pdf
using recursive method .pdf
vichu19891
 
a. Population - families in the state of Florida b. Variable .pdf
 a. Population - families in the state of Florida b. Variable .pdf a. Population - families in the state of Florida b. Variable .pdf
a. Population - families in the state of Florida b. Variable .pdf
vichu19891
 
there is no reaction between HNO3 and KCl. S.pdf
                     there is no reaction between HNO3 and KCl.  S.pdf                     there is no reaction between HNO3 and KCl.  S.pdf
there is no reaction between HNO3 and KCl. S.pdf
vichu19891
 
The thickness of non-saturated zone and physico-c.pdf
                     The thickness of non-saturated zone and physico-c.pdf                     The thickness of non-saturated zone and physico-c.pdf
The thickness of non-saturated zone and physico-c.pdf
vichu19891
 
The folding process of proteins is hierarchical, .pdf
                     The folding process of proteins is hierarchical, .pdf                     The folding process of proteins is hierarchical, .pdf
The folding process of proteins is hierarchical, .pdf
vichu19891
 
Step1 ppt of PbCl2 are soluble in hot water. Ste.pdf
                     Step1 ppt of PbCl2 are soluble in hot water.  Ste.pdf                     Step1 ppt of PbCl2 are soluble in hot water.  Ste.pdf
Step1 ppt of PbCl2 are soluble in hot water. Ste.pdf
vichu19891
 
Should take off H from SH group, forming RS-Na+ s.pdf
                     Should take off H from SH group, forming RS-Na+ s.pdf                     Should take off H from SH group, forming RS-Na+ s.pdf
Should take off H from SH group, forming RS-Na+ s.pdf
vichu19891
 
Rest are okay but B) I think should be vanderwall.pdf
                     Rest are okay but B) I think should be vanderwall.pdf                     Rest are okay but B) I think should be vanderwall.pdf
Rest are okay but B) I think should be vanderwall.pdf
vichu19891
 
pH =4.217 pH = - log(concentration of H+) = -log .pdf
                     pH =4.217 pH = - log(concentration of H+) = -log .pdf                     pH =4.217 pH = - log(concentration of H+) = -log .pdf
pH =4.217 pH = - log(concentration of H+) = -log .pdf
vichu19891
 
Liquids may change to a vapor at temperatures bel.pdf
                     Liquids may change to a vapor at temperatures bel.pdf                     Liquids may change to a vapor at temperatures bel.pdf
Liquids may change to a vapor at temperatures bel.pdf
vichu19891
 
intra extra equilibrium potential mEqL mEqL 1.pdf
                     intra extra equilibrium potential  mEqL mEqL  1.pdf                     intra extra equilibrium potential  mEqL mEqL  1.pdf
intra extra equilibrium potential mEqL mEqL 1.pdf
vichu19891
 
Which of the following forms of DES is considered the most vulnerabl.pdf
Which of the following forms of DES is considered the most vulnerabl.pdfWhich of the following forms of DES is considered the most vulnerabl.pdf
Which of the following forms of DES is considered the most vulnerabl.pdf
vichu19891
 
This is actually pretty simple, so Ill help explain. Take a gi.pdf
This is actually pretty simple, so Ill help explain. Take a gi.pdfThis is actually pretty simple, so Ill help explain. Take a gi.pdf
This is actually pretty simple, so Ill help explain. Take a gi.pdf
vichu19891
 
There are many test IPv6 networks deployed across the world. For act.pdf
There are many test IPv6 networks deployed across the world. For act.pdfThere are many test IPv6 networks deployed across the world. For act.pdf
There are many test IPv6 networks deployed across the world. For act.pdf
vichu19891
 
The three ways of presenting the changes in the balance of the Compr.pdf
The three ways of presenting the changes in the balance of the Compr.pdfThe three ways of presenting the changes in the balance of the Compr.pdf
The three ways of presenting the changes in the balance of the Compr.pdf
vichu19891
 
The RASopathies are a group of genetic syndromes caused by germline .pdf
The RASopathies are a group of genetic syndromes caused by germline .pdfThe RASopathies are a group of genetic syndromes caused by germline .pdf
The RASopathies are a group of genetic syndromes caused by germline .pdf
vichu19891
 
ThanksSolutionThanks.pdf
ThanksSolutionThanks.pdfThanksSolutionThanks.pdf
ThanksSolutionThanks.pdf
vichu19891
 
Step1 In O2 ; we have 2 unpaired electrons which occupy pi 2p Anti.pdf
Step1 In O2 ; we have 2 unpaired electrons which occupy pi 2p  Anti.pdfStep1 In O2 ; we have 2 unpaired electrons which occupy pi 2p  Anti.pdf
Step1 In O2 ; we have 2 unpaired electrons which occupy pi 2p Anti.pdf
vichu19891
 
Quartzite is sandstone that has been converted to a solid quartz roc.pdf
Quartzite is sandstone that has been converted to a solid quartz roc.pdfQuartzite is sandstone that has been converted to a solid quartz roc.pdf
Quartzite is sandstone that has been converted to a solid quartz roc.pdf
vichu19891
 

More from vichu19891 (20)

1. Copper is above silver in the activity series. Thus Cu metal will.pdf
1. Copper is above silver in the activity series. Thus Cu metal will.pdf1. Copper is above silver in the activity series. Thus Cu metal will.pdf
1. Copper is above silver in the activity series. Thus Cu metal will.pdf
 
using recursive method .pdf
  using recursive method .pdf  using recursive method .pdf
using recursive method .pdf
 
a. Population - families in the state of Florida b. Variable .pdf
 a. Population - families in the state of Florida b. Variable .pdf a. Population - families in the state of Florida b. Variable .pdf
a. Population - families in the state of Florida b. Variable .pdf
 
there is no reaction between HNO3 and KCl. S.pdf
                     there is no reaction between HNO3 and KCl.  S.pdf                     there is no reaction between HNO3 and KCl.  S.pdf
there is no reaction between HNO3 and KCl. S.pdf
 
The thickness of non-saturated zone and physico-c.pdf
                     The thickness of non-saturated zone and physico-c.pdf                     The thickness of non-saturated zone and physico-c.pdf
The thickness of non-saturated zone and physico-c.pdf
 
The folding process of proteins is hierarchical, .pdf
                     The folding process of proteins is hierarchical, .pdf                     The folding process of proteins is hierarchical, .pdf
The folding process of proteins is hierarchical, .pdf
 
Step1 ppt of PbCl2 are soluble in hot water. Ste.pdf
                     Step1 ppt of PbCl2 are soluble in hot water.  Ste.pdf                     Step1 ppt of PbCl2 are soluble in hot water.  Ste.pdf
Step1 ppt of PbCl2 are soluble in hot water. Ste.pdf
 
Should take off H from SH group, forming RS-Na+ s.pdf
                     Should take off H from SH group, forming RS-Na+ s.pdf                     Should take off H from SH group, forming RS-Na+ s.pdf
Should take off H from SH group, forming RS-Na+ s.pdf
 
Rest are okay but B) I think should be vanderwall.pdf
                     Rest are okay but B) I think should be vanderwall.pdf                     Rest are okay but B) I think should be vanderwall.pdf
Rest are okay but B) I think should be vanderwall.pdf
 
pH =4.217 pH = - log(concentration of H+) = -log .pdf
                     pH =4.217 pH = - log(concentration of H+) = -log .pdf                     pH =4.217 pH = - log(concentration of H+) = -log .pdf
pH =4.217 pH = - log(concentration of H+) = -log .pdf
 
Liquids may change to a vapor at temperatures bel.pdf
                     Liquids may change to a vapor at temperatures bel.pdf                     Liquids may change to a vapor at temperatures bel.pdf
Liquids may change to a vapor at temperatures bel.pdf
 
intra extra equilibrium potential mEqL mEqL 1.pdf
                     intra extra equilibrium potential  mEqL mEqL  1.pdf                     intra extra equilibrium potential  mEqL mEqL  1.pdf
intra extra equilibrium potential mEqL mEqL 1.pdf
 
Which of the following forms of DES is considered the most vulnerabl.pdf
Which of the following forms of DES is considered the most vulnerabl.pdfWhich of the following forms of DES is considered the most vulnerabl.pdf
Which of the following forms of DES is considered the most vulnerabl.pdf
 
This is actually pretty simple, so Ill help explain. Take a gi.pdf
This is actually pretty simple, so Ill help explain. Take a gi.pdfThis is actually pretty simple, so Ill help explain. Take a gi.pdf
This is actually pretty simple, so Ill help explain. Take a gi.pdf
 
There are many test IPv6 networks deployed across the world. For act.pdf
There are many test IPv6 networks deployed across the world. For act.pdfThere are many test IPv6 networks deployed across the world. For act.pdf
There are many test IPv6 networks deployed across the world. For act.pdf
 
The three ways of presenting the changes in the balance of the Compr.pdf
The three ways of presenting the changes in the balance of the Compr.pdfThe three ways of presenting the changes in the balance of the Compr.pdf
The three ways of presenting the changes in the balance of the Compr.pdf
 
The RASopathies are a group of genetic syndromes caused by germline .pdf
The RASopathies are a group of genetic syndromes caused by germline .pdfThe RASopathies are a group of genetic syndromes caused by germline .pdf
The RASopathies are a group of genetic syndromes caused by germline .pdf
 
ThanksSolutionThanks.pdf
ThanksSolutionThanks.pdfThanksSolutionThanks.pdf
ThanksSolutionThanks.pdf
 
Step1 In O2 ; we have 2 unpaired electrons which occupy pi 2p Anti.pdf
Step1 In O2 ; we have 2 unpaired electrons which occupy pi 2p  Anti.pdfStep1 In O2 ; we have 2 unpaired electrons which occupy pi 2p  Anti.pdf
Step1 In O2 ; we have 2 unpaired electrons which occupy pi 2p Anti.pdf
 
Quartzite is sandstone that has been converted to a solid quartz roc.pdf
Quartzite is sandstone that has been converted to a solid quartz roc.pdfQuartzite is sandstone that has been converted to a solid quartz roc.pdf
Quartzite is sandstone that has been converted to a solid quartz roc.pdf
 

Recently uploaded

How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17
Celine George
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
Priyankaranawat4
 
Main Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docxMain Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docx
adhitya5119
 
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat  Leveraging AI for Diversity, Equity, and InclusionExecutive Directors Chat  Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
TechSoup
 
Cognitive Development Adolescence Psychology
Cognitive Development Adolescence PsychologyCognitive Development Adolescence Psychology
Cognitive Development Adolescence Psychology
paigestewart1632
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
Scholarhat
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
tarandeep35
 
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UPLAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
RAHUL
 
Life upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for studentLife upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for student
NgcHiNguyn25
 
How to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP ModuleHow to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP Module
Celine George
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
Priyankaranawat4
 
DRUGS AND ITS classification slide share
DRUGS AND ITS classification slide shareDRUGS AND ITS classification slide share
DRUGS AND ITS classification slide share
taiba qazi
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
Nicholas Montgomery
 
How to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 InventoryHow to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 Inventory
Celine George
 
How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17
Celine George
 
Liberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdfLiberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdf
WaniBasim
 
Digital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental DesignDigital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental Design
amberjdewit93
 
PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.
Dr. Shivangi Singh Parihar
 
Community pharmacy- Social and preventive pharmacy UNIT 5
Community pharmacy- Social and preventive pharmacy UNIT 5Community pharmacy- Social and preventive pharmacy UNIT 5
Community pharmacy- Social and preventive pharmacy UNIT 5
sayalidalavi006
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
History of Stoke Newington
 

Recently uploaded (20)

How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
 
Main Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docxMain Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docx
 
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat  Leveraging AI for Diversity, Equity, and InclusionExecutive Directors Chat  Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
 
Cognitive Development Adolescence Psychology
Cognitive Development Adolescence PsychologyCognitive Development Adolescence Psychology
Cognitive Development Adolescence Psychology
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
 
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UPLAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
 
Life upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for studentLife upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for student
 
How to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP ModuleHow to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP Module
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
 
DRUGS AND ITS classification slide share
DRUGS AND ITS classification slide shareDRUGS AND ITS classification slide share
DRUGS AND ITS classification slide share
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
 
How to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 InventoryHow to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 Inventory
 
How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17
 
Liberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdfLiberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdf
 
Digital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental DesignDigital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental Design
 
PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.
 
Community pharmacy- Social and preventive pharmacy UNIT 5
Community pharmacy- Social and preventive pharmacy UNIT 5Community pharmacy- Social and preventive pharmacy UNIT 5
Community pharmacy- Social and preventive pharmacy UNIT 5
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
 

The java program that reads an input file input.txt an.pdf

  • 1. /* * The java program that reads an input file input.txt * and then reads the contens of file. * the program throws file not found exception if file not found. * The program throws DimensionMismatchException if mismatch of dimension. * The program throws NumberFormatException if mismatch of data read. * */ //FormatChecker.java import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class FormatChecker { public static void main(String[] args) { String fileName="input.txt"; //calling checkFormat checkFormat(fileName); } //Method checkFormat that takes string and check for //errors otherwise reads the file and print to console private static void checkFormat(String fileName) { Scanner filescanner=null; try { //thorws FileNotFoundException error if file not found filescanner=new Scanner(new File(fileName)); } catch (FileNotFoundException e) { System.out.println(e.getMessage()); } int rows=0; int cols=0; double[][] arr;
  • 2. String dimensions[]=filescanner.nextLine().split(" "); try { //thorws DimensionMismatchException error if mismatch of dimension if(dimensions.length>2) throw new DimensionMismatchException("Dimension mismatch"); } catch (DimensionMismatchException e1) { e1.printStackTrace(); } try { //thorws NumberFormatException error if mismatch of reading values rows=Integer.parseInt(dimensions[0]); cols=Integer.parseInt(dimensions[1]); arr=new double[rows][cols]; System.out.println("File contents"); for (int i = 0; i < arr.length; i++) { for (int j = 0; j < arr[i].length; j++) { arr[i][j]=filescanner.nextDouble(); System.out.printf("%6.1f",arr[i][j]); } System.out.println(); } } catch (NumberFormatException e) { System.out.println(e.getMessage()); }
  • 3. } } ------------------------------------------------------------------------ //User defined DimensionMismatchException class //that extends the Exception class //DimensionMismatchException.java public class DimensionMismatchException extends Exception { public DimensionMismatchException(String msg) { super(msg); } } ------------------------------------------------------------------------ input.txt 5 6 2.5 0 1 0 0 0 0 -1 4 0 0 0 0 0 0 0 1.1 0 0 2 0 0 5 0 0 -3.14 0 0 0 0 ------------------------------------------------------------------------ File contents 2.5 0.0 1.0 0.0 0.0 0.0 0.0 -1.0 4.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 1.1 0.0 0.0 2.0 0.0 0.0 5.0 0.0 0.0 -3.1 0.0 0.0 0.0 0.0 Note : DimensionMismatchException class is included Solution /*
  • 4. * The java program that reads an input file input.txt * and then reads the contens of file. * the program throws file not found exception if file not found. * The program throws DimensionMismatchException if mismatch of dimension. * The program throws NumberFormatException if mismatch of data read. * */ //FormatChecker.java import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class FormatChecker { public static void main(String[] args) { String fileName="input.txt"; //calling checkFormat checkFormat(fileName); } //Method checkFormat that takes string and check for //errors otherwise reads the file and print to console private static void checkFormat(String fileName) { Scanner filescanner=null; try { //thorws FileNotFoundException error if file not found filescanner=new Scanner(new File(fileName)); } catch (FileNotFoundException e) { System.out.println(e.getMessage()); } int rows=0; int cols=0; double[][] arr; String dimensions[]=filescanner.nextLine().split(" ");
  • 5. try { //thorws DimensionMismatchException error if mismatch of dimension if(dimensions.length>2) throw new DimensionMismatchException("Dimension mismatch"); } catch (DimensionMismatchException e1) { e1.printStackTrace(); } try { //thorws NumberFormatException error if mismatch of reading values rows=Integer.parseInt(dimensions[0]); cols=Integer.parseInt(dimensions[1]); arr=new double[rows][cols]; System.out.println("File contents"); for (int i = 0; i < arr.length; i++) { for (int j = 0; j < arr[i].length; j++) { arr[i][j]=filescanner.nextDouble(); System.out.printf("%6.1f",arr[i][j]); } System.out.println(); } } catch (NumberFormatException e) { System.out.println(e.getMessage()); }
  • 6. } } ------------------------------------------------------------------------ //User defined DimensionMismatchException class //that extends the Exception class //DimensionMismatchException.java public class DimensionMismatchException extends Exception { public DimensionMismatchException(String msg) { super(msg); } } ------------------------------------------------------------------------ input.txt 5 6 2.5 0 1 0 0 0 0 -1 4 0 0 0 0 0 0 0 1.1 0 0 2 0 0 5 0 0 -3.14 0 0 0 0 ------------------------------------------------------------------------ File contents 2.5 0.0 1.0 0.0 0.0 0.0 0.0 -1.0 4.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 1.1 0.0 0.0 2.0 0.0 0.0 5.0 0.0 0.0 -3.1 0.0 0.0 0.0 0.0 Note : DimensionMismatchException class is included