I want to have a Java code. This code need read a file called content file( ther
ID: 3751794 • Letter: I
Question
I want to have a Java code. This code need read a file called content file( there will be numbers and letters in it) from desktop, and add every character into array1. Users need to type the name of the file. Then it need to read another file called test file. Test file will have two lines of content(letters and numbers), and the elements of the first line of content will be stored in array2, elements in second line will store into array3. Then code need to test , are all the elements in array2 can be found in array1, and all the elements in array3 can be found in array1.
Output should be ''array2 test success'' or ''array2 test fail''. The nest line is about array3, output will be ''array3 test success'' or''array3 test fail''
If it is possible, I want the code can test whether the order can also be detected. If array1 have ''icne'', array2 have ''nice'', then this test is fail. If and only if when the order is correct, system print success, such as ''156t'' in array1, ''1t'' in array2 , test success.
Explanation / Answer
package expertqa;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class compareFiles {
public static void main(String[] args) throws Exception
{
String DesktopFilePath, contentFileName, testFileName, contentFilePath,
testFilePath, contentData, testData, result1, result2;
Scanner in = new Scanner(System.in);
//takes user input for file names
DesktopFilePath = "/Users/xyz/Desktop/";
System.out.println("Please Enter Content File Name:");
contentFileName = in.nextLine();
System.out.println("Please Enter Test File Name:");
testFileName = in.nextLine();
contentFilePath = DesktopFilePath + contentFileName;
testFilePath = DesktopFilePath + testFileName;
//get file data in string.
contentData = readContentFile(contentFilePath);
testData = readTestFile(testFilePath);
//create character arrays from string.
int newLineIndex = testData.indexOf(" ");
char[] array1 = contentData.toCharArray();
char[] array2 = new char[newLineIndex];
char[] array3 = new char[testData.length() - newLineIndex];
testData.getChars(0, newLineIndex, array2, 0);
testData.getChars(newLineIndex, testData.length(), array3, 0);
//prints final result
result1 = compareArray(array1, array2, "array2");
result2 = compareArray(array1, array3, "array3");
System.out.println(result1);
System.out.println(result2);
}
//function accepts file path and returns the file content as a single string.
public static String readContentFile(String path) throws FileNotFoundException {
String data = "";
File file = new File(path); //creates file object.
Scanner sc = new Scanner(file);
while (sc.hasNextLine()) { //scans the line until no new line found.
data += sc.nextLine();
}
return data;
}
//function accepts file path and returns the file content as a single string.
public static String readTestFile(String path) throws FileNotFoundException {
String data = "";
File file = new File(path);
Scanner sc = new Scanner(file);
while (sc.hasNextLine()) {
data += sc.nextLine() + " ";
}
return data;
}
//function to check is all the elements in a array exists in another array in the same order
public static String compareArray(char[] arr1, char[] arr2, String name) throws FileNotFoundException {
String result = name + " test fail";
int j = 0;
for(int i = 0; i< arr2.length; i++){ //iterates second array
int found = 0;
for(;j< arr1.length; j++) {
if (arr1[j] == arr2[i]) { //check is element exists in array 1
j = j; //updates array1 start point for next iteration to maintain same order
found = 1; //value to keep track of comparison.
break;
}
}
if (found == 0) {break;}
else {result = name + " test success";}
}
return result;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.