Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I have this Java code is working perfect, but I just need to count how many Pair

ID: 3903542 • Letter: I

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 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<count;i++)//iterating through array

{

for(int j=i+1;j<count;j++)//with each nuber in array checking pairs with another numbers in array

{

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

Console output:

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

Explanation / Answer

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

int PairCount = 0;

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<count;i++)//iterating through array

{

for(int j=i+1;j<count;j++)//with each nuber in array checking pairs with another numbers in array

{

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

{

PairCount++;

}

}

}

System.out.println(PairCount);

bw.close();//closig buffered writer

fw.close();//closing file writer

}

}