• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Reading from a File

 
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have been given the task of reading some numbers from a files I created. The file I created only has numbers in it and is saved in the folder of the NetBeans project. My assignment has given me some information, but I do not understand it at all. I was wondering if someone could break it down to a more simple format for me.

Reading a file can cause exceptions, for instance, if the file is not found.  Exception handling was discussed last week, but for this assignment, you do not have to write code to handle the exception.  Instead, just have main "throw" the exception as follows:
public static void main(String[] args) throws IOException, FileNotFoundException {

and if you have a separate method that actually does the file processing, it should also have the throws clause:
public static void processFile (String filename) throws IOException, FileNotFoundException {

Throwing the exception basically returns the exception to the method that calls it.  Your main method calls processFile and processFile throws the exception, if it occurs, back to main, which in turn throws it to the runtime system, causing the program to terminate. To read the file, you will set up a BufferedReader as follows:
BufferedReader inputReader = new BufferedReader (new InputStreamReader(new FileInputStream(filename)));

This line of code creates an input stream to the file specified by the filename (a string, ie: "c:\\myfile.txt").  You can think of this like a straw that draws the contents of the file out, line by line.

Several methods are available:

readline - used to read one line from a file, terminated by a line return.  Regardless of the contents of the file, the line will be read as a string.
close - closes the file
In a loop, you will call readLine to read each line in the file:
while (( line = inputReader.readLine()) != null)

where line is declared as a string.  The method readLine returns null when it reaches the end of the file.

Note especially that whenever files on disk are read with a BufferedReader, the lines of data are in fact strings. If the file actually contains numbers that you want to do calculations with, you will need to cast (convert) them from type String to type int or double, after they are initially read from the file.  This example demonstrates how to parse a double from the string line:
doubleValue = Double.parseDouble(line);

Once you are done reading all the lines from the file, the loop will terminate.  At this point, the file needs to be closed or resources will leak:
inputReader.close();


I understand what I have to do, but with these guidelines I do not understand how to do it.
 
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Lexi Turgeon wrote:My assignment has given me some information..

Apart from the information you have been given written code already.

Lexi Turgeon wrote:..but I do not understand it at all.

So what specifically you do not understand? A bit strange, because at least what you could do, is to search on google "How to read file in Java", and by comparing search results with the given instructions to you - you should be able to assemble the app. Almost just assemble.

Lexi Turgeon wrote:I understand what I have to do, but with these guidelines I do not understand how to do it.

As I said, you have been given almost all code, so, without giving away exact output, it is hard to give some hints.

Do you know how to create a class? Create one and show us. Only the class.
 
Lexi Turgeon
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I do not understand where I am supposed to start my code. I do not really get if everything given goes in the same method or if I have to write new methods.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have already been told whether you need one method or several.
 
Lexi Turgeon
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do I have all the code I need here or do I have to come up with other code on my own?
 
Liutauras Vilda
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Lexi Turgeon wrote:I do not understand where I am supposed to start my code.


For now is enough to understand, that all Java methods need to be part of a class. So once again...

Earlier I wrote:Do you know how to create a class? Create one and show us. Only the class.

 
Lexi Turgeon
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Lexi Turgeon wrote:Do I have all the code I need here or do I have to come up with other code on my own?



Two points. One. None of your previous assignments are in isolation. If you are having issues "getting started", perhaps, it would be a good idea to look at your previous assignments. Maybe some of the stuff, that you should have learned earlier, didn't "stick" -- and you might need to go back and review.

Two. Of course, you need to "come up with other code on [your] own". After all, that is what being a developer is about. However, admittedly, your instructor is pretty generous here. He/she pretty much done most of it. You just need to put the pieces together.

Henry
 
Liutauras Vilda
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Lexi Turgeon wrote:


So, you have created a class, great. Not sure why you have added that import statement there yet, don't go too far, one step at a time, and don't forget to compile after each step. I hope you tried it and it compiles?

1. Now in order to run the program, class needs to have a 'main' method, right? Please add main method declaration. Look for the hint in the given instructions.
2. After you add 'main' method, can you try to add all other methods suggested in your instructions?

After that repost your code to see if you can handle it.
 
Lexi Turgeon
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:

Lexi Turgeon wrote:


So, you have created a class, great. Not sure why you have added that import statement there yet, don't go too far, one step at a time, and don't forget to compile after each step. I hope you tried it and it compiles?

1. Now in order to run the program, class needs to have a 'main' method, right? Please add main method declaration. Look for the hint in the given instructions.
2. After you add 'main' method, can you try to add all other methods suggested in your instructions?

After that repost your code to see if you can handle it.



I believe I have added all other methods that were in my instructions.
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Lexi Turgeon wrote:
I believe I have added all other methods that were in my instructions.



Yup, you correctly did the first paragraph of your instructions (well, almost).

Your next paragraph in the your instructions are ...

Lexi Turgeon wrote:and if you have a separate method that actually does the file processing, it should also have the throws clause:
public static void processFile (String filename) throws IOException, FileNotFoundException {



Can you take a shot at that?

Henry
 
Lexi Turgeon
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:

Your next paragraph in the your instructions are ...

Lexi Turgeon wrote:and if you have a separate method that actually does the file processing, it should also have the throws clause:
public static void processFile (String filename) throws IOException, FileNotFoundException {



Can you take a shot at that?

Henry



 
Liutauras Vilda
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Lexi

Yeah, it seems fine so far. So, read through the next part of instructions and think what needs to be done.
Hint: the rest specified code (in instructions) needs to be used in one particular method you have created already.

So, try to assemble the last specified code. How that will be looking like?
Note: in order not to have compilation errors, you'll need to add some little bits from your side - declare variables.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Lexi Turgeon wrote:I have been given the task of reading some numbers from a files I created. The file I created only has numbers in it and is saved in the folder of the NetBeans project. My assignment has given me some information, but I do not understand it at all.
...
I understand what I have to do, but with these guidelines I do not understand how to do it.


Well if so, you should be halfway there, because what is much more important than 'how'.

I don't want to disrupt the excellent advice you're already being given, but just to prove to me that you DO understand what your program is supposed to do, try explaining it to me (or us) in English - ie, NOT in Java.

Just as a guide:
  "read lines from a file"
is the 'what'.
  while (( line = inputReader.readLine()) != null) ...
is the 'how'.
(and it's only one of many ways of doing it - which is quite common for "how" code)

I suspect that part of your problem is that you're already thinking about the 'how' before you have the 'what' completely sorted out; and what Liutauras is trying to show you is how to deal with one thing at a time.
It can be a very tough lesson to learn when you're starting out, because you're just dying to get started on the "'how' code" - ie, the stuff inside methods - but trust me, taking a bit of time to really understand the 'what' first will help you a lot in the future.

You may also find this article worth reading.

HIH

Winston
 
Lexi Turgeon
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:@Lexi

Yeah, it seems fine so far. So, read through the next part of instructions and think what needs to be done.
Hint: the rest specified code (in instructions) needs to be used in one particular method you have created already.

So, try to assemble the last specified code. How that will be looking like?
Note: in order not to have compilation errors, you'll need to add some little bits from your side - declare variables.



This was as far as I got. Does this seem correct so far?

 
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can't use grades.txt. If you want a String literal it would have to be "grades.txt" (in quotes). If you want a variable you might choose something like fileName.
 
Lexi Turgeon
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The file i am trying to read from is a file called "grades.txt", would I have to put that in quotes in the code?
 
Carey Brown
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Lexi Turgeon wrote:The file i am trying to read from is a file called "grades.txt", would I have to put that in quotes in the code?



and if you have a separate method that actually does the file processing, it should also have the throws clause:
public static void processFile (String filename) throws IOException, FileNotFoundException {



Always refer back to your requirements.
 
Liutauras Vilda
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Lexi Turgeon wrote:This was as far as I got. Does this seem correct so far?

Greate Lexi, you're doing some progress already. However, Carey already pointed out one thing. A bit more in details that would mean, that you're going to call method 'processFile' and you're going to supply a full path to file as an argument. Without giving away all details, that will look similar to:
Can you try to incorporate the idea to your current code?

However, you didn't compile your code often enough, because you have line 12 which shouldn't compile. The sooner you could notice all compile errors, the easier are to fix them. Some people say you should compile your code after every newly written 5 - 10 lines.  Try to follow this approach, that needs to be as a habit probably.
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:
Always refer back to your requirements.



Agreed. The OP seems to be constantly deviating from the requirements during implementation. I understand getting stuff to compile, and "working" is a priority during implementation, but deviating from requirements means "work as required" is no longer the target.

Henry
 
Lexi Turgeon
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:Can you try to incorporate the idea to your current code?

However, you didn't compile your code often enough, because you have line 12 which shouldn't compile. The sooner you could notice all compile errors, the easier are to fix them. Some people say you should compile your code after every newly written 5 - 10 lines.  Try to follow this approach, that needs to be as a habit probably.



 
Liutauras Vilda
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

instructions wrote:set up ... as follows...


instructions wrote:


That could be important if your teacher is going to check if you can follow the instructions as they appear. I'm having in mind parameter name, which in this case is 'filename' rather than 'pathToFile' (as I suggested for a demo purpose).

Another 2 points.
First. You still don't have a habit to compile code, this has been mentioned multiple times already. Line 12 wouldn't compile. Why you ignoring it?
Second. Very important. Winston basically raised very good point. Do you actually understand what this program is meant to do? Could you please describe in English?

All your work looks very mechanical. Because you don't have any other questions, it seems these don't pop up at all to you. That worries me a bit.

Do you have anything else to do according to your instructions?

[edit] fixed 2 typos
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A file input stream will probably work for a simple text file, and you will have to use it because you have been told to, but you shou‍ld really use a file reader for a text file. When you have finished your assignment, have a look at the appropriate Java™ Tutorials section.
 
Lexi Turgeon
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:

instructions wrote:set up ... as follows...


instructions wrote:


That could be important if your teacher is going to check if you can follow the instructions as they appear. I'm having in mind parameter name, which in this case is 'filename' rather than 'pathToFile' (as I suggested for a demo purpose).

Another 2 points.
First. You still don't have a habit to compile code, this has been mentioned multiple times already. Line 12 wouldn't compile. Why you ignoring it?
Second. Very important. Winston basically raised very good point. Do you actually understand what this program is meant to do? Could you please describe in English?

All your work looks very mechanical. Because you don't have any other questions, it seems these don't pop up at all to you. That worries me a bit.

Do you have anything else to do according to your instructions?

[edit] fixed 2 typos



When I compile, I do not know how I would fix the compiler errors.

My instructions are:
Use Notepad or another text editor to create a list of scores.  They can be grades, sports scores, whatever you choose.   Be sure to enter each number on a separate line and then hit the Enter key. Save the original data file in your Netbeans project node (top level in that NetBeans project folder) as "scores.txt".  There should be at least 20 values.

Create a method called processFile as declared above and call it from main passing the name of the file that you created ("scores.txt").  In processFile,  set up the BufferedReader and loop through the file, reading each score.  Convert the score to an integer, add them up, and calculate and display the mean.


I am so lost with this assignment.
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Lexi Turgeon wrote:
I am so lost with this assignment.



Yeah. We kinda figured that part out ... over the last two days, you are literally playing robot. Ignoring posts that goes into discussions. And doing stuff, only if they are exactly specified, and with no feedback (or any comments) whatsoever.

To be honest, this is not good. It means that you don't understand what you are doing (even for stuff that you did correctly).

Henry
 
Carey Brown
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Lexi Turgeon wrote:Create a method called processFile as declared above and call it from main passing the name of the file that you created ("scores.txt").  In processFile,  set up the BufferedReader and loop through the file, reading each score.  Convert the score to an integer, add them up, and calculate and display the mean.


Are you converting each score too an integer? (This is simply a matter of following your instructions. Re-read your instructions often and make sure each line of code is there to achieve your goals.)

I assume you know what a sum is, do you know how to create a sum in Java?

Do you know what a "mean" is? What data do you need to calculate a "mean"?
 
Liutauras Vilda
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I am so lost with this assignment.

A bit unfortunate and sad.

1. By the way, is it secondary school assignment? College? University?
2. How long programming classes lasted until you got this assignment?
3. Did you attend all of them?

Sorry for asking that, but trying to understand a bit of a background. You don't need to answer if you don't want to though.

Back to assignment.

One more thing just popped up as you didn't show us actual instructions, or these are just extended ones (not sure about that). Because:

Today's instructions wrote:Convert the score to an integer, add them up, and calculate and display the mean.


Previously provided instructions wrote:This example demonstrates how to parse a double



How else we could help you? I'm willing to, but don't know at the moment how. You probably also need to spend a bit more of time personally studying, like looking info via google, reading some books, that should give some light I believe.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:. . . Are you converting each score too an integer? (This is simply a matter of following your instructions. . . .) . . .

It shou‍ld only take a minute to convert the double you are using at present to an integer.
 
Lexi Turgeon
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:
1. By the way, is it secondary school assignment? College? University?
2. How long programming classes lasted until you got this assignment?
3. Did you attend all of them?



This is an assignment for a Virtual High School class.
I have been in this class since mid-September.
I do not attend them as they are online, the teacher and students offer little to no help at all.

But I have gotten this far with the coding.


My problem I think comes from my code not recognizing my file I am trying to read from. I have put it in the NetBeansProjects folder and into the Folder for this project. The name of the file is Scores.txt so I am not sure why it is not working. Any idea why?
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does that code compile?
 
Carey Brown
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Lexi Turgeon wrote:

Liutauras Vilda wrote:Can you try to incorporate the idea to your current code?

However, you didn't compile your code often enough, because you have line 12 which shouldn't compile. The sooner you could notice all compile errors, the easier are to fix them. Some people say you should compile your code after every newly written 5 - 10 lines.  Try to follow this approach, that needs to be as a habit probably.




You got the file handling correct in this post but you've put back your old code that was broken in your latest post.
 
Lexi Turgeon
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Everything compiles except for line 6, 11, 16, and 17.
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Lexi Turgeon wrote:Everything compiles except for line 6, 11, 16, and 17.



I highly recommend that you do not worry about how many compiler errors you have. Also, I recommend that you only work on the first compiler error. Why?

Well, basically, once the compiler encounters an error, it has to make an assumption on where the error ends, and continue compiling. This means that it could be possible that, after you fix the error on line 6, all the other errors will disappear. Also, it is possible that, after you fix the error on line 6, you get a whole bunch of new errors.

With enough experience, you should learn to predict whether a compiler error is relevant or not, but for now... I recommend that you tackle only the first error, and then, recompile your application.


Now, if you want help with line 6, it may be a good idea to show us the compiler error message for line 6.

Henry
 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What do you think is wrong with line 6?
 
Liutauras Vilda
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Lexi Turgeon wrote:The name of the file is Scores.txt so I am not sure why it is not working. Any idea why?

We were speaking about it already. Same as about other places where errors occur, I have in mind declaring variable before you use it, you have that issue present on line 16. Problem is that probably you don't understand (yet) what variable declaration is and don't know how to recognize it, because you had the same problem with variable 'Value', after we showed you, you declared, but then again made exactly same mistake on the next line.

Look what Carey 2 days ago wrote.

Carey Brown wrote:You can't use grades.txt. If you want a String literal it would have to be "grades.txt" (in quotes). If you want a variable you might choose something like fileName.



Lexi Turgeon wrote:I do not attend them as they are online, the teacher and students offer little to no help at all.

Practice it seems doesn't help you much at the moment, probably you need some theory before.

Do you have any book you could read? Maybe could find one in local library? Everyone probably starts similar way.
 
Lexi Turgeon
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I do not quite understand how to fix my error with the file name I am trying to get information from. I do not get why I have to declare it as a variable if it is coming from a file that already exists in a folder within the project?
 
Dave Tolls
Rancher
Posts: 4801
50
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Lexi Turgeon wrote:
My problem I think comes from my code not recognizing my file I am trying to read from.



If the code doesn't compile then this here is not your problem.
I think it was mentioned earlier, but you should be compiling frequently and not adding broken code on top of already broken code.

In this case, you should be (as mentioned above) taking the current compiler issues one at a time and resolving them.

At the moment you have no idea if the logic of your code is correct or not because it cannot be executed.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Lexi Turgeon wrote:. . . I do not get why I have to declare it as a variable . . .

Because Java® is a strongly‑typed language. Every variable must be declared with a type before use. If you write Scores.txt without quote marks, that is a class called Scores with a static field called txt. Nowhere do you have a Scores class, so the compiler thinks you shou‍ld have a variable called Scores with a member called txt.
When you put the quote marks round it, that becomes a String literal and that doesn't need to be declared as a variable.

Beware: the link I gave you may be difficult to read.
 
Lexi Turgeon
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I still do not understand how I would fix my error.
 
Carey Brown
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think it's time to get a book. Perhaps Head First Java.
 
reply
    Bookmark Topic Watch Topic
  • New Topic