Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp

Eric Banks

Greenhorn
+ Follow
since Nov 23, 2017
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Eric Banks

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
1 year ago
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.

1 year ago
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!:

1 year ago
The top 3 lines are a mistake from a previous copy. Of course, the imports only need to happen one time.
1 year ago
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

1 year ago
That's exactly what happened, I mistakenly added the name to the top of a few of the files.

Thank you
1 year ago
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:

1 year ago
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.
1 year ago
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.

1 year ago
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
1 year ago
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,

1 year ago
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!!
1 year ago
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
1 year ago
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;





1 year ago