can someone describe to me what is happening step by step with this code and describe what its functionality of it is.
@smokeybrown
I'll admit that the syntax looks a bit unfamiliar to me, but I can tell that the code is reading in a lines of text from an input file and storing the lines of text into the variable "temp" temp = in.readLine(); Then, the for-loop is starting from the very end of the line and printing each character in the line in backwards order, from the end to the beginning. So, if the line of text was "SmokeyBrown", that part of the code would take in the line of text and go through each character backwards, starting from 'n', then 'w', all the way to 'S'; the printed output would look like "nworByekomS" It seems that this process continues until there are no more lines to read in, hence the portion: while (temp != null)... To summarize, it seems like this code reads in a file, then takes in each separate line from a file and prints the characters which appear on those lines in reverse order.
Perhaps it would be more useful for you to see each line of the code with an explanation of what it does? I'm happy to go through the code like that as well: String temp; /*This line simply declares the variable "temp" as a String type; this variable is used later in the code to store the text on each line of the File as a String*/ int i; /*Declares the variable "i" as an integer type; "i" is used later in the for-loop as the counter; it would also be valid to declare and initialize "i" within the for-loop instead*/ BufferedReader in = new BufferedReader( new FileReader("IN.txt")); /*Declares new BufferedReader object which is named "in" and also passes in a text file by the name of "IN.txt"; this will allow the code to read in input from the "IN.txt" file*/ PrintWriter out = new PrintWriter( new FileReader("OUT.txt")); /*Declares new PrintWriter object which is named "out" and also passes in a text file by the name of "OUT.txt"; this will allow the code to print and write the output to the "OUT.txt" file*/ temp = in.ReadLine(); /*Reads in the next line of the IN.txt file and stores it into the previously declared temp variable*/ do{ /*Begins a new code-block, which we will see later is actually a while-loop, meaning the block inside the brackets {} continues to execute as long as the condition is fulfilled*/ for(i = temp.length()-1; i>=0; i--){ /*The for-loop starts by defining its pointer and the range of values it will execute; in this case, the range starts at the end of the line stored in temp (temp.length()-1) and continues to execute for every index of the line until the beginning of the line (0)--indexing can get a bit tricky to keep track of; I'll be happy to explain in more detail if needed*/ out.print(tempCharAt(i)); } /*The "current" character is written to the OUT.txt file; since the for-loop starts each line from the end and iterates through to the beginning, each line in OUT.txt will read in reverse to how it was in IN.txt*/ out.println(); /*Once the for-loop is done, there are no more characters on that line, so the code prints a blank line to OUT.txt so that it will continue printing on the next line instead of continuing to print on the same line in OUT.txt*/ temp = in.ReadLine(); /*The process begins again; the next line of IN.txt is read in and stored in "temp". This process will continue until...*/ } while(!temp == null); /*...until "temp = in.ReadLine();" reads in an empty line from IN.txt, signaling the end of the text file. This "while" condition says that the above block will continue reading in lines and printing them in reverse until there are no more lines left to read */ out.close(); /*The code is done writing output, so the FileWriter which was opened before should be closed using this function call. (I think it would also be good to close the FileReader using "in.close()", but we do not see this in the code)*/ } /*Always remember to balance open "{" and closed "}" brackets and parentheses :) */
it seems like its taking each line by char and copying it, in a very primitive way
Join our real-time social learning platform and learn together with your friends!