Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Written by MD Harrington
- Java solution to deal with these types of questions
- EG
- Look for another number apart from 709
- 709 709 709 709 709 709 709 709 709 709
- 709 709 709 709 709 709 709 709 709 709
- 709 709 709 709 709 709 709 709 709 709
- 709 709 709 709 709 709 709 709 709 709
- 709 409 709 709 709 709 609 709 709 709
- 709 509 709 709 709 709 709 709 709 709
- 709 709 678 709 709 509 709 709 709 709
- 209 709 609 709 709 709 709 709 709 709
- 709 709 709 709 709 709 709 709 709 709
- 709 709 709 709 709 709 709 709 709 709
- 709 709 709 409 709 709 709 709 775 709
- 709 709 709 709 709 709 709 709 709 709
- 709 709 709 709 709 809 709 709 709 709
- 709 709 709 409 709 709 709 709 709 709
- 709 709 709 709 709 709 709 709 709 709
- 709 508 709 709 709 709 709 709 709 609
- 709 709 209 709 709 709 709 709 709 709
- 209 709 709 709 709 709 709 709 709 709
- 709 709 609 709 709 709 709 709 709 709
- 709 709 709 709 709 709 709 309 709 709
- */
- Here is the solution to this in java
- package filetoarray;
- import java.io.BufferedReader;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.util.Arrays;
- public class FileToArray {
- private int[][] array;
- public FileToArray(String filePath) {
- try {
- // Open and read the file
- BufferedReader br = new BufferedReader(new FileReader(filePath));
- // Create a 2D array to store the numbers
- array = new int[20][10];
- // Read and parse the numbers from the file
- String line = br.readLine();
- String[] numbers = line.split("\\s+"); // Assuming numbers are separated by spaces
- for (int i = 0; i < 20; i++) {
- for (int j = 0; j < 10; j++) {
- array[i][j] = Integer.parseInt(numbers[i * 10 + j]);
- }
- }
- // Close the file
- br.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- public void compareAndPrintMismatches(int selectedNumber) {
- // Compare each number in the array
- for (int row = 0; row < 20; row++) {
- for (int col = 0; col < 10; col++) {
- int number = array[row][col];
- if (number != selectedNumber) {
- System.out.println("Mismatch: Row " + (row + 1) + ", Column " + (col + 1) + " - Value: " + number);
- }
- }
- }
- }
- public int countElements() {
- int count = 20 * 10;
- return count;
- }
- public void exportToFile(String outputPath, String delimiter) {
- try {
- FileWriter writer = new FileWriter(outputPath);
- for (int row = 0; row < 20; row++) {
- String rowString = Arrays.toString(array[row]).replace(",", delimiter);
- writer.write(rowString.substring(1, rowString.length() - 1) + "\n");
- }
- writer.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- public static void main(String[] args) {
- String inputFilePath = "numbers.txt"; // Change this to the path of your input file
- FileToArray fileToArray = new FileToArray(inputFilePath);
- // Define the number to compare against
- int selectedNumber = 709; // Change this to the number you want to compare
- // Call the function to compare and print mismatches
- fileToArray.compareAndPrintMismatches(selectedNumber);
- // Call the function to count the number of elements
- int elementCount = fileToArray.countElements();
- System.out.println("Total number of elements in the array: " + elementCount);
- String outputFilePath = "output.txt"; // Change this to the desired output file path
- String delimiter = "\t"; // Change this to the desired delimiter
- fileToArray.exportToFile(outputFilePath, delimiter);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement