SlideShare a Scribd company logo
Write a java program that allows the user to input the name of
a file that contains positive integers (one integer per line) The
number of integers in the file The program should find all odd
and even numbers and write the odd numbers to a file
"odd.txt" and write the even numbers to another file
"even.txt"
Solution
package expertques;
import java.io.File;
import java.io.PrintWriter;
import java.util.Scanner;
public class evenoddwithfile {
public static void main(String[] args) throws Exception {
File in;//file input
File even = new File("f://even.txt");//creates file even
File odd = new File("f://odd.txt");//creates file odd
Scanner s = new Scanner(System.in);//reads input from console
or user
System.out.println("Enter the file name with path: ");
in = new File(s.nextLine());
System.out.println("Enter the number of integers n the file:
");
int n = s.nextInt();
if (in.exists()) {//checks if input file exist or not
Scanner ins = new Scanner(in);//reads input file
PrintWriter pe = new PrintWriter(even);//write to even file
PrintWriter po = new PrintWriter(odd);//writes to odd file
for (int i = 0; i < n; i++) {//loop till the numbers exists
int x = ins.nextInt();//reads from input file
if (x % 2 == 0) {//checks for even or odd
pe.println(x);//if even writes in even file
} else {
po.println(x);//if odd writes in odd file
}
}
pe.close();//closes even file very important else nothing will be
written in the file
po.close();//closes odd file very important else nothing will be
written in the file
System.out.println("File successfully written with odds and
evens");
} else {
System.out.println("Error file not found at specified
path");//if file not exist
}
}
}
output
run:
Enter the file name with path:
f://integers.txt
Enter the number of integers n the file:
8
File successfully written with odds and evens
BUILD SUCCESSFUL (total time: 6 seconds)

More Related Content

PDF
Hi, I need help with a java programming project. specifically practi.pdf
PDF
Create a text file named lnfo.txt. Enter the following informatio.pdf
DOCX
PAGE 1Input output for a file tutorialStreams and File IOI.docx
PDF
The Java Program for the above given isimport java.io.File;impo.pdf
PPTX
Java 3 Computer Science.pptx
PPTX
IO and threads Java
DOCX
Write a program that will count the number of characters- words- and l.docx
PDF
Hi, I need help with a java programming project. specifically practi.pdf
Create a text file named lnfo.txt. Enter the following informatio.pdf
PAGE 1Input output for a file tutorialStreams and File IOI.docx
The Java Program for the above given isimport java.io.File;impo.pdf
Java 3 Computer Science.pptx
IO and threads Java
Write a program that will count the number of characters- words- and l.docx

Similar to Write a java program that allows the user to input the name of a fil.docx (20)

PPTX
Input output files in java
PPTX
ExtraFileIO.pptx
PPTX
Understanding java streams
PPT
File Input and Output in Java Programing language
DOCX
JAVA Q2- Write a program that reads strings from the user and writes t.docx
PPTX
File Handling.pptx
PDF
Java IO Stream, the introduction to Streams
PDF
Use Java to program the following.1. Create public java class name.pdf
PDF
Write a program in java that asks a user for a file name and prints .pdf
DOCX
source code which create file and write into it
DOCX
DOCX
Code red SUM
PDF
File Handling in Java.pdf
PDF
Please I am trying to get this code to output in -txt file- I need you.pdf
PPT
M251_Meeting 7 (Exception Handling and Text IO).ppt
PDF
Change the code in Writer.java only to get it working. Must contain .pdf
DOCX
Description 1) Create a Lab2 folder for this project2.docx
PPT
Java căn bản - Chapter12
PPT
Chapter 12 - File Input and Output
PDF
Java Program using RecursionWrite a program that reads in a file .pdf
Input output files in java
ExtraFileIO.pptx
Understanding java streams
File Input and Output in Java Programing language
JAVA Q2- Write a program that reads strings from the user and writes t.docx
File Handling.pptx
Java IO Stream, the introduction to Streams
Use Java to program the following.1. Create public java class name.pdf
Write a program in java that asks a user for a file name and prints .pdf
source code which create file and write into it
Code red SUM
File Handling in Java.pdf
Please I am trying to get this code to output in -txt file- I need you.pdf
M251_Meeting 7 (Exception Handling and Text IO).ppt
Change the code in Writer.java only to get it working. Must contain .pdf
Description 1) Create a Lab2 folder for this project2.docx
Java căn bản - Chapter12
Chapter 12 - File Input and Output
Java Program using RecursionWrite a program that reads in a file .pdf
Ad

More from ajoy21 (20)

DOCX
Please complete the assignment listed below.Define and explain, us.docx
DOCX
Please cite sources for each question. Do not use the same sources f.docx
DOCX
Please choose one of the following questions to answer for this week.docx
DOCX
Please check the attachment for my paper.Please add citations to a.docx
DOCX
Please answer to this discussion post. No less than 150 words. Refer.docx
DOCX
Please attach Non-nursing theorist summaries.JigsawExecutive .docx
DOCX
Please answer the question .There is no work count. PLEASE NUMBER .docx
DOCX
Please answer the following questions. Please cite your references..docx
DOCX
Please answer the following questions.1.      1.  Are you or.docx
DOCX
Please answer the following question with 200-300 words.Q. Discu.docx
DOCX
Please answer the following question Why do you think the US ha.docx
DOCX
Please answer the following questions. Define tunneling in the V.docx
DOCX
Please answer the following questions1. How can you stimulate the.docx
DOCX
Please answer the following questions very deeply and presicely .docx
DOCX
Please answer the following questions in an informal 1 ½ - 2-page es.docx
DOCX
Please answer the following questions in a response of 150 to 200 wo.docx
DOCX
Please answer these questions regarding the (TILA) Truth in Lending .docx
DOCX
Please answer the following question pertaining to psychology. Inc.docx
DOCX
Please answer the following questions in a response of 250 to 300 .docx
DOCX
Please answer the three questions completly. I have attached the que.docx
Please complete the assignment listed below.Define and explain, us.docx
Please cite sources for each question. Do not use the same sources f.docx
Please choose one of the following questions to answer for this week.docx
Please check the attachment for my paper.Please add citations to a.docx
Please answer to this discussion post. No less than 150 words. Refer.docx
Please attach Non-nursing theorist summaries.JigsawExecutive .docx
Please answer the question .There is no work count. PLEASE NUMBER .docx
Please answer the following questions. Please cite your references..docx
Please answer the following questions.1.      1.  Are you or.docx
Please answer the following question with 200-300 words.Q. Discu.docx
Please answer the following question Why do you think the US ha.docx
Please answer the following questions. Define tunneling in the V.docx
Please answer the following questions1. How can you stimulate the.docx
Please answer the following questions very deeply and presicely .docx
Please answer the following questions in an informal 1 ½ - 2-page es.docx
Please answer the following questions in a response of 150 to 200 wo.docx
Please answer these questions regarding the (TILA) Truth in Lending .docx
Please answer the following question pertaining to psychology. Inc.docx
Please answer the following questions in a response of 250 to 300 .docx
Please answer the three questions completly. I have attached the que.docx
Ad

Recently uploaded (20)

PDF
Pre independence Education in Inndia.pdf
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
Complications of Minimal Access Surgery at WLH
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PPTX
master seminar digital applications in india
PDF
Sports Quiz easy sports quiz sports quiz
PDF
Basic Mud Logging Guide for educational purpose
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
TR - Agricultural Crops Production NC III.pdf
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
Microbial disease of the cardiovascular and lymphatic systems
PPTX
Cell Types and Its function , kingdom of life
PPTX
Institutional Correction lecture only . . .
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
Pre independence Education in Inndia.pdf
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
2.FourierTransform-ShortQuestionswithAnswers.pdf
Complications of Minimal Access Surgery at WLH
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
master seminar digital applications in india
Sports Quiz easy sports quiz sports quiz
Basic Mud Logging Guide for educational purpose
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
TR - Agricultural Crops Production NC III.pdf
STATICS OF THE RIGID BODIES Hibbelers.pdf
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
Microbial diseases, their pathogenesis and prophylaxis
Microbial disease of the cardiovascular and lymphatic systems
Cell Types and Its function , kingdom of life
Institutional Correction lecture only . . .
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf

Write a java program that allows the user to input the name of a fil.docx

