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
6
7
danpost danpost

2017/3/26

#
Since you are parsing the string in a different method, the data will need to given more scope. Change line 208 (on page 2) to:
data = new Object[recordCount][2];
and add this line outside the method:
Object[][] data;
also, change line 1 in the 'getOutputString' method above to:
private String getOutputString()
Test by replacing lines 220 through 223 (on page 2) with this:
System.out.println(getOutputString());
mitrolex mitrolex

2017/3/26

#
It printed this out. Igor:11::Haa:6::Mitar:5::Igor:2::Bezimeni:2::Igorica:1::
danpost danpost

2017/3/26

#
mitrolex wrote...
It printed this out. Igor:11::Haa:6::Mitar:5::Igor:2::Bezimeni:2::Igorica:1::
Looks like they are in top-down order.
mitrolex mitrolex

2017/3/26

#
Yup, it looks like they are. :D But whay are there ':' and '::' again?
danpost danpost

2017/3/26

#
Now, you have a execution flow problem in the act method. Things that should only need done once are being done every act cycle (or, at least more often than they should be). Unfortunately, since I am not familiar with your native language, I cannot make heads or tails of exeactly what to do about it.
danpost danpost

2017/3/26

#
mitrolex wrote...
Yup, it looks like they are. :D But whay are there ':' and '::' again?
The method returns the String that will be put to the high scores file (rank.txt) when needed -- that is why. It is built by the data array which was sorted appropriately when the method is called. So, if you look at the contents of the array, you will find them to be in top-down order also. You could re-insert lines 220 through 223 after the line that replaced it for testing.
mitrolex mitrolex

2017/3/26

#
Now i'm having problems with calling the rank() method from a subclass when i click on it. Gives this: java.lang.NullPointerException at bg.rank(bg.java:226) at rank.act(rank.java:10) at greenfoot.core.Simulation.actActor(Simulation.java:604) at greenfoot.core.Simulation.runOneLoop(Simulation.java:562) at greenfoot.core.Simulation.runContent(Simulation.java:221) at greenfoot.core.Simulation.run(Simulation.java:211)
danpost danpost

2017/3/27

#
mitrolex wrote...
Now i'm having problems with calling the rank() method from a subclass when i click on it. Gives this: < Trace Omitted >
What is line 226 in your bg class?
mitrolex mitrolex

2017/3/27

#
temp = temp.substring(temp.indexOf("::")+2);
danpost danpost

2017/3/27

#
mitrolex wrote...
temp = temp.substring(temp.indexOf("::")+2);
I do not think the error occurred on that line. The error trace no longer matches your code. You will have to reproduce the error, then copy/paste the error trace and the bg class here before making any further changes to it.
danpost danpost

2017/3/27

#
Actually, I am wondering why you would be calling that method from another class. I think the only time that method would be called is when the data file is read in. I did notice an issue in your exception codes, however. Instead of setting 'c' to "0", you should set it to "".
mitrolex mitrolex

2017/3/28

#
I'll do that after school. I need it to show up when i click on that subclass...
mitrolex mitrolex

2017/3/28

#
Here you go.
java.lang.NullPointerException at bg.rank(bg.java:227) at rank.act(rank.java:10) at greenfoot.core.Simulation.actActor(Simulation.java:604) at greenfoot.core.Simulation.runOneLoop(Simulation.java:562) 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
{   
    Object[][] data;
    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();
                rank();
            }            
        }

        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();
                }
            }

        }
                       
    }

    private String getOutputString()
    {
        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;
    }

    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 line = c;
        String temp = line;
        int recordCount = 0;
        while (!"".equals(temp))
        {
            temp = temp.substring(temp.indexOf("::")+2);
            recordCount++;
        }        
        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));
        }
        getOutputString();
        for (int n=0; n<data.length; n++)
        {
            System.out.println((String)data[n][0]+":  "+(Integer)data[n][1]);           
        }
    }
}
Super_Hippo Super_Hippo

2017/3/29

#
Are you sure that you posted the whole class as it is? I can't believe that 'recordCount++;' could throw a NullPointer exection.
danpost danpost

2017/3/29

#
Why are you reading the file in every act cycle.? You only need to acquire the data once. Maybe you should move the reading of the file into the constructor of the class.
There are more replies on the next page.
1
2
3
4
5
6
7