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
Kelvin owns a small trading firm. Recently, he suspected that some of his employees were using fraudulent activities for their p
RideAnS [48]

Answer:

D. spyware

Explanation:

Well it kind of explains itself. He wants to "spy" on their activities.

7 0
3 years ago
Read 2 more answers
Write a shell script to count down from two hours to zero seconds using the hour, minute, second display format utilizing the sh
Rasek [7]

The beginning development of a star is marked by a supernova explosion, with the gases present in the nebula being forced to scatter. As the star shrinks, radiation of the surface increases and create pressure on the outside shell to push it away and forming a planetary nebula or white dwarf.

8 0
3 years ago
Implement a class Car with the following properties. A car has a certain fuel efficiency (measured in miles/gallon or liters/kmâ
zvonat [6]

Answer:

class Car(object):

   fuel = 0

   def __init__(self, mpg):

       self.mpg = mpg

   def drive(self, mile):

       if self.fuel * self.mpg >= mile:

           self.fuel -= mile / self.mpg

       else:

           print(f"get gas for your {self}")

       print(f"Fuel remaining: {self.fuel}")

   #classmethod

   def get_gas(cls):

       cls.fuel += 50

   #classmethod

   def add_gas(cls, gallon):

       if cls.fuel + gallon > 50:

           cls.fuel += 10

       else:

           cls.fuel += gallon

gulf = Car(20)

gulf.get_gas()

gulf.drive(200)

Explanation:

The Car class is defined in Python. Its drive method simulates the driving of a car with fuel that reduces by the miles covered, with efficiency in miles per gallon. The get_gas and add_gas methods fill and top up the car tank respectively.

3 0
2 years ago
The _____ _____ form of database attack injects code into user login information with one or more statements that perform action
amid [387]

Answer:

The answer is cross-site scripting(XSS)attack.

Explanation:

Hope this helps!

5 0
2 years ago
____ is a technology that exists inside another device
Julli [10]

Answer:

Embedded technology is a technology that exists inside another device.

8 0
3 years ago
Read 2 more answers
Other questions:
  • The act of engaging in crime through the use of a computer or similar type of device is called:
    8·1 answer
  • Temporary storage location for cutting and pasting
    9·1 answer
  • What is an enterprise system
    14·2 answers
  • #Write a function called hide_and_seek. The function should #have no parameters and return no value; instead, when #called, it s
    7·1 answer
  • Which of these would NOT be a valid Internet Control Message Protocol (ICMP) error message:
    6·1 answer
  • Colin Mackay Inc., a software company with its head office in Amsterdam, has employees across three continents. Certain project
    11·1 answer
  • What are some ways you can give staying off your phone a "boost" and make it easier to do?
    9·1 answer
  • Va rog urgent
    8·1 answer
  • DUE SOON NEED HELP FAST!!
    11·1 answer
  • Select the correct text in the passage.
    12·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!