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

PDF
2. Write a program which will open an input file and write to an out.pdf
DOCX
Complete the following Java code such that it opens a file named Fruit.docx
PPT
04_1.Exceptionssssssssssssssssssss..pptt
PDF
DOCX
PAGE 1Input output for a file tutorialStreams and File IOI.docx
DOCX
Write a java program that allows the user to input the name of a fil.docx
DOCX
Write a java program that allows the user to input the name of a file.docx
DOCX
JAVA Q2- Write a program that reads strings from the user and writes t.docx
2. Write a program which will open an input file and write to an out.pdf
Complete the following Java code such that it opens a file named Fruit.docx
04_1.Exceptionssssssssssssssssssss..pptt
PAGE 1Input output for a file tutorialStreams and File IOI.docx
Write a java program that allows the user to input the name of a fil.docx
Write a java program that allows the user to input the name of a file.docx
JAVA Q2- Write a program that reads strings from the user and writes t.docx

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

PPTX
ExtraFileIO.pptx
PPT
Exception handling
PPT
Basic input-output-v.1.1
PPTX
Files that are designed to be read by human beings
PPTX
Java 3 Computer Science.pptx
PPTX
IO and threads Java
PDF
For the following I have finished most of this question but am havin.pdf
PDF
Change the code in Writer.java only to get it working. Must contain .pdf
PPTX
Session 23 - JDBC
PDF
import required package file import java.io.File; The Filed.pdf
PDF
OOPs with Java_unit2.pdf. sarthak bookkk
PPTX
Input output files in java
DOCX
Description 1) Create a Lab2 folder for this project2.docx
PDF
Use Java to program the following.1. Create public java class name.pdf
PDF
Comments added... import java.io.File; import java.io.FileNotF.pdf
PDF
Hi, I need help with a java programming project. specifically practi.pdf
PDF
Please I am trying to get this code to output in -txt file- I need you.pdf
PDF
The Java Program for the above given isimport java.io.File;impo.pdf
PPTX
Understanding java streams
ExtraFileIO.pptx
Exception handling
Basic input-output-v.1.1
Files that are designed to be read by human beings
Java 3 Computer Science.pptx
IO and threads Java
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
Session 23 - JDBC
import required package file import java.io.File; The Filed.pdf
OOPs with Java_unit2.pdf. sarthak bookkk
Input output files in java
Description 1) Create a Lab2 folder for this project2.docx
Use Java to program the following.1. Create public java class name.pdf
Comments added... import java.io.File; import java.io.FileNotF.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
The Java Program for the above given isimport java.io.File;impo.pdf
Understanding java streams
Ad

More from vichu19891 (20)

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

Recently uploaded (20)

PDF
01-Introduction-to-Information-Management.pdf
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PPTX
Pharma ospi slides which help in ospi learning
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PPTX
Cell Types and Its function , kingdom of life
PDF
Microbial disease of the cardiovascular and lymphatic systems
PPTX
202450812 BayCHI UCSC-SV 20250812 v17.pptx
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
01-Introduction-to-Information-Management.pdf
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Chinmaya Tiranga quiz Grand Finale.pdf
O7-L3 Supply Chain Operations - ICLT Program
Anesthesia in Laparoscopic Surgery in India
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Module 4: Burden of Disease Tutorial Slides S2 2025
VCE English Exam - Section C Student Revision Booklet
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
O5-L3 Freight Transport Ops (International) V1.pdf
Pharma ospi slides which help in ospi learning
human mycosis Human fungal infections are called human mycosis..pptx
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Cell Types and Its function , kingdom of life
Microbial disease of the cardiovascular and lymphatic systems
202450812 BayCHI UCSC-SV 20250812 v17.pptx
STATICS OF THE RIGID BODIES Hibbelers.pdf

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