[Logo]
Forums Register Login
Select a randomly generated file from a folder and displays the file to screen
Hello,

I have a question that I've searched on the internet for, and can't seem to find the answer. I should have asked this question here several days ago to save me some frustration.  I've written 2 small programs (I'm sure it's not the most efficient or theoretical way to write them), to accomplish my task, but I need to merge these ideas to do what I'm really trying to accomplish. Ultimately, I'm trying to do a random selection of files within a folder. Here's the code that accomplished this:



When  run this, I get a random selection of the files that are in this directory. Part 1 is accomplished. This next program takes one specific file from the 'myFiles' directory and opens it using FileReader and BufferedReader:



It does exactly what I want it to do, displays the file.txt file. Now, my problem is that I need to combine both of these programs to do a random search on a file in this directory (first program), and then put that value (the randomly selected file), into the FileReader (unless there is a better way of course) command to display the output of this randomly selected file to the terminal. I've searched on the internet and have seen articles on how to do one or the other, but not both. Please help me understand whats the best way to randomly select a file from a folder, and have that file displayed to the screen?

Thank you
If you are only trying to display one random file out of all the files in a directory, it's pretty straight forward.


Also, I noticed that you attempted to identify your posted code with the "Code" button. You didn't get it quite right. You have to first highlight your block of code and then click the Code button. I'll fix it for you this time. Remember, you can click "Preview" to see if it worked correctly.
And welcome to The Ranch !
I am not sure if the answer here already covered what you're looking for. 

Just open the file you randomly select and read the entire thing into a long String , then call System.out.println(longString);

Alternatively you could read it in parts and call System.out.print(shorterString) or System.out.println(shorterString) in a loop.

System.out.println is of course the call to print it to default output, usually the command line unless you've redirected it somehow.

HTH
 

David Simkulette wrote:Alternatively you could read it in parts and call System.out.print(shorterString) or System.out.println(shorterString) in a loop.


Reading it in parts is more reliable when you don't know how big it will be.
Welcome again to the Code Ranch!

