aspose file tools*
The moose likes Beginning Java and the fly likes Scanning/Storing a String that the user enters? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Scanning/Storing a String that the user enters?" Watch "Scanning/Storing a String that the user enters?" New topic
Author

Scanning/Storing a String that the user enters?

Matt Hazan
Greenhorn

Joined: Jan 27, 2011
Posts: 29
Hello everyone,
I am completely new to java and have some experience with C programming.

I am trying to prompt the user for a filename that I am going to read from. More specifically, I am trying to STORE the filename that the user enters in a String Variable, and then read from the filename using that String Variable.


Any help will be appreciated,

Thanks in advance.

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18655
    
    8

Okay... and I assume you must have some question about that code? Could you let us know what that question is? Read this: TellTheDetails for more information about how to ask a question here.

I see this is your first post here... welcome to the Ranch!
Markas Korotkovas
Greenhorn

Joined: Jan 27, 2011
Posts: 21

I don't think Scanner has a method nextString(). Try the next() method instead.
Matt Hazan
Greenhorn

Joined: Jan 27, 2011
Posts: 29
Markas Korotkovas wrote:I don't think Scanner has a method nextString(). Try the next() method instead.


Thank you, This worked like a charm haha
Matt Hazan
Greenhorn

Joined: Jan 27, 2011
Posts: 29
Paul Clapham wrote:Okay... and I assume you must have some question about that code? Could you let us know what that question is? Read this: TellTheDetails for more information about how to ask a question here.

I see this is your first post here... welcome to the Ranch!


I did explain the details of my question above the code that I posted...

Thanks for the welcome :)
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11406
    
  16

You explained what you were trying to do...but not what the problem was. Does the code you provide compile? Do you know how to compile? does it run and throw and exception? Does it run and produce the wrong answer? If so, what answer did you expect and what did you get?

You say "i'm trying to store the filename". Does that part work? If not, are you not able to get the value from the user? Are you not able to store it?

Did you get it saved to a variable?

Are you having problems using it to open the file?
...

I could go on, but I hope I've made my point. You really do need to TellTheDetails, since they are critical in knowing how to help.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Matt Hazan
Greenhorn

Joined: Jan 27, 2011
Posts: 29
Hi again,
Okay so let me try to explain in more detail my second problem.
I was able to store the filename prompted from the user in a String Variable called 'filename', I am trying to check whether or not that filename exists and if it does real all the data from it and print it out.

Right now I am getting an error and my program will not compile.
The error I am getting is: "Illegal modifier for parameter x; only final permitted."

The filename kind of looks this:
17
20
8


My main questions are: How do I get rid of the error and why am I getting it?
If I create a new.txt file in the same SRC folder as the program that reads from it, can I only type in the filename or do I need to type in the whole path?




Thanks in advance

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18655
    
    8

Matt Hazan wrote:Right now I am getting an error and my program will not compile.
The error I am getting is: "Illegal modifier for parameter x; only final permitted."


You didn't say which line of code caused that exception. That's important information too. The compiler doesn't produce these errors just to annoy you, it produces them so you can identify a problem and fix it. So please post all of the information next time.

However now that you tell us the error message, it's possible to see that it's line 16 in your posted code. (Correct?) You can't declare a local variable as private; that doesn't make sense in Java. Only class-level variables can be declared private. As the error message says, the only modifier allowed is "final". You don't need it to be final, so just don't declare it as "private".
Matt Hazan
Greenhorn

Joined: Jan 27, 2011
Posts: 29
Thanks for the response.

Ok so I got rid of the private part. Now I am getting an error saying that I did not declare variable x correctly. Can someone please help me understand what I did wrong? I have been playing around with for a while, trying to figure out if there is an alternate way to declare it, but I am unable to.

Errors are on line 40, 36, and 37.


Thanks in advance.

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18655
    
    8

Matt Hazan wrote:Now I am getting an error saying that I did not declare variable x correctly.


It doesn't really say "You didn't declare variable x correctly", does it?

