+ 1

What does this code?

I played around with the code below. The game was "create another error". Still no clue how to use it. And I need to use it. Some hints maybe?

27th Mar 2017, 7:21 PM
Alex Snaidars
Alex Snaidars - avatar
25 Answers
+ 18
As far as I can tell, it reads in the text from input.txt one line at a time and then writes that line to output.txt.
27th Mar 2017, 8:36 PM
Jafca
Jafca - avatar
+ 18
I think it should be writer.println(cardDetails) Also, leave your comments so others can see how you ended up solving your problem 🍰
27th Mar 2017, 9:19 PM
Jafca
Jafca - avatar
+ 18
Dunno about Java but in C#, if you try to write to a file that doesn't exist, it creates one for you and then writes to it
27th Mar 2017, 9:51 PM
Jafca
Jafca - avatar
+ 18
Just Compilation error. I thought code playground would be able to compile it, at least. If the file was wrong, wouldn't there be a runtime error?
27th Mar 2017, 10:51 PM
Jafca
Jafca - avatar
+ 17
@Ace I think he meant his "answer" to this post 😀
27th Mar 2017, 9:51 PM
Jafca
Jafca - avatar
+ 17
There's a share button in the top right of code playground
27th Mar 2017, 10:37 PM
Jafca
Jafca - avatar
+ 17
I'm getting Compilation error
27th Mar 2017, 10:45 PM
Jafca
Jafca - avatar
+ 17
I'll just take your word for it that it works 😄
27th Mar 2017, 10:50 PM
Jafca
Jafca - avatar
+ 16
Make sure you're using writer.println(cardDetails), writer.flush() and writer.close()
27th Mar 2017, 10:08 PM
Jafca
Jafca - avatar
+ 3
So basically if I need to record into a file what a user types, I just need this part of code aren't I? FileOutputStream fos = new FileOutputStream("output.txt");           PrintWriter writer = new PrintWriter(fos); and System.out.println("Tell me your debit card number and the pin. Just for fun."); Scanner scanner2 = new Scanner(System.in); long cardDetails = scanner2.nextLong(); writer.println(cardDetails); I'll try to do it. If this comment stays, the try was unsuccessful.
27th Mar 2017, 9:09 PM
Alex Snaidars
Alex Snaidars - avatar
+ 3
The good news is, it compiled. What I am worried about is how does the program should access output.txt? Even java compiler needs exact directory. No attempts to record sth into the file via the program were successful. It doesn't even mind that I do so: FileOutputStream fos = new FileOutputStream("HolyBible.txt"); It compiled despite the fact that I am an atheist nor there doesn't exist such a file on my PC.
27th Mar 2017, 9:44 PM
Alex Snaidars
Alex Snaidars - avatar
+ 2
FileWriter.java import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.PrintWriter; import java.util.Scanner; public class FileWriter { public static void main(String[] args) throws FileNotFoundException {           Scanner scanner = new Scanner(new File("input.txt"));           FileOutputStream fos = new FileOutputStream("output.txt");           PrintWriter writer = new PrintWriter(fos);           while (scanner.hasNext()) {                String line = scanner.nextLine();              writer.println(line);    }            writer.flush();    writer.close(); } }
27th Mar 2017, 7:21 PM
Alex Snaidars
Alex Snaidars - avatar
+ 2
A little improvement. The file is automatically created and the problem is in writer() function, i think
27th Mar 2017, 10:05 PM
Alex Snaidars
Alex Snaidars - avatar
+ 2
Yup. Done. Thanks for great help! --- import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.PrintWriter; import java.util.Scanner; public class FileWriter { public static void main(String[] args) throws FileNotFoundException {           FileOutputStream fos = new FileOutputStream("output.txt");           PrintWriter writer = new PrintWriter(fos); System.out.println("Say something "); Scanner scanner = new Scanner(System.in); String sth = scanner.nextLine();         writer.println(sth);    writer.flush();    writer.close(); } }
27th Mar 2017, 10:28 PM
Alex Snaidars
Alex Snaidars - avatar
+ 2
How do I copy the link in the app?
27th Mar 2017, 10:35 PM
Alex Snaidars
Alex Snaidars - avatar
+ 2
https://code.sololearn.com/cMO9TgASSJUw/?ref=app You can't see the file so it is useless in the app.
27th Mar 2017, 10:42 PM
Alex Snaidars
Alex Snaidars - avatar
+ 2
@Jafca Give it a try in a special IDE
27th Mar 2017, 10:48 PM
Alex Snaidars
Alex Snaidars - avatar
+ 2
Anyways, you know where to find it if needed. Thanks for the help. Good night, morning or evening.
27th Mar 2017, 10:57 PM
Alex Snaidars
Alex Snaidars - avatar