This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Creating a new file and saving to a specified directory, all with user input (Java)  RSS feed

 
Christopher Sheridan
Ranch Hand
Posts: 50
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wrote a short script to create a file to my Desktop, and the file appeared. I just did it all in main, like so:



I just wanted to make sure the code worked, which it did. After that, I wanted to expand by creating a file with user input, as well as define which directory the user wished to send the file to. I'm on a Linux machine, and I wanted to send it to my Desktop again, so my user input was "/home/christopher/Desktop" for the userPath. Nothing happened. I even cd'd to my Desktop via terminal to "ls" everything there, and still nothing.

Perhaps my syntax is wrong?

If this is a duplicate of anything, my apologies. I tried to do a thorough search before coming here, but I only found info on creating files and sending files to directories that are already defined as a string (e.g. File myFile = new File("/home/User/Desktop/myFileName")).

Here is the expanded attempt:



My print statement for a debug attempt outputs "/home/christopher/Desktop", but not the file name appended to the directory.

Thanks for any help offered. This is just for experimentation while learning Java I/O. Since a hypothetical user may not be on the same OS as me, I can work on those methods later. I'm keeping it on my home machine, hence the Unix filepath names.

 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try using input.next() instead of input.nextLine(). I suspect that the if statement is not being taken. You can add an else clause to the if also to verify this.
 
Christopher Sheridan
Ranch Hand
Posts: 50
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
PLEASE tell me why in the world input.next() worked, but input.nextLine() did not? I thought when entering the file path, it is reading until the end of a line?

I don't know why I have so many bugs with the Scanner class. I have to fire empty input.nextLines often, change to input.next(), etc etc. Silly Scanner has the dumb.

Anyway, that worked like a charm. Now I can expand my code further so it's not platform dependent. Thank you so much. You wouldn't believe how long I've been trying to get this to work!
 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, Scanner has some real quirks.

Scanner#nextLine() does not consume the trailing newline character when you hit return or enter. This newline stays in the buffer and then is consumed by the next nextXXX method (nextLine, nextInt, etc.) and therefore skips any entry you try to make.

I always use next() to get the next String. I only use nextLine() for situations like, "Press <enter> to continue".
 
Christopher Sheridan
Ranch Hand
Posts: 50
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you. I completely understand what you said.
 
Campbell Ritchie
Marshal
Posts: 55751
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't understand why nextLine didn't work. Please show us the exact input. Did you write Y and nothing else? If you write “Y ” the space will be included in the next line and that will make the input not equal.
 
Campbell Ritchie
Marshal
Posts: 55751
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try nextLine().charAt(0) instead which will give the char 'Y'. You will need the == operator rather than equals.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!