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

2017/3/29

Help with reading strings from file

1
2
Asiantree Asiantree

2017/3/29

#
while (fileReader.hasNext())
        {
            String type = fileReader.nextLine();
            if (type.equals("ant"))
            {
              int xPos, yPos;
              double xVel, yVel;
              
              xPos = fileReader.nextInt();
              yPos = fileReader.nextInt();
              
              xVel = fileReader.nextDouble();
              yVel = fileReader.nextDouble();
              
              Ant nextAnt = new Ant(xVel, yVel);
              addObject(nextAnt, xPos, yPos); 
            }
            if (type.equals("bad"))
            {
                int xPos, yPos;
                  
                xPos = fileReader.nextInt();
                yPos = fileReader.nextInt();
                  
                Fries nextFries = new Fries(50, 50);
                addObject(nextFries, xPos, yPos);
            }
I am having trouble reading from a file to get an ant and bad food (in this case, fries) to spawn into the world. My text file only contains these lines of code as formatted here: ant 300 250 0.7 0.5 bad 100 100 What I am trying to do is get the fileReader to look for the word "ant" and use the line of code to spawn in an ant when it reads that line from the text file. Likewise for the "bad" line in the text file. Any tips on how to read in those words and then use code to get them to spawn into the world? Also, ignore the "new Fries (50, 50)" part.
danpost danpost

2017/3/29

#
First, you would need to parse each line using the space as a separator. This will give you an array of strings. You would then compare the first element in the array for each of the possible values using a line like the following:
if ("ant".equals(data[0]))
where 'data' is the name of the array. The number of remaining elements should be determinable from the value of the first element. If you have issues with any step in the process, start a new discussion thread on that part and show what you tried.
Asiantree Asiantree

2017/3/29

#
String[] data = new String[3];
        data[0] = "ant";
        data[1] = "bad";
        data[2] = "good";
        while (fileReader.hasNext())
        {
            if ("ant".equals(data[0]))
            {
              int xPos, yPos;
              double xVel, yVel;
            
              xPos = fileReader.nextInt();
              yPos = fileReader.nextInt();
            
              xVel = fileReader.nextDouble();
              yVel = fileReader.nextDouble();
            
              Ant nextAnt = new Ant(xVel, yVel);
              addObject(nextAnt, xPos, yPos);
            }
        }
Something like this? (Sorry I am new to arrays and how they should function).
Super_Hippo Super_Hippo

2017/3/29

#
Could you please stop creating multiple discussions about the same topic? See this and this. Nosson already told you, but you are still continuing to do that. Stop it!
danpost danpost

2017/3/29

#
Please do not double post everything (destroy the new discussion thread you started 'Reply to Danpost'). You need to read a line before you can parse and test it:
while (fileReader.hasNext())
{
    String line = fileReader.nextLine();
    String[] parts = line.split(" ");
    if ("ant".equals(parts[0]))
    {
        addObject(new Ant(parts[3], parts[4]), parts[1], parts[2]);
    }
    if ("bad".equals(parts[0]))
    // etc.
Asiantree Asiantree

2017/3/29

#
My apologies everyone, I will stop. But one last thing, I am getting a "String cannot be converted to double" error for this line
addObject(new Ant(parts[3], parts[4]), parts[1], parts[2]);
for "parts 3"
danpost danpost

2017/3/29

#
Sorry, try this:
addObject(new Ant(Double.valueOf(parts[3]), Double.valueOf(parts[4])), Integer.valueOf(parts[1]), Integer.valueOf(parts[2]));
Asiantree Asiantree

2017/3/29

#
For some reason, the movement of the ant only lasts for one second or so and then it stops. Any tips on where to look?
danpost danpost

2017/3/29

#
Asiantree wrote...
For some reason, the movement of the ant only lasts for one second or so and then it stops. Any tips on where to look?
The code for ant behavior should be in the Ant class.
Asiantree Asiantree

2017/3/29

#
public void act() 
    {
        if (goodCalorie > 0 && badCalorie > 0)
        {
            exactX += xSpeed;
            exactY += ySpeed;
            setLocation ( (int) exactX, (int) exactY );
            if (getX() <= getImage().getWidth() / 2 || getX() >= getWorld().getWidth() - (getImage().getWidth() / 2)) 
            {
                xSpeed = -xSpeed;
            }
            if (getY() <= getImage().getHeight() / 2 || getY() >= getWorld().getHeight() - (getImage().getHeight() / 2)) 
            {
                ySpeed = -ySpeed;
            }
        }
This is my act method for my Ant class (ignore goodCalorie and badCalorie for now).
danpost danpost

2017/3/29

#
I think you should check the current direction along with edge collision to determine whether the direction needs changed or not:
public void act() 
{
    if (goodCalorie > 0 && badCalorie > 0)
    {
        exactX += xSpeed;
        exactY += ySpeed;
        setLocation ( (int) exactX, (int) exactY );
        if ((xSpeed < 0 && getX() <= getImage().getWidth() / 2) || (xSpeed > 0 && getX() >= getWorld().getWidth() - (getImage().getWidth() / 2))) 
        {
            xSpeed = -xSpeed;
        }
        if ((ySpeed < 0 && getY() <= getImage().getHeight() / 2) || (ySpeed > 0 && getY() >= getWorld().getHeight() - (getImage().getHeight() / 2))) 
        {
            ySpeed = -ySpeed;
        }
    }
Asiantree Asiantree

2017/3/29

#
Hmm, it doesn't seem to quite work either.
Super_Hippo Super_Hippo

2017/3/29

#
Maybe goodCalorie or badCalorie is set to 0 or something below after "one second or so".
Asiantree Asiantree

2017/3/29

#
public class Ant extends Actor
{
    private double xSpeed;
    private double ySpeed;
    
    private int goodCalorie;
    private int badCalorie;
    
    private double exactX;
    private double exactY;
    
    private boolean dead;
    
    public Ant (double xSpeed, double ySpeed)
    {
        goodCalorie = 200;
        badCalorie = 100;
        this.xSpeed = xSpeed;
        this.ySpeed = ySpeed;
        dead = false;
    }
    protected void addedToWorld (World myWorld)
    {
        exactX = getX();
        exactY = getY();
    }
    /**
     * Act - do whatever the Ant wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        if (goodCalorie > 0 && badCalorie > 0)
        {
            exactX += xSpeed;
            exactY += ySpeed;
            setLocation ( (int) exactX, (int) exactY );
            if ((xSpeed < 0 && getX() <= getImage().getWidth() / 2) || (xSpeed > 0 && getX() >= getWorld().getWidth() - (getImage().getWidth() / 2))) 
            {
                xSpeed = -xSpeed;
            }
            if ((ySpeed < 0 && getY() <= getImage().getHeight() / 2) || (ySpeed > 0 && getY() >= getWorld().getHeight() - (getImage().getHeight() / 2))) 
            {
                ySpeed = -ySpeed;
            }
        }
        goodCalorie = goodCalorie - 1;
        badCalorie = badCalorie - 5;
        if (goodCalorie == 0 && badCalorie == 0)
        {
            dead = true;
        }
    }
    }
This is my entire Ant class. The goodCalorie and badCalorie represent good and bad energy respectively, and after so many movements, the Ant will die. I have yet to code in that, but for now I just need to get the Ants loaded in and moving until badCalorie and goodCalorie count runs out.
danpost danpost

2017/3/29

#
On the 201st act cycle both 'goodCalorie' and 'badCalorie' will be less than zero. That is probably just less than 4 seconds of running time (which is probably what you are experiencing). The 'dead' field will never be set to true as the two calorie fields reach zero at different times (they are never both zero at the same time).
There are more replies on the next page.
1
2