Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I have this Java code is working perfect, but I just need to count how many Pairs, in below you will find the output is

I have this Java code is working perfect, but I just need to count how many Pairs, in below you will find the output is 2 pairs, so I need in that output ( the number of pairs = 2 )

this is the link of my old question: https://www.chegg.com/homework-help/questions-and-answers/write-program-called-sumtozirojava-following-reads-integers-text-input-file-inputtxt-count-q29613052

the code :

----------------------------------

package Sumtoziro;

import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.FileReader;

import java.io.FileWriter;

import java.io.IOException;

public class Sumtoziro {

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

int numbers[]=new int[1000];//to hold numbers read from txt file

FileReader fr=new FileReader("F://anand/Chegg/src/input.txt");//opening a file

BufferedReader br=new BufferedReader(fr);//passing filereader object to buffered reader for reading from file

String str="";//to hold each number read from file

int count=0;//to hold number of integers read from file

while((str=br.readLine())!=null)//reading line by line

{

numbers[count++]=Integer.parseInt(str);//storing in array

}

br.close();//closing buffered reader

fr.close();//closing filereader

FileWriter fw=new FileWriter("F://anand/Chegg/src/output.txt");//opening a output file

BufferedWriter bw=new BufferedWriter(fw);//passing file reader to buffered reader to write into file

bw.write("Pairs ");

System.out.println("Pairs");

for(int i=0;i

{

for(int j=i+1;j

{

if(numbers[i]+numbers[j]==0)//checking summ if zero then

{

System.out.println(numbers[i]+","+numbers[j]);//printing to console and writing to file

bw.write(numbers[i]+","+numbers[j]+" ");

}

}

}

bw.close();//closig buffered writer

fw.close();//closing file writer

}

}

Output.txt

image text in transcribed

Console output:

image text in transcribed

?new 45 email bt MB new 46 MB nen 1 Pairs 2 -77867,77867 3 155091,-155091

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Temporal Databases Research And Practice Lncs 1399

Authors: Opher Etzion ,Sushil Jajodia ,Suryanarayana Sripada

1st Edition

3540645195, 978-3540645191

More Books

Students also viewed these Databases questions

Question

For the diagram, compute the value of D: .5D D L i-12% 500

Answered: 1 week ago