Question: 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

I have this Java code is working perfect, but I just need

Console output:

to count how many Pairs, in below you will find the output

?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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!