aspose file tools*
The moose likes Beginning Java and the fly likes Highscore not working Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Highscore not working" Watch "Highscore not working" New topic
Author

Highscore not working

Kacey Simeon
Greenhorn

Joined: Jul 30, 2011
Posts: 18
im trying to set up a highscore list for my game. But for some reason cant get it to work. If anyone can help that would be awesome.



Sorry if this is an inconvience to anyone.
AnujS Sharma
Greenhorn

Joined: Jul 30, 2011
Posts: 15
Why are you unable to set the "HighScore"??

Are you getting a compiler error or is it not working as expected??

If it is the first one, then please post the error for us to help you. If it is the latter one, for what all inputs are you not getting the expected output.

My advice would be to identify such inputs and debug through your code until you find the root cause of the problem. Change the code appropriately and test again.

Ove Lindström
Ranch Hand

Joined: Mar 10, 2008
Posts: 326

Do you need to store the highscore list in a readable text form or just as persistence between runs?

If the first, then you are on the right track. If the second, why not just serialize the whole array/list to a file and read it when you start the program?
Kacey Simeon
Greenhorn

Joined: Jul 30, 2011
Posts: 18
Ove Lindström wrote:Do you need to store the highscore list in a readable text form or just as persistence between runs?

If the first, then you are on the right track. If the second, why not just serialize the whole array/list to a file and read it when you start the program?


To answer your question i put everything i know in the other reply
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18911
    
    8

What other reply?
Kacey Simeon
Greenhorn

Joined: Jul 30, 2011
Posts: 18
AnujS Sharma wrote:Why are you unable to set the "HighScore"??

Are you getting a compiler error or is it not working as expected??

If it is the first one, then please post the error for us to help you. If it is the latter one, for what all inputs are you not getting the expected output.

My advice would be to identify such inputs and debug through your code until you find the root cause of the problem. Change the code appropriately and test again.



