I'm trying to figure out a basic thing (I know, "haha") about putting this char Array in reverse order...probably forgetting something. Really confused! Any help would be awesome :'D
package com.codegym.task.task19.task1926;
/*
Mirror image
1. Read a file name from the console. Read the contents of the file.
2. For each line in the file:
2.1. reverse the order of all characters.
2.2. display the result.
3. Close the streams.
Example input file:
I'm a programmer.
Amigo
Example result:
.remmargorp a m'I
ogimA
Requirements:
1. The program must read the file name from the console (use BufferedReader).
2. The BufferedReader used for reading input from the console must be closed after use.
3. The program must read the file's contents (use FileReader).
4. The file input stream (FileReader) must be closed.
5. The program should display all of the characters from the file in reverse order.
*/
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); // first BR
String fileName = br.readLine(); // read from System.in.
br.close(); // close first BR
FileReader fr = new FileReader(fileName); // new FR, reading from the filename String.
BufferedReader br2 = new BufferedReader(fr); // second BR
while (br2.ready()){
String str = br2.readLine();
char[] charArr = str.toCharArray();
for(int i=0; i<charArr.length-1; i++){
char temp = charArr[i];
charArr[i] = charArr[charArr.length -i -1];
charArr[charArr.length -i -1] = temp;
}
for(char item : charArr){
StringBuilder sb = new StringBuilder(item);
System.out.println(sb.reverse());
sb.setLength(0);
}
}
fr.close(); // close filereader
br2.close(); // close second BR
}
}