java
Write a program to determine the number pairs of values in an input file that are equal. If your first try is quadratic, think again and use Arrays.sort() to develop a linearithmic solution.
import java.io.*;
import java.util.*;
public class DuplicatePairs
{
public static void main(String[] args)throws Exception
{
File file = new File("C:\\Users\\Debjit
G\\Desktop\\test.txt");
BufferedReader br = new BufferedReader(new FileReader(file));
//Loading the required file into BufferedReader
String st=br.readLine(); //Reading the line of numbers
int n,i,j;
n=0;
for(i=0;i<st.length();i++) //Counting the number of spaces and
hence number of integers in the string
{
if(st.charAt(i)==' ')
n++;
}
n=n+1;
int a[]=new int[n];
String w="";
j=0;
for(i=0;i<st.length();i++) //Extracting the integers from the
string into an array
{
if(st.charAt(i)==' ')
{
a[j++]=Integer.parseInt(w);
w="";
}
else
w=w+st.charAt(i);
}
a[j++]=Integer.parseInt(w);
Arrays.sort(a); //Sorting the array
j=0;
int count=0,c;
while(j<n) //Counting the frequency of every number in the array
and hence finding the number of duplicate pairs
{
c=1;
while(j+1<n && a[j+1]==a[j])
{
c++;
j++;
}
j++;
count=count+(c*(c-1)/2);
}
System.out.println("Number of duplicate pairs in the array :
"+count);
}
}
So, we first load the file into the BufferedReader object and read the line of integers from the file. Then we count the number of spaces to know the number of integers in the string, and create an array of that size. Then we extract the integers into the array one by one. Now we have two choices. We can create every pair in the array and check for duplicates, but that will take Quadratic Time Complexity. So we sort the array and count the frequency of every number in the array. For n same integers in the array, nC2 number of pairs will be formed. Now nC2 can also be written as n*(n-1)/2, which we use in the code. We output the result stored in count, and the Time Complexity is Linear.
java Write a program to determine the number pairs of values in an input file that...
java Program Write a program called Copy that accepts the names of an input file and an output file on the command line and copies the contents of the input file to the output file, for example, csc$ java Copy infile.txt outfile.txt
Write a program that reads a Java source-code file and reports the number of keywords (including null, true, and false) in the file. If a keyword is in a comment or in a string, don’t count it. Test your program to count the keywords in the Welcome.java file: Please input the Java filename: Welcome.java The number of keywords in the program is 6
java Write a program called Copy that accepts the names of an input file and an output file on the command line and copies the contents of the input file to the output file, for example, csc$ java Copy infile.txt outfile.txt
Write a program in Java that reads the file and does two things: Write to an output file called dataSwitch.txt the same data from the input file, but switch the order of the data entries on each line. For example, if the first line of the input file is “90001 57110”, the first line of the output file would be “57110 90001”. Additionally, print to the screen the number of lines in the file with a label
How to write a Java program that reads the file "input.txt" and writes all even values from this file into a new file called "output.txt." Sample input 10 12 1 3 5 34 2 5 7 9 44
FOR JAVA Write a program that takes two command line arguments: an input file and an output file. The program should read the input file and replace the last letter of each word with a * character and write the result to the output file. The program should maintain the input file's line separators. The program should catch all possible checked exceptions and display an informative message. Notes: This program can be written in a single main method Remember that...
Write a program that will check a Java file for syntax errors. The program will ask for an input-file name and output-file name and will then copy all the code from the Java input file to the Java output file, but with the following changes: 1. Any syntax error found in the Java input file will be corrected: a. Missing semicolon b. Missing compound statements (curly braces) c. All comments have to start with // and end with a period....
Write a program that takes a file as input and checks whether or not the content of the file is balanced. In the context of this assignment “balanced” means that your program will check to make sure that for each left bracket there is a closing right bracket. Examples: [ ( ) ] { } à balanced. [ ( ] ) { } à unbalanced. Here is the list of brackets/braces that program must support: (: left parentheses...
Write a Java program that reads four integers and prints "two pairs" if the input consists of two matching pairs (in some order) and "not two pairs" otherwise. For example, 1 2 2 1 two pairs 1 2 2 3 not two pairs 2 2 2 2 two pairs
Write a program in Java which accepts Input from File and provides output on screen.