1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Alenkinab [10]
3 years ago
5

Write a program that reads a stream of integers from a file and writes only the positive numbers to a second file. The user shou

ld be prompted to enter the names of both the input file and output file in main(), and then main() should attempt to open both files (providing an error if there is an error during this process). The main() method should then call the process() method to read all the integers from the input file and write only the positive integers to the output file. The process() method takes as arguments a Scanner to read from the input and a PrintWriter to write to the output. You can assume that if you are able to successfully open the input file, then there will only be integers in it.

Computers and Technology
1 answer:
borishaifa [10]3 years ago
5 0

Answer:

See explaination for thr program code.

Explanation:

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.FileWriter;

import java.io.IOException;

import java.io.OutputStream;

import java.io.PrintWriter;

import java.util.Scanner;

public class ReadWriteInt

{

public static void main(String args[]) throws IOException

{

Scanner input=new Scanner(System.in);

System.out.println("Enter the name of the input file:");

String inputFile=input.next();

System.out.println("Enter the name of the output file:");

String outputFile=input.next();

File fin=new File(inputFile);

File fout=new File(outputFile);

//print error, if input file does not exist

if(!fin.exists())

{

System.out.println("Error:Input file does not exist");

System.exit(0);

}

//print error, if output file does not exist

if(!fout.exists())

{

System.out.println("Error:output file does not exist");

System.exit(0);

}

//input stream to read

Scanner read=new Scanner(fin);

OutputStream os=new FileOutputStream(fout);

//output stream to write

PrintWriter write=new PrintWriter(os,true);

//read from input file until there are no numbers

while(read.hasNextLine())

{

String num=read.nextLine();

//convert the string into number

int number=Integer.parseInt(num);

//if the number is positive , write it to out put file

if(number>=0)

{

write.println(number);

System.out.println(number);

}

}

System.out.println("Positive numbers are copied successfully");

}

}

Please check attachment for screenshot and output

You might be interested in
You have found statistics on the Internet that you would like to use in your speech.
miss Akunina [59]

Answer:

You cite them correctly and that the statistics are real.

Explanation:

5 0
3 years ago
Henry has to create software that manages a database of all his clients of his firm. He wishes to run software on another comput
soldi70 [24.7K]

Answer:

The answer is B.

Explanation:

8 0
2 years ago
Read 2 more answers
2) [13 points] You’ve been hired by Banker Bisons to write a C++ console application that determines the number of months to rep
Paha777 [63]

Answer:

Check the explanation

Explanation:

CODE:

#include <iostream>

#include <iomanip>

using namespace std;

double getLoanAmount() { // functions as per asked in question

double loan;

while(true){ // while loop is used to re prompt

cout << "Enter the car loan amount ($2,500-7,500): ";

cin >> loan;

if (loan>=2500 && loan <= 7500){ // if the condition is fulfilled then

break; // break statement is use to come out of while loop

}

else{ // else error message is printed

cout << "Error: $"<< loan << " is an invalid loan amount." << endl;

}

}

return loan;

}

double getMonthlyPayment(){ // functions as per asked in question

double monthpay;

while(true){ // while loop is used to re prompt

cout << "Enter the monthly payment ($50-750): ";

cin >> monthpay;

if (monthpay>=50 && monthpay <= 750){ // if the condition is fulfilled then

break; // break statement is use to come out of while loop

}

else{ // else error message is printed

cout << "Error: $"<< monthpay << " is an invalid monthly payment." << endl;

}

}

return monthpay;

}

double getInterestRate(){ // functions as per asked in question

double rate;

while(true){ // while loop is used to re prompt

cout << "Enter the annual interest rate (1-6%): ";

cin >> rate;

if (rate>=1 && rate <= 6){ // if the condition is fulfilled then

break; // break statement is use to come out of while loop

}

else{ // else error message is printed

cout << "Error: "<< rate << "% is an invalid annual interest rate." << endl;

}

}

return rate;

}

int main() {

cout << setprecision(2) << fixed; // to print with 2 decimal places

int month = 0; // initializing month

// calling functions and storing the returned value

double balance= getLoanAmount();

double payment= getMonthlyPayment();

double rate= getInterestRate();

rate = rate /12 /100; // as per question

// printing as per required in question

cout << "Month Balance($) Payment($) Interest($) Principal($)"<< endl;

while(balance>0){ // while the balance is more than zero

month = month + 1; // counting Months

// calculations as per questions

double interest = balance * rate;

double principal = payment - interest;

balance = balance - principal;

// printing required info with proper spacing

cout <<" "<< month;

cout <<" "<< balance;

cout <<" "<< payment;

cout <<" "<< interest;

cout <<" "<< principal << endl;

}

cout << "Months to repay loan: " << month << endl; // displaying month

cout << "End of Banker Bisons";

Kindly check the output in the attached image below.

5 0
3 years ago
•Should other states wait to see if the high-speed rail system works well before starting their own plans for similar systems?
solmaris [256]
Other states should see how well the system works and also understand how much money it requires for the state to build a high-speed rail system.
7 0
3 years ago
Jamie has to enter the names, grades, and scores of a group of students into a worksheet. Which option will Jamie use to describ
GuDViN [60]

Answer: B. graphs

Jamie can use graphs to describe the data she enter such as names, grades and the scores of the group of students. In addition, using graphs will allow the reader to create visualization of the data, which will help the reader to easily understand and define the main purpose of the data entered.

7 0
3 years ago
Read 2 more answers
Other questions:
  • Your game design company has recently asked all employees to use a specific personal information management application (PIM) to
    6·1 answer
  • A plan that outlines the steps and timeline for reaching a certain goal is called a(n):
    13·1 answer
  • "Simon Says" is a memory game where "Simon" outputs a sequence of 10 characters (R, G, B, Y) and the user must repeat the sequen
    12·1 answer
  • What is the property of the Magnetic Lasso tool?
    6·2 answers
  • In Microsoft Word you can access the _______ command from the "Mini toolbar
    11·1 answer
  • Convert the binary number into a hexadecimal number.
    7·1 answer
  • 0x400C (in hexadecimal)?
    8·1 answer
  • What is the Gain (dB) of a transmission if the Maximum Data Rate is 1 Gbps and the Bandwidth =7000 MHz? Group of answer choices
    6·1 answer
  • Besides earning money why do people work​
    6·2 answers
  • Which of the following are acceptable to share? Check all of the boxes that apply.
    13·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!