I bieleve it is the first. Here is the error
Exception in thread "main" java.io.FileNotFoundException: Highscorelist.txt (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileReader.<init>(Unknown Source)
at testthing.main(testthing.java:463)

By the way Im as dumb as a boxx of roxx so im sorry if I sound stupid.
Kacey Simeon
Greenhorn

Joined: Jul 30, 2011
Posts: 18
Paul Clapham wrote:What other reply?

LOL sorry didnt think you would get it so quickly
AnujS Sharma
Greenhorn

Joined: Jul 30, 2011
Posts: 15
The exception thrown says that it cannot find the file.

When you're specifying the file, specify the absolute path. Maybe the the file that you want to read & write is not in the same directory or sub directory as that of the your java class file. This is why it is throwing an exception. Try specifying the whole path like, "D:\FolderName\HighScore.txt".


Kacey Simeon
Greenhorn

Joined: Jul 30, 2011
Posts: 18
AnujS Sharma wrote:The exception thrown says that it cannot find the file.

When you're specifying the file, specify the absolute path. Maybe the the file that you want to read & write is not in the same directory or sub directory as that of the your java class file. This is why it is throwing an exception. Try specifying the whole path like, "D:\FolderName\HighScore.txt".




1. sorry for the late reply

2. sorry im about to sound stupid.

So you want me to replace the (new FileReader("Highscorelist.txt")); with something like (new FileReader("D:\Downloads\Highscore.txt")); ?

fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11480
    
  16

don't apologize for asking questions...there is a LOT to learn, and we all have to start somewhere.

Assuming your file is in the D:\Downloads directory, and assuming it really is named "Highscore.txt", then yes...almost.

remember that in String literals in java, a backslash character '\' has special meaning. so if you do it exactly as you wrote, java will think the "\D" and "\H" are 'special characters', and will most likely complain. you need to tell it you want a LITERAL backslash, and the way you do that is to use TWO in a row:

(new FileReader("D:\\Downloads\\Highscore.txt"));

at least...i think. I haven't tested this...but this is the basic idea.

Note that it will also vary depending on what OS you are using...windows is different from Linux is different from MacOS. Somone (probably Campbell) will most likely chime in soon and tell you the RIGHT way to do it with directory separators, but this should work for now.


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

Joined: Jul 30, 2011
Posts: 18
fred rosenberger wrote:don't apologize for asking questions...there is a LOT to learn, and we all have to start somewhere.

Assuming your file is in the D:\Downloads directory, and assuming it really is named "Highscore.txt", then yes...almost.

remember that in String literals in java, a backslash character '\' has special meaning. so if you do it exactly as you wrote, java will think the "\D" and "\H" are 'special characters', and will most likely complain. you need to tell it you want a LITERAL backslash, and the way you do that is to use TWO in a row:

(new FileReader("D:\\Downloads\\Highscore.txt"));

at least...i think. I haven't tested this...but this is the basic idea.

Note that it will also vary depending on what OS you are using...windows is different from Linux is different from MacOS. Somone (probably Campbell) will most likely chime in soon and tell you the RIGHT way to do it with directory separators, but this should work for now.


Thanks alot for the help but it still not getting the .txt I double checked and tripled checked all of the spelling a captializations. It now reads

I know i must be doing something wrong. Also i use windows if it helps at all. I also am not sure but i use eclipse.
AnujS Sharma
Greenhorn

Joined: Jul 30, 2011
Posts: 15
Did you change it for the FileReader as well??

Try debugging the program. Use System.out.println() to see till where the program is executing and at what line precisely you are getting the error.


I can see that you haven't pasted the full code, may be that's why we are unable to help you..
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11480
    
  16

also, and this is a WAG...does the file actually exist?

based on the code in your original post, the first thing you try and do is open it with the reader. I'm not sure what that does if the file isn't already there, but a "Filenotfound" exception sounds like a reasonable thing to happen.

if it is not, try putting a file there, even if it is empty.
Kacey Simeon
Greenhorn

Joined: Jul 30, 2011
Posts: 18
fred rosenberger wrote:also, and this is a WAG...does the file actually exist?

based on the code in your original post, the first thing you try and do is open it with the reader. I'm not sure what that does if the file isn't already there, but a "Filenotfound" exception sounds like a reasonable thing to happen.

if it is not, try putting a file there, even if it is empty.


I actually didnt have it in the original post but after ward when i changed the code I made the .txt and placed it in the everything folder. So yeah thanks for the help though
Kacey Simeon
Greenhorn

Joined: Jul 30, 2011
Posts: 18
AnujS Sharma wrote:Did you change it for the FileReader as well??

Try debugging the program. Use System.out.println() to see till where the program is executing and at what line precisely you are getting the error.


I can see that you haven't pasted the full code, may be that's why we are unable to help you..


I didnt think the rest of the code could affect it because the only part of the code that will lead to the highscore is when my while loop ends. But if you think so i could post the rest.

Edit: I did it to the writer and the reader.....
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11480
    
  16

Try this...

Write a brand new program, something like "FileReadTest.java". Have it do nothing but open the file, read and print each line, then close the file.

See if you can get that to work. If not, post THAT code here, since it should only be about 20-30 lines, max.

If it DOES work, then look at what may be different between your new test program and your existing one. Running from the same directory? File name the same? etc.
Kacey Simeon
Greenhorn

Joined: Jul 30, 2011
Posts: 18
fred rosenberger wrote:Try this...

Write a brand new program, something like "FileReadTest.java". Have it do nothing but open the file, read and print each line, then close the file.

See if you can get that to work. If not, post THAT code here, since it should only be about 20-30 lines, max.

If it DOES work, then look at what may be different between your new test program and your existing one. Running from the same directory? File name the same? etc.


Thanks for your help but my comp is going crazy ill edit this responce as soon as i can get my computer stable again.
Kacey Simeon
Greenhorn

Joined: Jul 30, 2011
Posts: 18
Sorry for not getting back to ya guys sooner i completely forgot to tell you guys. I fixed the problem. Thanks for everyones help! You guys are such an awesome community
 
 
subject: Highscore not working