Work with Files in Java: Searching and Replacing Elements of Array
Submitted by pavel7_7_7 on Saturday, February 8, 2014 - 20:14.
This article is an example of a typical college assignment. This article describes the opening file processes, and resolves some problems relevant to array usage.
Often, students are asked to implement some assignments that are connected with different array algorithms. Another popular task is the reading of data from file. In this article I'll describe an example of assignment, that is connected with files and arrays in Java. For example:
Your task consist of three questions:
The output of the program is:
Enter the file name (for example array.txt)
array.txt
Enter the size of array
10
Enter the number for search in array(in format like 5,4)
11,9
Question 1 Answer
The index of element 11.9 is 9
Enter the number to be replaced in array (in format like 5,4)
4,2
Enter the new value of replaced number (in format like 5,4)
2,12
Question 2 Answer
2,00 7,00 2,12 3,10 9,40 2,40 -5,70 8,10 9,20 11,90
- Open a file and read data to an array.
- Search an element in array
- Replace an element in array
- Open a file.
First step is to ask user to input the name of file. All the input of the user's data is done in the main method. That's why I'll start the review of the project from the main method.The variables that are defined in this method are:
To ask user for file's name and the size of array we have to create a scanner object to input data.The Scanner class methods that are used int the program are
- int size,
- index;
- double searchNumber,
- replaceNumber;
- String fileName;
- String nextLine() - returns inputed double
- String nextInt() - returns the next integer from console
- String nextDouble() - returns the next double from console
And now, I call a method to open the file, read data from it and search a number in array:- //create a scanner to read input
- fileName = in.nextLine();
- size = in.nextInt();
- searchNumber = in.nextDouble();
The searchIndex method has the next implementation:- index = searchIndex(fileName,size,searchNumber);
It declares an array of doubles and the, the readFile method returns the values that are stored in the txt file to- double[] array;
- int index;
- array = readFile(fileName,size);
- index = searchArray(array,searchValue);
- return index;
- }
array
variable. The readFile method opens a file in the next way:The first step is to initialize the array of returned values. After this you can try to read data from file. The process of reading data from file can throw exception. So, you always need to surround it with try/catch block to handle exceptions. So, the reading of double values is performed while the file has double values and the array is not full. This method returns the array of doubles, that is used in the next steps.- //method that reads the file
- //parameters are file name and size of array
- //return value - array of doubles from file
- double [] result;
- result = new double[size];
- //scanner class used to read the file
- Scanner scan;
- try {
- scan = new Scanner(file);
- int i = 0;
- while(scan.hasNextDouble()&&i<size){
- result[i] = scan.nextDouble();
- ++i;
- }
- scan.close();
- }
- return result;
- }
- Searching the index of an element in array
The next step is to search the index of an element, inputted by user, in the array of doubles. For this purpose
searchArray(array,searchValue);
is called. Let's take a look on searchArray method implementation:It's really simple and this method traverse the array and compare elements of the array to the searched value; An important moment of the assignment class is the fact, that the results of the question are printed by the special method- //a method to find searhNumber index in array
- //if the element is found - returns the index of the element
- //if there is no element in array returns -1
- public static int searchArray(double[] array,double searchNumber){
- for(int i = 0; i != array.length; ++i){
- if(array[i]==searchNumber)
- return i;
- }
- return -1;
printResult
The questions have different types of answer, that's why the same name of methods is used. The difference is only in the list of arguments:When we obtained the index of searched element, we can print the result of the search:- }
- }
- public static void printResult(int numQuestion,double[] array){
- for(int i = 0; i != array.length; ++i){
- }
- }
- printIndex(index,searchNumber);
- Replacing the element in array with new value
The next step is to replace the element of array by another element. For this you have to ask user to input the value that is replaced and the new value:
When you call this method, you need to pass an array of double to it as the first argument. It's done by
- double[] newArray = replaceElements(readFile(fileName,size),searchNumber,replaceNumber);
readFile(fileName,size)
method, that return an array of double, read from file. The replaceElements method returns a new array with the modifications, that are made. The replacing of elements consists of 2 part : find replaced element and replace it with new element:- public static double[] replaceElements(double[] array,double find, double replace){
- for(int i = 0 ; i!=array.length; ++i){
- if(array[i]==find)
- array[i] = replace;
- }
- return array;
- }
- 2.0 7.0 4.2 3.1 9.4 2.4
- -5.7 8.1 9.2 11.9 27.1 0.0 12.7
- 15.1 4.02 7.65 84.21 1.1 12.1 21.1
Add new comment
- 18 views