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 Console output:
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started