2

How do I name a file using Scanner? For some reason, Java's FileWriter won't accept a pre-defined String as an input. The code below is simplified to what I mean : `

import java.io.*;

public class Example{
    public static void main(String[] arg) {
        String title;
        Scanner input = new Scanner(System.in);

        title = input.next();
        try {
            FileWriter fw = new Filewriter(title);
        } catch (IOException ex) {}
        input.close()
    }
}

I don't know why it doesn't work. I've checked other questions online and none of them seem to help.

EDIT: Okay, I edited the IOException and here's what I got from the stack trace;

java.io.FileNotFoundException: entries\awdwad
.txt (The filename, directory name, or volume label syntax is incorrect)
    at java.base/java.io.FileOutputStream.open0(Native Method)
    at java.base/java.io.FileOutputStream.open(FileOutputStream.java:292)
    at java.base/java.io.FileOutputStream.<init>(FileOutputStream.java:235)
    at java.base/java.io.FileOutputStream.<init>(FileOutputStream.java:156)
    at java.base/java.io.FileWriter.<init>(FileWriter.java:82)
    at main.JournalInput.main(JournalInput.java:26)

I'm sorry, I'm still confused about this.

EDIT EDIT: I found out the problem. There was a Delimiter I forgot and removing it apparently fixed it. I didn't put it in the code above either. Sorry for wasting everyone's time on this.

  • 3
    You should include more information than just *"won't accept"* - is it an error (if so, show us the full error message and stack trace), does it give wrong results (if so, what is the expected vs. actual result), or something else? Also, `Filewriter` is misspelled; it should be `FileWriter` with a capital `W`. – kaya3 Dec 19 '19 at 11:08
  • 2
    `Java's FileWriter won't accept ...` what does that mean? don't silently ignore `IOException`, it probably have answer to your question – rkosegi Dec 19 '19 at 11:08
  • And what do you mean "name a file"? – George Z. Dec 19 '19 at 11:09
  • Does this answer your question? [Rename a file using Java](https://stackoverflow.com/questions/1158777/rename-a-file-using-java) – George Z. Dec 19 '19 at 11:09
  • the code does not compile because of the spelling error pointed out by kaya3. You could add a System.out.println(title); to see if it is the expected file name and also print the stack trace of the exception you receive – stacker Dec 19 '19 at 11:11
  • Can you paste the error message received in the catch – Pie Dec 19 '19 at 11:12
  • @AmerulAtiq: it's better to edit the question to include that, as it can easily be missed in the comments down here. – Joachim Sauer Dec 19 '19 at 11:25
  • You're using a relative path. Does the directory `entries` exist in the current working directory of your application? – Mark Rotteveel Dec 19 '19 at 11:27
  • what filename are you passing? – Shubham Singh Dec 19 '19 at 11:33
  • I tried your code with corrected FileWriter spelling, it is working fine and creating a file with name picked by scanner – Shubham Singh Dec 19 '19 at 11:35

2 Answers2

1

Scanner parses text into tokens, x.txt will easily become 3 tokens. Instead of next() use nextLine(). Still better would be to not use Scanner.

A FileNotFoundException under Windows also happens a lot, when the file extension is hidden (.txt, .docx, ...).

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
1

Mostly depends of your input to scanner for example if you will pass filename along with / then it won't work. Posting same code tested on three different inputs.
1. example
2. example.txt
3. /example.txt

public class Example {


     public static void main(String[] arg) {
            String title;
            Scanner input = new Scanner(System.in);

            title = input.next();
            try {
                FileWriter fw = new FileWriter(title);
                System.out.print("Working with "+title);

            } catch (IOException ex) {
                System.out.print("Error: with "+ex);
                ex.printStackTrace(System.out);
            }
            input.close();
        }

}

will work on first two input but will throw exception in case of last input.

Afsar edrisy
  • 1,985
  • 12
  • 28