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]
4 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]4 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
My window key dont work and I dont have a fn button, what can I do?<br> PLS help me.
Black_prince [1.1K]

Answer:

click on window button thought mouse or press esc and ctrl key at same time to open start menu option or to type window+r to open run dialog box

4 0
4 years ago
_____ uses computer-generated, three-dimensional images to create the illusion of interaction in a real-world environment.
mr_godi [17]

Answer:

"Virtual reality" is the appropriate answer.

Explanation:

The utilization of technological advances gives the impression of a 3D interactive environment wherein the things that have such a perception of space embodiment or involvement, are considered as Virtual reality.

Virtual reality implementations often include:

  • Entertainment,
  • Medical and
  • Educational activities.
3 0
3 years ago
What sort of negative outcomes are possible for this type of risk?
Harlamova29_29 [7]
More details please?
6 0
3 years ago
Read 2 more answers
What is the best information to include in the template name to differentiate it from other templates? Check all that apply.
KengaRu [80]

Answer:

2. version number

Explanation:

It is the version number of the template that explains how old or new it is. Also. if we want to differentiate between the two versions, then we probably have the best option as a version number to find out a template. and you just need to find the version number for it. You can have four versions in a year, and hence the year is not an option either. Also, the author's name can be the same for two software, and hence the author's name cannot be counted. The file size of the two versions can be the same as well. Hence, the file size cannot be used as well.

8 0
3 years ago
The function below takes a single parameters: a list of numbers called num_list. Return a new list containing just the three lar
yarga [219]

Answer:

  1. def three_largest_numbers(num_list):
  2.    sort_list = num_list
  3.    sort_list.sort(reverse=True)
  4.    return sort_list[0:3]
  5.    
  6. print(three_largest_numbers([5, 7, 1, 8, 9, 3, 6]))

Explanation:

Firstly, we declare a function that will take one input list, num_list (Line 1). To avoid modifying the original num_list, we create a copy of num_list and assign it to a new list, sort_list (Line 2) in the function. Next, we use Python list  sort function to sort the list in descending order (Line 3). At last, return the first three elements of the sort_list as output (Line 4).

We test the function using a sample list (Line 6) and we shall get [9, 8, 7] printed in terminal.

8 0
3 years ago
Other questions:
  • Which type of software is created and updated by a worldwide community of programmers and is available for​ free?
    14·1 answer
  • What happens when a Boolean expression is evaluated?
    7·1 answer
  • Which of the following is NOT true about functions? They go below the main program in the source code file. They do a single tas
    10·1 answer
  • In the context of this passage, which of the following is a synonym for alleged? A intended B claimed C denied D revealed
    15·1 answer
  • Instructions:Select the correct answer.
    9·2 answers
  • What is the most useful advantage of using the Slide Sorter view to rearrange slides instead of using the slide thumbnails in No
    13·1 answer
  • True or False: A function with no parameters or extern vars can only return a
    8·1 answer
  • Assignment 2 edhesive
    8·2 answers
  • During the Inspect and Adapt event, how are reflection, data collection, problem solving, and identification of improvement acti
    5·1 answer
  • 2.
    9·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!