SlideShare a Scribd company logo
1 of 6
Download to read offline
/*
* 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

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.pdfarihantplastictanksh
 
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 .pdfsecunderbadtirumalgi
 
Session 23 - JDBC
Session 23 - JDBCSession 23 - JDBC
Session 23 - JDBCPawanMM
 
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.pdfanandinternational01
 
Input output files in java
Input output files in javaInput output files in java
Input output files in javaKavitha713564
 
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.docxtheodorelove43763
 
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.pdff3apparelsonline
 
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.pdfanwarfoot
 
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.pdfPRATIKSINHA7304
 
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.pdfasenterprisestyagi
 
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.pdfanjanacottonmills
 
Understanding java streams
Understanding java streamsUnderstanding java streams
Understanding java streamsShahjahan 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.pdfrishabjain5053
 
Java căn bản - Chapter12
Java căn bản - Chapter12Java căn bản - Chapter12
Java căn bản - Chapter12Vince 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.pdfvichu19891
 
using recursive method .pdf
  using recursive method .pdf  using recursive method .pdf
using recursive method .pdfvichu19891
 
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 .pdfvichu19891
 
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.pdfvichu19891
 
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.pdfvichu19891
 
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, .pdfvichu19891
 
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.pdfvichu19891
 
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.pdfvichu19891
 
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.pdfvichu19891
 
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 .pdfvichu19891
 
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.pdfvichu19891
 
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.pdfvichu19891
 
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.pdfvichu19891
 
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.pdfvichu19891
 
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.pdfvichu19891
 
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.pdfvichu19891
 
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 .pdfvichu19891
 
ThanksSolutionThanks.pdf
ThanksSolutionThanks.pdfThanksSolutionThanks.pdf
ThanksSolutionThanks.pdfvichu19891
 
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.pdfvichu19891
 
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.pdfvichu19891
 

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

Improved Approval Flow in Odoo 17 Studio App
Improved Approval Flow in Odoo 17 Studio AppImproved Approval Flow in Odoo 17 Studio App
Improved Approval Flow in Odoo 17 Studio AppCeline George
 
An overview of the various scriptures in Hinduism
An overview of the various scriptures in HinduismAn overview of the various scriptures in Hinduism
An overview of the various scriptures in HinduismDabee Kamal
 
Graduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptxGraduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptxneillewis46
 
Đề tieng anh thpt 2024 danh cho cac ban hoc sinh
Đề tieng anh thpt 2024 danh cho cac ban hoc sinhĐề tieng anh thpt 2024 danh cho cac ban hoc sinh
Đề tieng anh thpt 2024 danh cho cac ban hoc sinhleson0603
 
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUMDEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUMELOISARIVERA8
 
Observing-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptxObserving-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptxAdelaideRefugio
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSAnaAcapella
 
SPLICE Working Group: Reusable Code Examples
SPLICE Working Group:Reusable Code ExamplesSPLICE Working Group:Reusable Code Examples
SPLICE Working Group: Reusable Code ExamplesPeter Brusilovsky
 
ANTI PARKISON DRUGS.pptx
ANTI         PARKISON          DRUGS.pptxANTI         PARKISON          DRUGS.pptx
ANTI PARKISON DRUGS.pptxPoojaSen20
 
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfFICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfPondicherry University
 
e-Sealing at EADTU by Kamakshi Rajagopal
e-Sealing at EADTU by Kamakshi Rajagopale-Sealing at EADTU by Kamakshi Rajagopal
e-Sealing at EADTU by Kamakshi RajagopalEADTU
 
diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.pptx....diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.pptx....Ritu480198
 
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes GuàrdiaPersonalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes GuàrdiaEADTU
 
SURVEY I created for uni project research
SURVEY I created for uni project researchSURVEY I created for uni project research
SURVEY I created for uni project researchCaitlinCummins3
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...Nguyen Thanh Tu Collection
 
Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...EduSkills OECD
 

Recently uploaded (20)

Improved Approval Flow in Odoo 17 Studio App
Improved Approval Flow in Odoo 17 Studio AppImproved Approval Flow in Odoo 17 Studio App
Improved Approval Flow in Odoo 17 Studio App
 
An overview of the various scriptures in Hinduism
An overview of the various scriptures in HinduismAn overview of the various scriptures in Hinduism
An overview of the various scriptures in Hinduism
 
Including Mental Health Support in Project Delivery, 14 May.pdf
Including Mental Health Support in Project Delivery, 14 May.pdfIncluding Mental Health Support in Project Delivery, 14 May.pdf
Including Mental Health Support in Project Delivery, 14 May.pdf
 
Graduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptxGraduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptx
 
Đề tieng anh thpt 2024 danh cho cac ban hoc sinh
Đề tieng anh thpt 2024 danh cho cac ban hoc sinhĐề tieng anh thpt 2024 danh cho cac ban hoc sinh
Đề tieng anh thpt 2024 danh cho cac ban hoc sinh
 
VAMOS CUIDAR DO NOSSO PLANETA! .
VAMOS CUIDAR DO NOSSO PLANETA!                    .VAMOS CUIDAR DO NOSSO PLANETA!                    .
VAMOS CUIDAR DO NOSSO PLANETA! .
 
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUMDEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
 
Observing-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptxObserving-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptx
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
 
SPLICE Working Group: Reusable Code Examples
SPLICE Working Group:Reusable Code ExamplesSPLICE Working Group:Reusable Code Examples
SPLICE Working Group: Reusable Code Examples
 
ANTI PARKISON DRUGS.pptx
ANTI         PARKISON          DRUGS.pptxANTI         PARKISON          DRUGS.pptx
ANTI PARKISON DRUGS.pptx
 
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfFICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
 
e-Sealing at EADTU by Kamakshi Rajagopal
e-Sealing at EADTU by Kamakshi Rajagopale-Sealing at EADTU by Kamakshi Rajagopal
e-Sealing at EADTU by Kamakshi Rajagopal
 
diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.pptx....diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.pptx....
 
Supporting Newcomer Multilingual Learners
Supporting Newcomer  Multilingual LearnersSupporting Newcomer  Multilingual Learners
Supporting Newcomer Multilingual Learners
 
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes GuàrdiaPersonalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
 
SURVEY I created for uni project research
SURVEY I created for uni project researchSURVEY I created for uni project research
SURVEY I created for uni project research
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
 
Mattingly "AI and Prompt Design: LLMs with NER"
Mattingly "AI and Prompt Design: LLMs with NER"Mattingly "AI and Prompt Design: LLMs with NER"
Mattingly "AI and Prompt Design: LLMs with NER"
 
Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...
 

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