I did the solution with the help of the new feature "download correct solution".
Who can explain how it works and what it doing? I tried to run that code in Idea, but there is nothing coming out to console,despite it passing validator - i am stil not fully understand what it doing.
Even debuging didn't help.
package com.codegym.task.task13.task1318;
import java.io.*;
import java.util.Scanner;
/*
Reading a file
*/
public class Solution {
public static void main(String[] args) throws IOException {
// write your code here
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); //reading from console
String g = br.readLine();//putting it to variable
FileInputStream stream = new FileInputStream(g);// searching "variable path" on the disk
Scanner scanner = new Scanner(stream); // USUALLY FOR USING IT HAS TO BE SYSTEM.IN INSIDE
StringBuilder builder = new StringBuilder();// I've read what it is,we want to build String
while (scanner.hasNextLine())
{
builder.append(scanner.nextLine()).append("\n");//why we using 2 append,and what the heck is "\n"
}
System.out.println(builder.toString());// nothing coming to console,but validator satisfied, but not me,i didn't get it :(
scanner.close();
br.close();
}
}
Anatoly
Level 17
Who can explain?
Archived
Comments (3)
- Popular
- New
- Old
You must be signed in to leave a comment
Nouser
24 February 2021, 10:20
Escaping characters
0
Gellert Varga
24 February 2021, 00:36useful
When i did this task on webIDE, it said it couldn't run there.
The validator didn't run your program either, this is the reason why there was no output. He just checks to see if it's good.
CodeGym doesn't run this program, because the program ask a real file name with a real path and it would actually need to open that file from your hard drive. To do this, I don't know what kind of IP address path to enter, for purpose to run the program on CodeGym server, and to be able to find your file on your machine via net.
I test these programs on my machine by running on the "commandline". (I don't like IntelliJ IDEA ...) Then the console will ask for the name of the file, I will type it, and then the program will read the contents of the specified file.
This code:
builder.append (scanner.nextLine ()). append ("\n")
- "append" means like "+"
- "\n" means a new line char
Overall, the previous line of code means something like this here:
String str = str + nextLine + a newLineCharacter
The nextLine() method (or the readLine() method from the Reader classes) reads and sends to you a line, but it cuts the "endOfTheLine" char from the end of the line, so you must to add one "manually".
Without this for example your inputfile contains ten lines, but the output would be only one long line.
FileInputStream stream = new FileInputStream(g); // the argument "g" is a string. This string must contain a filename with path, something like this: "C:/TEST/inputfile.txt"
After getting the filename, it creates an inputstream to this file.
The scanner will use this stream as source.
+1
Anatoly
24 February 2021, 19:38
Thank you very much. It became much clearer now!
0