This site requires JavaScript, please enable it in your browser!
Greenfoot back
mitrolex
mitrolex wrote ...

2017/3/8

Write Multiple Things To A File

1
2
3
4
5
danpost danpost

2017/3/26

#
mitrolex wrote...
I wanted to ask where and how should i define the 'n' variable because of "cannot find symbol - variable n"? I'm an idiot, i know.
It would depend on the current player -- is it 'Billy Bob' or 'Bobby Sue' (0 or 1). Then you would compare the current end game score with that players currently stored high score to see if the file needs updated with a new high score for that player.
mitrolex mitrolex

2017/3/26

#
I don't need it to update, i just want the scoreboard to show up when i click on an actor and that's it. This is getting a bit complicated for me but i have to do it somehow, it's for my ending year school project.
danpost danpost

2017/3/26

#
I would guess then that you just need to iterate through the players and scores to display them using something like:
for (int n=0; n<data.length; n++)
{
    System.out.println((String)data[n][0]+":  "+(Integer)data[n][1]);
}
I just printed it to the terminal, but you can do whatever to display it the way you want.
mitrolex mitrolex

2017/3/26

#
Prints out nothing and the "java.lang.StringIndexOutOfBoundsException: String index out of range: -1" is still there.
danpost danpost

2017/3/26

#
mitrolex wrote...
Prints out nothing and the "java.lang.StringIndexOutOfBoundsException: String index out of range: -1" is still there.
Please show the entire error printout and the class code in which the error occurred.
mitrolex mitrolex

2017/3/26

#
java.lang.StringIndexOutOfBoundsException: String index out of range: -1 at java.lang.String.substring(String.java:1967) at bg.rank(bg.java:213) at bg.act(bg.java:158) at greenfoot.core.Simulation.actWorld(Simulation.java:610) at greenfoot.core.Simulation.runOneLoop(Simulation.java:545) at greenfoot.core.Simulation.runContent(Simulation.java:221) at greenfoot.core.Simulation.run(Simulation.java:211)
import greenfoot.*;
import java.io.*;
import javax.swing.JOptionPane;
public class bg extends World
{   
    boolean p=false;
    int razmak_cevi=100;
    int cevbr=0;
    int ptbr=0; 
    int score=0;
    int prva_cev=230;   
    int zmbr=0;
    skor skorObj=new skor();
    boolean pocetak=false;
    String line = null;
    String inputValue=null;
    String c=null;
    BufferedWriter file = null;
    BufferedReader br = null;
    BufferedReader br1=null;
    BufferedWriter f = null;    
    public bg()
    {           
        super(600, 400, 1,false);
        setPaintOrder(rank.class,kraj.class,skor.class,najskor.class,tvsk.class,ptica.class,zemlja.class,cev.class,cevnadole.class);
        ptica pt = new ptica();
        addObject(pt, 100, getHeight()/2-50);              
        zemlja Zemlja = new zemlja();      
        addObject(Zemlja, 500, 375);        
        skorObj.postaviskor(0);
        addObject(skorObj ,getWidth()/2,getHeight()/2-100);
        rank r = new rank();
        addObject(r,30,20);

    }

    public void act() 
    {
        if(Greenfoot.isKeyDown("space")==true && pocetak==false)
        {
            Greenfoot.delay(5);   
            pocetak=true;
        }
        if(pocetak==true)
        {

            cevbr++;
            if (cevbr % 100 == 0)
            {
                stvoricev();
            }
            if(cevbr >= prva_cev)
            {   
                if(ptbr % 100 ==0)
                {               
                    score++;
                    skorObj.postaviskor(score);
                    Greenfoot.playSound("sfxpoint.mp3");
                }
                ptbr++;               
            }
        }        
        zmbr++;      
        if(zmbr%100==0)
        {            
            stvorizemlju();
        }
        ptica a = (ptica) getObjects(ptica.class).get(0);
        if (a != null)
        {
            if (a.touchingCev() || a.touchingZemlja() || a.touchingCevnadole())
            {
                krajigre();
                kraj();
            }            
        }

        try
        {
            br = new BufferedReader(new FileReader("skorevi/najscore.txt"));
            line = br.readLine();           
            br.close();
        }
        catch(IOException ioe)
        {
            System.out.println(ioe);
            try 
            { 
                br.close(); 
            } 
            catch 
            (IOException x){}
            line = "0";
        }

        try
        {
            br1 = new BufferedReader(new FileReader("skorevi/rank.txt"));
            c = br1.readLine();           
            br.close();
        }
        catch(IOException ioe)
        {
            System.out.println(ioe);
            try 
            { 
                br.close(); 
            } 
            catch (IOException x){}
            c = "0";
        }

        if(score>Integer.parseInt(line))
        {

            try 
            {
                file = new BufferedWriter(new FileWriter("skorevi/najscore.txt"));                      
                file.write(""+score);
                file.close();            
            }
            catch (IOException ioe) 
            {
                ioe.printStackTrace();
            }
            finally
            {
                try {
                    file.close();
                }
                catch (IOException ioe) {
                    ioe.printStackTrace();
                }
            }            
        }

        if(p==true)
        {
            try 
            {
                f = new BufferedWriter(new FileWriter("skorevi/rank.txt")); 
                f.write(""+c+inputValue+":"+score+"::");            
                f.close();
            }
            catch (IOException ioe) 
            {
                ioe.printStackTrace();
            }
            finally
            {
                try {
                    f.close();
                }
                catch (IOException ioe) {
                    ioe.printStackTrace();
                }
            }
            rank();
        }
        
    }

