Results 1 to 8 of 8
  1. #1
    theunlockeRRR is offline Code Child
    Join Date
    Aug 2016
    Location
    Serbia
    Posts
    45
    Rep Power
    0

    Question Reading a int(or double) from a text file

    I want to make a highscore system for a game I'm making but I'm having troubles with reading the highscore from a text file. My code always returns some numbers between 50-54 or something like that even though my number in text file is like 500 or 600. Can someone help me fix this? Thanks
    Java Code:
    private static double getHighScore() {
            String fileName = "highscore.txt";
    
            double hs = 0;
    
            try {
                
                FileReader fileReader = 
                    new FileReader(fileName);
    
         
                BufferedReader bufferedReader = 
                    new BufferedReader(fileReader);
    
               hs = bufferedReader.read();
             
                bufferedReader.close();         
            }
            catch(FileNotFoundException ex) {
                System.out.println(
                    "Unable to open file '" + 
                    fileName + "'");                
            }
            catch(IOException ex) {
                System.out.println(
                    "Error reading file '" 
                    + fileName + "'");                  
           
            }
    		return hs;
        }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Reading a int(or double) from a text file

    Before I can tell you how to retrieve the number, I need to know how yo stored the number in the file? If it was in the form of a String you
    must return the string and convert it.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Reading a int(or double) from a text file

    A simpler way to read a small text file would be with the Scanner class. It has a constructor that takes a File object pointing to the file. The Scanner class has lots of methods for reading and converting text from a file.
    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    theunlockeRRR is offline Code Child
    Join Date
    Aug 2016
    Location
    Serbia
    Posts
    45
    Rep Power
    0

    Default Re: Reading a int(or double) from a text file

    Quote Originally Posted by jim829 View Post
    Before I can tell you how to retrieve the number, I need to know how yo stored the number in the file? If it was in the form of a String you
    must return the string and convert it.

    Regards,
    Jim
    For testing purporses I just wrote the number in notepad myself.

    EDIT: After reading a string instead of a int from a file and then converting the string into a integer my problem is solved.
    I have 1 more question, what is the way that I can get both the player name and the highscore from 1 text line?
    Last edited by theunlockeRRR; 08-17-2016 at 07:12 PM.

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Reading a int(or double) from a text file

    Java Code:
    Scanner scanner = new Scanner("highscore.txt");
    int score = scanner.nextInt();
    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #6
    theunlockeRRR is offline Code Child
    Join Date
    Aug 2016
    Location
    Serbia
    Posts
    45
    Rep Power
    0

    Default Re: Reading a int(or double) from a text file

    Quote Originally Posted by jim829 View Post
    Java Code:
    Scanner scanner = new Scanner("highscore.txt");
    int score = scanner.nextInt();
    Regards,
    Jim
    I tried that code but decided to write player name on the next line for simplicity, thanks anyway.

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Reading a int(or double) from a text file

    Scanner scanner = new Scanner("highscore.txt"); // Set String to scan
    The Scanner class has several different constructors.
    The one shown above takes a String to be scanned.

    To read the contents of a file the constructor takes a File object:
    Java Code:
    Scanner scanner = new Scanner(new File("highscore.txt"));  // read from highscore.txt
    If you don't understand my response, don't ignore it, ask a question.

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Reading a int(or double) from a text file

    Yep. I should have known better. I hate giving bad advice.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Replies: 1
    Last Post: 12-09-2014, 12:27 AM
  2. Reading from text file
    By Zaadyn in forum New To Java
    Replies: 2
    Last Post: 02-02-2012, 10:00 PM
  3. Replies: 8
    Last Post: 07-17-2011, 02:38 PM
  4. Reading Text file to Data type Double
    By orpheus2708 in forum New To Java
    Replies: 17
    Last Post: 04-08-2011, 03:11 AM
  5. read only double input from text file
    By napi1234 in forum New To Java
    Replies: 6
    Last Post: 06-28-2010, 04:06 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •