-2

I was searching for information on how to write text from Java to a file and I read this question

How do I create a file and write to it in Java?

however, none of the answers included what needs to be imported to make PrintWriter work.

QUESTION: What are the correct import statements for PrintWriter? If this is NOT my issue, please tell me what the issue is and how to solve it.

PLEASE USE PLAIN ENGLISH!! I am amateur coder with zip experience, so explain this to me like you are talking to a 7th grader.

Here is my code:

import java.util.Random;
import java.util.*;
import java.lang.Object;

public class Driver01 {
    public static void main(String[] args) {

        PrintWriter writer = new PrintWriter("the-file-name.txt", "UTF-8");

        for (int b = 1; b < 10001; b++) {
            int x = (int) (Math.random() * 9);
            int y = (int) (Math.random() * 9);
            int z = (int) (Math.random() * 9);
            int a = (int) (Math.random() * 9);

            writer.print(b + ":" + x);
            writer.print(y);
            writer.print(z);
            writer.print(a);
            writer.println("");

            writer.close();
        }

    }

}
Community
  • 1
  • 1
Ungeheuer
  • 1,393
  • 3
  • 17
  • 32
  • 1
    What problem are you having? – Sotirios Delimanolis Aug 03 '14 at 01:43
  • _Just tell me what code I am missing, or point me to an article or something_ Those requests are both off topic. You may want to re-word your _question_. – Sotirios Delimanolis Aug 03 '14 at 01:43
  • 2
    Why are you closing writer inside loop? – Pshemo Aug 03 '14 at 01:44
  • @Pshemo, just realized I did that. But that is not the source of my issue. – Ungeheuer Aug 04 '14 at 04:20
  • @SotiriosDelimanolis My compiler is not recognizing the existence of PrintWriter, it does not recognize the type. I know, or at least I am pretty sure, that this is related to lack of correct imports. Furthermore, my question is NOT off-topic. This site was created so those with coding questions could get answers. My first question is basically: "What am I missing?" I believe the answer is correct import statements, of course I could be wrong, I did mention I am an amateur coder. Therefore, I need to know the correct import statements. If I am wrong, please correct me and tell me what is wrong – Ungeheuer Aug 04 '14 at 04:23
  • @Pshemo, what problems would arise from closing the writer in the loop? Please explain in the comments, direct me to a question, an article, or some material that explains this. – Ungeheuer Aug 04 '14 at 04:31
  • The community felt that your question was unclear. The message block that appears under your question explains why and gives links with information on how to improve it. Our [meta](http://meta.stackoverflow.com/) site can also help if it's still unclear. – Sotirios Delimanolis Aug 04 '14 at 04:31
  • Furthermore, asking for off-site resources is off-topic. You should stop doing that. – Sotirios Delimanolis Aug 04 '14 at 04:32
  • @SotiriosDelimanolis Community? What community? Do you mean four people who can't understand that I am very obviously asking for the correct import statements? – Ungeheuer Aug 04 '14 at 04:33
  • @user3519829 Yes, that's what I mean. Also, we expect you to show some effort. Consider googling `PrintWriter` and checking the first link. That is known as javadoc and describes the class, its methods, its fields, how it works, and the package it's in. – Sotirios Delimanolis Aug 04 '14 at 04:35
  • @SotiriosDelimanolis I did not know that asking for off-site resources is off-topic. What if the full explanation of an answer is not listed on stackoverflow, or the individual answering believes outside documentation provides a more eloquent explanation? What then? Do I sit on my ass and not ask for or provide that documentation? – Ungeheuer Aug 04 '14 at 04:35
  • If you disagree with the community, you can flag comments, edit your question so that it can be reviewed, and you can post on meta to discuss it. – Sotirios Delimanolis Aug 04 '14 at 04:35
  • @SotiriosDelimanolis I did, and I did not understand what I was reading. As I said, zip coding experience. I need stuff very clearly explained to me, and the javadocs, while usually helpful, were, in this case, unhelpful. – Ungeheuer Aug 04 '14 at 04:36
  • Also, you can vote to re-open the question. As I've just done. – Sotirios Delimanolis Aug 04 '14 at 04:50
  • @user3519829 "*what problems would arise from closing the writer in the loop?*" by closing stream/writer/reader you are saying that you don't need connection to this resource any more (in other words you resign from right to get access to it using this particular stream/reader/writer). So if you close stream in one iteration you will not be able to use it in next iteration (because it will be closed). You should close stream after loop. – Pshemo Aug 04 '14 at 10:22
  • @user3519829 about voting to close this question: you need to understand that question beside having code which causes problems also need to have description of problem you are facing like "throws NullPointerException", "hangs in infinite loop". In other words it needs to contain description which could be used by others with similar problem to find your question so they could also read answer to it. Your question wasn't clear about this part. Now after you added "What are the correct import statements for PrintWriter?" question is a lot better so I vote to reopen it. – Pshemo Aug 04 '14 at 10:34
  • 1
    @user3519829 Maybe you didn't know that Stack Overflow goal is to create repository of programming questions and answers. It means that is not to help single person with problem, but it is oriented to helping group of people with similar problem, that is why questions need to be easy to find - pay attention to title you used and make sure you described your problem clearly. Anyway [here is a check-list](http://meta.stackoverflow.com/q/260648/1393766) of questions you need to answer before posting questions written by all-knowing [Jon Skeet](http://stackoverflow.com/users/22656/jon-skeet). – Pshemo Aug 04 '14 at 11:08
  • Anyway here are few things you need to know about `import`: (1) it let us use short name of class like `SomeClass` instead of `full.package.name.of.SomeClass`, so if you do `import full.package.name.of.SomeClass` you are able to write code like `SomeClass sc = new SomeClass();` instead of `full.package.name.of.SomeClass sc = new full.package.name.of.SomeClass();`. – Pshemo Aug 04 '14 at 11:30
  • (2) Some classes do not need to be imported, and you still will be able to use their short names instead of full package names. I'm talking about classes from `java.lang` packages: `String` `Integer` and classes from same package as class you are creating, so if you are coding `foo.YourClass` you don't have to use inside it `foo.Bar` (class from same package as `YourClass` which will be `foo`) but you can simply use `Bar`. All other cases requires import. – Pshemo Aug 04 '14 at 11:33
  • @SotiriosDelimanolis wonderful. now because of these downvotes, when I really need help for the computer science class I am taking, I cant ask questions on here anymore. How about you upvote this so I can post my question. I really need help and need to post a question. It has to do with do-while loop. Mine is not executing even when the conditions are satisfied. – Ungeheuer Oct 16 '14 at 01:06

2 Answers2

2

I'm going to attempt to answer the question(s) I think you're asking, plus offer some (possibly redundant) advice on StackOverflow questions -- this doesn't look like your first time here, exactly, but I can see that you're new.

The reason posts don't necessarily tell you what to import is that, if you don't know already, a simple google search for "java Classname", substituting the class you want, will tell you. In this case, PrintWriter is in the java.io package, so the import is java.io.PrintWriter.

As was pointed out in the comments, you probably don't want to close the PrintWriter you've instantiated until after you are out of the loop. As the code stands, I would expect it to crash the second time through the loop on the first statement that attempts to write to the file.

To ask good StackOverflow questions, try to keep in mind that we don't know what you're trying to do, we don't know what you've tried, and we don't know what your question is until/unless you tell us. SO lurkers don't tend to write programs for people, especially people who have not shown us what they've tried (and that they HAVE tried); this helps further define the question, and tells us what not to try or suggest ourselves.

When you have a little program like this, post the whole thing and tell us it is the whole thing.

If you have a compiler error, paste the entire compiler error into your question and say what you want to know about it.

If you have a runtime stacktrace, paste the entire stacktrace into your question and say what you want to know about it.

If the program is simply not doing what you expect, tell us (1) what it is doing, and (2) what you expect.

Often in the course of this, after you've done it for a while, you will find your own answer to your question before you've posted it; it is one way SO helps, by guiding us to focus on the problem in a way that questions can be answered.

Good luck.

arcy
  • 12,845
  • 12
  • 58
  • 103
  • Thank you. In just about every post, I attempt to make it patently clear that I am new to this site and that people should tell me if my question is bad (as in posted badly) and to tell what to change. Instead, they completely ignore the fact that I need help, they tell me my question is crap, they downvote me (I am now close to being banned from asking questions apparently), and close the question so that I cant get help. I appreciate you clarifying the proper technique to writing a good question on this site. You are a God among coders. – Ungeheuer Aug 04 '14 at 04:27
  • And as you can see arcy, this question has already been placed on hold because there are those who would rather just mark this question bad and move with their lives while I sit here wondering what to do. – Ungeheuer Aug 04 '14 at 04:29
2

Well, you need to import PrintWriter itself.

import java.io.PrintWriter;

After that, you'll run into another problem. PrintWriter can throw some exceptions which you'll need to handle. The lesson I've linked should be more than enough to get you going with that.

That should pretty much cover it.