If you want to read more about how to UseCodeTags (<- that's the link) click above the dotted line.
Thank you for the quick responses. I've just got back to the house, and I'm trying the code that Carey suggested. I'm sure that I've misunderstood the instructions because I'm getting an error. I can't use BufferedReader without FileReader right? When I add FileReader with the code that you provided, it seems like both are not needed. I'm posting the code here. Please help with what I've done wrong. Thank you.

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Random;





David,

I would also like to try the solution that you provided, but I can't see how to code it in my mind. If that's something that you could share, it would be greatly appreciated.

Thank you
Close.
Wow, it makes sense! That's it, I'm dusting the webs off of my 'Heads Start' Java, which is amazing by the way, and get back my Java studies. I work in IT, and my duties have pulled me away from Java lately because I'm not a developer, but have doing more of Linux Sys Admin work, but I absolutely love Java, and can't wait to get back to it. I started reading my Head Start book, but didn't get very far, so I'm very excited to know that this forum is available. It's good to be back at the ranch, and I'm never leaving again. Thank you so much for assisting with the first of many conversations that we will have! You'all are the best!!
Here's how a developer might think about that problem.

"There are two steps: (1) Choose a random file; (2) Copy its contents to the console. So let's write the first step and then the second step."

The first step is lines 15-17 and the second step is lines 19-27.

Breaking a problem into smaller pieces. One of the most basic things in the developer's toolbox.
Thank you Paul,

Carey, and team, here is the next phase of the program that I'm trying to complete. So, to recap, the program grabs a specific filename from a directory and displays the contents of the filename. The next thing that I'm trying to do is to ask this question after the contents are displayed; Can you guess the name of this file? Then I put in what I think the name of the file is based on the contents that I see on the console, and if I am correct print out "Perfect match", else, print out "Wrong answer".  Here's the code that I have. it works up to the point where it asks the question, I put in the name of the file, but nothing happens after that.  I think that there is something wrong with my if logic. Please help me figure this out. Also, I've seen examples where the 'break' statement was after the 'else' statement. Would that be ideal for this situation?

Thank you,

You are calling next() twice. you should only call it once and it should really be nextLine().

You are comparing a String to a File. The String is ok but you'll need to get the file name from the File.
Your "else" is missing braces.
Ok, I'm trying to determine which one should use nextLine:

guess = userInput.next();
String input = userInput.next();

From this comment:

"You are comparing a String to a File. The String is ok but you'll need to get the file name from the File."

I'm not sure what I need to do to fix thisl
 

Eric Banks wrote:Ok, I'm trying to determine which one should use nextLine:

guess = userInput.next();
String input = userInput.next();


This depends on which one you want to use in the comparison, then get rid of the other one.

From this comment:
"You are comparing a String to a File. The String is ok but you'll need to get the file name from the File."
I'm not sure what I need to do to fix thisl


I suggest looking at the Javadocs for "File" and see if there's a method that suits your needs.
Ok, here's what I did. It appears to be working now. I used the File.getName() method to get the file name from the file. I think that the last thing that I will attempt to do is to make the program start over once the correct answer has been guessed.

Instead of equals() you might want to use equalsIgnoreCase(). Also, getName() returns the file name including suffix (e.g. ".txt"). Do you expect users to enter the name with suffix also?
Ok, I will use equalsIgnoreCase(). The filenames in for this program are all numbers. So, I was using a strings like, 9th 13th, 23rd, etc. But as I think about it, I will just use the number itself; so 9, 13, 23, etc.
Hi,

I thought that this was working correctly, but I'm noticing that the name of the file comes up as well as the contents when I ask if they recognize the hexagram. I only want the contents of the file to show up, and have them guess what the names is, and compare the name of the file to the value that they input. I also notice that this doesn't always happen. Sometimes, it's just the contents of the file, but sometimes it's the name and the contents that come up after I ask the question:

If it doesn't happen all the time I suspect that somehow the name got added to a couple of the files.
That's exactly what happened, I mistakenly added the name to the top of a few of the files.

Thank you
Hello,

I've added some logic to the program to display the right answer if they select the wrong answer. It works, but it's displaying the full path to the file instead of just the name of the file. I was thinking about the 'trim' command, to trim off everything except the file name, but i can't seem to get it working. Can you provide assistance please? Here's the code:



Here's the results:

Can you guess this Hexagram? 4

Wrong answer
The correct answer is: c:\tmp/myFiles\21

I only want it to display '21'.

Thank you

The top 3 lines are a mistake from a previous copy. Of course, the imports only need to happen one time.
 

Eric Banks wrote:It works, but it's displaying the full path to the file instead of just the name of the file.



But you already used the method which gives you the name of the file, rather than the full path to the file. Why not just use that method again?
I tried to use getName before, but couldn't get the syntax right. Now that you mentioned it, I started trying again and got iit to work this  time; thanks!:

You have too many parentheses in line 4. Not that it's wrong to do that, just a bit off-putting. My guess is that when you tried the first time you had an extra right parenthesis, and to fix the problem you added an extra left parenthesis, instead of removing the extra right parenthesis. But matching parens can be hard, I know. One thing you should know is that (unless you have parens in String literals or something like that) you should always have equal numbers of left and right parens in an expression. So... how about trying to rewrite that line without the extra parens?
Paul,

It's giving me an error when I remove one of the parenthesis on the right.  Maybe I didn't copy it correctly earlier.

The correct answer is:



Your original code, with too many parens? It was still correct, so it must have had balanced parens. So removing one on the right would naturally have unbalanced them. But removing one on the right and one on the left...
I see what' you're saying now. I removed one parenthesis from each side, and it worked.



I didn't realize that they weren't needed.

Thank you
It's good practice (and you might thanks yourself in the future) to print a stack trace when you encounter an exception -- either to a log file or in your case, just to the console.
Thank you for this advise!
Wink, wink, nudge, nudge, say no more ... https://richsoil.com/cards


This thread has been viewed 329 times.

All times above are in ranch (not your local) time.
The current ranch time is
Apr 23, 2018 03:52:00.