Come on, Matt, we have been asking you to provide useful information all through this thread. Don't just provide paraphrases of the error messages, copy and paste them. This is computer programming and accuracy is important.
Matt Hazan
Greenhorn

Joined: Jan 27, 2011
Posts: 29
Paul Clapham wrote:
Matt Hazan wrote:Now I am getting an error saying that I did not declare variable x correctly.


It doesn't really say "You didn't declare variable x correctly", does it?

Come on, Matt, we have been asking you to provide useful information all through this thread. Don't just provide paraphrases of the error messages, copy and paste them. This is computer programming and accuracy is important.


Sorry... the error says that "Variable x may have not been initialized correctly".
Markas Korotkovas
Greenhorn

Joined: Jan 27, 2011
Posts: 21

You might want to initialize the x with a null to begin with and move the x.close() into a finally block.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11406
    
  16

The problem is exactly what the error says. What happens if line 30 throws an exception? Your 'x' variable (which is a HORRIBLE name for that) will NOT be initialized. You then get to line 36, where indeed x HASN'T been initialized.

You could put the 'while' inside the 'try' block. That way that code would only run if we did get x initialized.

You have a similar problem with line 40. There is no reason to close x if it never gets created - and in fact, doing so would cause a null pointer exception...
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39396
    
  28
Welcome to the Ranch, too, Markas Korotkovas
Matt Hazan
Greenhorn

Joined: Jan 27, 2011
Posts: 29
Thank you Fred! Your advice definitely got rid of the errors. I am sorry for the really 'Beginner no-brainer' questions that most coders might find especially annoying.
It takes me a while to really get the hang of how the code works and the logic behind the code. But, the more questions I ask, the more mistakes I make, the faster I learn


I appreciate your advice.


I have another question. When it comes to reading from a file, and prompting the user to enter the file name that he would like to read from, how does it work in java? In C programming you can simply place the .txt document in the same directory as the program, and the program automatically picks up on it. So basically my question is, IF i want the user to simply input "filename.txt" and nothing else, and I want the JAVA program to pick up on it, How would I do that?

What I tried to do is create a new .txt file with Eclipse and place it in the same directory as the .java program that searches for it. However, it did not find the file:/

Any advice?



This is my newest code:

Thank you
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11406
    
  16

and again i ask what happens with this code? Does it compile? Does it run and throw an error? Does it run and do something you don't expect?

You say "it did not find the file". How do you know that? I'm going to guess it's because your program prints "could not find file". The problem here is that you catch EVERY POSSIBLE KIND OF EXCEPTION in one catch block, then basically ignore what that exception was and print a generic, unhelpful message.

without knowing what kind of exception was thrown, you can't really diagnose the problem. at the very least, you should do an "e.printStatckTrace" to see what the exception was.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18655
    
    8

Matt Hazan wrote:I am sorry for the really 'Beginner no-brainer' questions that most coders might find especially annoying.

Yes, those same questions show up all the time. We all had to work through them. You are not alone.
I have another question. When it comes to reading from a file, and prompting the user to enter the file name that he would like to read from, how does it work in java? In C programming you can simply place the .txt document in the same directory as the program, and the program automatically picks up on it. So basically my question is, IF i want the user to simply input "filename.txt" and nothing else, and I want the JAVA program to pick up on it, How would I do that?

What I tried to do is create a new .txt file with Eclipse and place it in the same directory as the .java program that searches for it. However, it did not find the file:/

That's not a feature of Java or of C. It's a feature of the operating system called "Current Working Directory". You can set the current working directory with the "cd" command in pretty much any of the common operating systems in use these days. If you don't specify the full path for a file, then that file defaults to being in the current working directory. Eclipse sets your current working directory to something related to the project you're working in, but I'm not quite sure what it is.
Any advice?

Yes, don't assume you know what the current working directory is when you're writing a program. And there's really not much point in writing a file into the directory where your source code is stored. That is hardly ever going to be a real-life requirement. So get rid of that distraction; choose a directory to write your file to -- maybe even create one specifically for this project -- and specify its full path.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Scanning/Storing a String that the user enters?