-1

Cannot find the issue, as you can see in the method "readFile", I've tried to see if the problem is that the scanner reads a string, but when I compile the program it'll say the textfile's name, scores.txt, and not the content. Been working on this for about 3 hours, dayum.

package pointSystem;

import java.io.*;
import java.lang.*;
import java.util.*;

import main.Gooftw;

public class HighscoreManager {

    private int currentHighScores[] = new int[5];
    private int newScore = Gooftw.getScore();
    private int temp;

    //private Formatter formatter;
    private Scanner sc;
    private String file = "scores.txt";
    File outfile;
    FileOutputStream fop = null;

    public HighscoreManager () {
        System.out.println("Hello World!");
        try {
            //formatter = new Formatter(file);

            sc = new Scanner(file);

        } catch(Exception e){
            System.out.println("you have an error");
        }

        //Read current highscores to game

    }

    public void readFile(){
        int i = 0;
        String values [] = new String [5];
        while(sc.hasNext()) {
            values[i] = sc.next();
            System.out.println(values[i]);
            /*System.out.println(sc.nextInt());
            currentHighScores[i] = sc.nextInt();
            System.out.println(currentHighScores[i]);*/
            i++;
        }

        this.closeFile();

        /*for(i=0;i<5;i++) {
            System.out.println(currentHighScores[i]);       
        }
        /*while(sc.hasNext()){
            String a = sc.next();
            String b = sc.next();
            String c = sc.next();

            System.out.printf(a,b,c);
        }*/
    }

    public void addRecords(){

        try{
        outfile = new File(file);
        fop = new FileOutputStream(outfile);

        if(!outfile.exists()){
            outfile.createNewFile();
        }
        for(int i = 0; i<5; i++){
            //formatter.format("%s \n", currentHighScores[i] );
            fop.write(currentHighScores[i] );

        }
        //fop.flush();
        fop.close();

        }catch(IOException e){
            e.printStackTrace();
        } finally {
            try{
                if(fop!=null){
                    fop.close();
                }

                }catch (IOException e){
                    e.printStackTrace();
                }
            }
        }


        //formatter.close();



    public void compareScores(){

        if(newScore > currentHighScores[0]){
            temp = currentHighScores[0];
            currentHighScores[0] = newScore;
        }
        for(int y = 1 ; y<5 ; y++){
            if(temp < currentHighScores[y]){

                temp = temp + currentHighScores[y];
                currentHighScores[y] = temp - currentHighScores[y];
                temp = temp - currentHighScores[y];
            }
        }

    }

    public void closeFile(){
        sc.close();
    }
}

1 Answers1

0

You are getting the name of the file rather than the file because you pass the string that is the name of the file.

http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Scanner.html#Scanner(java.lang.String)

public Scanner(String source) Constructs a new Scanner that produces values scanned from the specified string. Parameters: source - A string to scan

You want this constructor instead:

public Scanner(InputStream source, String charsetName) Constructs a new Scanner that produces values scanned from the specified input stream. Bytes from the stream are converted into characters using the specified charset.

Make a file input stream using this constructor:

public FileInputStream(String name) throws FileNotFoundException Creates a FileInputStream by opening a connection to an actual file, the file named by the path name name in the file system.

And pass THAT to the scanner.

What did we learn today? Don't assume what a method or constructor does if you haven't read its documentation. Following this simple rule will save you a lot of time.

Patashu
  • 21,443
  • 3
  • 45
  • 53