    private void stvoricev() 
    {        
        cevnadole Cevnd = new cevnadole();
        cev Cev = new cev();        
        GreenfootImage image = Cevnd.getImage();        
        addObject(Cev,getWidth(),getHeight()/2 + image.getHeight()- Greenfoot.getRandomNumber(200));
        addObject(Cevnd,getWidth(),Cev.getY() - image.getHeight() - razmak_cevi);
    } 

    private void stvorizemlju()
    {
        zemlja Zemlja = new zemlja();
        addObject(Zemlja, 500, 375);
    }    

    public void kraj()
    {
        p=true;
        Greenfoot.playSound("sfxhit.mp3");
        tvsk kr = new tvsk();       
        addObject(kr,300,130);        
        inputValue= JOptionPane.showInputDialog("UNESI SVOJE IME:");        
        if(inputValue==null)
        {
            inputValue="Bezimeni";            
        }
        Greenfoot.stop();
    }

    public void krajigre()
    {
        Color c = new Color(0,0,0,0);           
        Actor ns = new najskor();
        addObject(ns, 341, 220);
        ns.setImage(new GreenfootImage(""+Integer.parseInt(line), 35, Color.BLACK, c));
    }

    public void rank()
    {
        String temp = line;
        int recordCount = 0;
        while (!"".equals(temp))
        {
            temp = temp.substring(temp.indexOf("::")+2);
            recordCount++;
        }        
        Object[][] data = new Object[recordCount][2];
        for (int i=0; i<recordCount; i++)
        {
            int recLen = line.indexOf("::");
            String record = line.substring(0, recLen);
            line = line.substring(recLen+2);
            int nameLen = record.indexOf(":");
            data[i][0] = record.substring(0, nameLen);
            data[i][1] = Integer.valueOf(record.substring(nameLen+1));
        }
        
        for (int n=0; n<data.length; n++)
        {
            System.out.println((String)data[n][0]+":  "+(Integer)data[n][1]);
        }        
    }
}
Here you go.
danpost danpost

2017/3/26

#
Why are you working with two files when one is sufficient? What data is the rank file supposed to contain? give an example String value for String 'c'.
mitrolex mitrolex

2017/3/26

#
I have one file (najskor) which contains the highest score. The other file (rank) contains player names and scores.
danpost danpost

2017/3/26

#
mitrolex wrote...
I have one file (najskor) which contains the highest score. The other file (rank) contains player names and scores.
Why not just have the one file sorted from best to worst score?
mitrolex mitrolex

2017/3/26

#
That's what im looking for all this time.
danpost danpost

2017/3/26

#
mitrolex wrote...
That's what im looking for all this time.
You first need a way to sort the data and create the output string:
private String getOutputString(Object[][] data)
{
    for (int i=1; i<data.length; i++)
    {
        int n = i;
        while (n > 0 && (Integer)data[n][1] > (Integer)data[n-1][1])
        {
            Object[] hold = data[n-1];
            data[n-1] - data[n];
            data[n] = hold;
            n--;
        }
    }
    String outputLine = "";
    for (int i=0; i<data.length; i++) outputLine += data[i][0]+":"+data[i][1]+"::";
    return outputLine;
}
Now, you can write 'getOutputString(data)' to the file and it will be a highest to lowest sorted string of player names paired with their scores. When read in and parsed, the overall high score will be:
int bestHighScore = (Integer)data[0][1];
and the player that made it is:
String topPlayer = (String)data[0][0];
Unless, of course, the end game score of the current player broke that high score.
mitrolex mitrolex

2017/3/26

#
So where should i put that code? And how is it connected to the files, names, scores...?
danpost danpost

2017/3/26

#
mitrolex wrote...
So where should i put that code? And how is it connected to the files, names, scores...?
It is a method that goes in your 'bg' World subclass. You would call the method at the time you are writing (updating) the file. You will always read the file and parse the data first; then, compare check for changes or additions; then, if needed, update the file; you can also, if desired, display some or all of the top scores. You still have not answered my question about the file -- whether there is currently any data in it at all.
mitrolex mitrolex

2017/3/26

#
There is data in both files always.
danpost danpost

2017/3/26

#
mitrolex wrote...
There is data in both files always.
For testing purposes, insert the following line at line 112 above:
System.out.println("'"+line+"'");
Compile and run. Scroll to top and click in upper portion of terminal window if an error was produced. Report what output was produced (copy/paste terminal output using Ctrl-A, Ctrl-C and Ctrl-V).
There are more replies on the next page.
1
2
3
4
5