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

2019/6/9

Help With Inputting and Showing Data File Formated .txt

gacha321 gacha321

2019/6/9

#
I'm sorry but I have problem with my FarmGame again :((( in this case, my game must can inputting data to file formated .txt and can show that if we choose the "Look our Plants" list. I tried so many times and exactly it doesn't work at all :(( So, before we want to buy, sell or show our plants, we must walk to garden first, when we touch the garden it would showing us 3 lists, 1. Buy Plants; 2. Sell Plants; 3. Look our Plants; when we choose the 1st list, so it would be go to the BuyPlants World that show us 3 lists of Plants (Grape, Guava, and Manggo) wich have a sell price. when we wan't to buy the Plants, I make a 3 buy button for the Plants, we concern to the buy button in Grape (Button4).. I tried to code and I just have the code like this..
import greenfoot.*; 
import java.io.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
/**
 * Write a description of class Button4 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Button4 extends Button
{
    /**
     * Act - do whatever the Button4 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
   
    
    public void act() 
    {
        // Add your action code here.
        if(Greenfoot.mouseClicked(this)){
            Counter counter = ((BuyPlants)getWorld()).getCounter();
            if (counter.getValue() >= 100)
            {
                Greenfoot.playSound("ChaChing.mp3");
                name();
                counter.walkCount(-100);
            }
        }
    }    
    
    public void name(){
        Scanner in = new Scanner(System.in);
		String plantsName="";
		System.out.print ("Input Grape's Name : ");
		plantsName = in.nextLine();
		try{
			File data = new File("plants.txt");
			PrintWriter pr = new PrintWriter(new FileWriter(data, true));
			pr.println(plantsName);
			pr.close();
			System.out.println("Data has been saved!");
		}
		catch(Exception e) {
			System.out.println("Cannot Print!");
		}
    }
}
There's no error when compiling that code.. but when I tried to click the button, this is no act :(( there's no ChaChing sound and also the code like useless.. :(( I don't know where's the problem... After that, I tried to code for the 3rd list.. (Look our Plants) I tried to code like this in (List3.class)
public class List3 extends Button
{
    /**
     * Act - do whatever the List3 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        // Add your action code here.
        if(Greenfoot.mouseClicked(this)){
            data();
    }    
    }
    
 
    public static void data() throws FileNotFoundException{
        File plantstxt = new File("plants.txt");
        Scanner plantsfile = new Scanner(plantstxt);
        ArrayList<String> ve = new ArrayList<String>();
        
        while (plantsfile.hasNextLine()){
            ve.add(plantsfile.nextLine());
        }
        System.out.println("Garden Lists : ");
        for(int i=0;i<ve.size();i++){
            System.out.println((i+1) +". " + ve.get(i));
        }
        System.out.println("Total plants in Garden : "+ve.size());
        System.out.println("");
    }
    
}
But.. it has an error that shows unreported exception java.io.FileNotFoundException; must be caught or declared to be thrown in this code...
data();
I don't know what must I do then :(( I tried to code this in 2 days and I don't know It's right or wrong because It doen's work in my game ;)) If you have a suggest or know what's wrong with my code.. please reply this.. your help is very very meaningfull for me ;))
You need to login to post a reply.