Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Reading the name of the file from the keyboard  RSS feed

 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
THE TASK IS TO....

Write a program that extracts words from a file. For the purposes of this program, a word is defined as a series of adjacent letters. Only print words that are at least four and no more than 12 letters long. Print each word on a different line.

I KNOW HOW TO DO THAT PART BUT THIS NEXT PART IS KIND OF CONFUSING....

The program should read the name of the file from the keyboard.
************************************************** ************************************************** **************
I need to get the filename from the user for this particular program. Usually I would have the name of the file prewritten into the source code like this....
Scanner input = new Scanner(new File("gettsy_burg.txt");

I tried different ways but I just can't seem to figure it out.... I guess what I'm really asking is how to rearrange Scanner input = new Scanner(new File("gettsy_burg.txt"); since I want the user to input the filename instead of the filename being prewritten by the programmer(me).

This is what I have so far to let the user for input....
Scanner keys = new Scanner(System.in);
System.out.println("Enter the filename: ");
String fileName = keys.nextLine();
keys = new Scanner(new File(fileName));
 
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What you did so far, looks good. So, what's your question about it - does it work as you expected or not?
 
Aron Silvester
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Scanner keys = new Scanner(System.in);
System.out.println("Enter the filename: ");
String fileName = keys.nextLine();
keys = new Scanner(new File(fileName));


while (fileName.hasNext()) {
String oneLine = fileName.nextLine(); //reads the line in the file
System.out.println(oneLine);
int length = oneLine.length();
if (length >= 4 && length <= 12) {
System.out.println(oneLine);
}
}


******
MY QUESTION: the .hasNext inside the boolean expression is underlined in red, it says "Cannot find symbol". What is wrong???
 
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"fileName" is a String object - which has no hasNext method. Perhaps you meant to call hasNext on some other object?
 
Aron Silvester
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Should I change fileName into keys instead, which makes sense since fileName only contains the name of the file not the actual content of the document.

while (keys.hasNext()) {
String oneLine = keys.nextLine();
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You'll need to use an object which has that method. Which objects could you use?
 
Aron Silvester
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But the variable fileName is only a String input which contains the name of the file. Wouldn't I want the the variable that contains the content of the document???
 
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Conrado Sanchez wrote:But the variable fileName is only a String input which contains the name of the file.

Correct.
Conrado Sanchez wrote:Wouldn't I want the the variable that contains the content of the document???

Yes. And that is what you had in your last post. Did that not work ?
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Conrado Sanchez wrote:...

That sounds right (although I'm not sure what you mean by "But..." - it sounds as if you're contradicting something, and I'm not sure what that might be).

What happened when you tried?
 
Aron Silvester
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It wouldn't read the document even though i typed in the name of the file AnalyzingText.txt
I saved the document as AnalyzingText.txt in the folder where my project is located but the loop would never iterate.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What changes have you made to the code as a result of this discussion? You seemed to be on the right track.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!