  • 1. Write a java program that allows the user to input the name of a file that contains positive integers (one integer per line) The number of integers in the file The program should find all odd and even numbers and write the odd numbers to a file "odd.txt" and write the even numbers to another file "even.txt" Solution package expertques; import java.io.File; import java.io.PrintWriter; import java.util.Scanner; public class evenoddwithfile { public static void main(String[] args) throws Exception { File in;//file input File even = new File("f://even.txt");//creates file even File odd = new File("f://odd.txt");//creates file odd Scanner s = new Scanner(System.in);//reads input from console or user System.out.println("Enter the file name with path: "); in = new File(s.nextLine()); System.out.println("Enter the number of integers n the file: ");
  • 2. int n = s.nextInt(); if (in.exists()) {//checks if input file exist or not Scanner ins = new Scanner(in);//reads input file PrintWriter pe = new PrintWriter(even);//write to even file PrintWriter po = new PrintWriter(odd);//writes to odd file for (int i = 0; i < n; i++) {//loop till the numbers exists int x = ins.nextInt();//reads from input file if (x % 2 == 0) {//checks for even or odd pe.println(x);//if even writes in even file } else { po.println(x);//if odd writes in odd file } } pe.close();//closes even file very important else nothing will be written in the file po.close();//closes odd file very important else nothing will be written in the file System.out.println("File successfully written with odds and evens"); } else { System.out.println("Error file not found at specified path");//if file not exist } }
  • 3. } output run: Enter the file name with path: f://integers.txt Enter the number of integers n the file: 8 File successfully written with odds and evens BUILD SUCCESSFUL (total time: 6 seconds)