Hello. I am doing a project right now to simulate the ride lines from an amusement park. My world currently has a load button, when the user press this button a FileDialog should pop up, the user choose a txt file. This text file only have a string with each name of the ride lines. What I need to
do is create an array where it stores each string so then it can write those strings in the world one on top of each other. My code is the following
My code for the load button is the following
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.awt.FileDialog; import java.io.*; import java.util.Scanner; /** * Write a description of class MyWorld here. * * @author (your name) * @version (a version number or a date) */ public class MyWorld extends World { public static final int MAX_RIDES= 14 ; //maximum number of rides possible String[] rides; /** * Constructor for objects of class MyWorld. * */ public MyWorld() { // Create a new world with 800x600 cells with a cell size of 1x1 pixels. super ( 800 , 600 , 1 ); addObject( new LoadButton(), 230 , 20 ); addObject( new MergeButton(), 300 , 20 ); addObject( new SortByNameButton(), 400 , 20 ); addObject( new SortByLengthButton(), 500 , 20 ); rides = new String[ 13 ]; } public void readFromFile() { // pop up GUI dialog for file FileDialog fd = null ; fd = new FileDialog(fd, "Specify a file" , FileDialog.LOAD); fd.setVisible( true ); // build the fully quialified name of the file String name=fd.getDirectory()+fd.getFile(); // build file from the filename File readFile = new File(name); // build a Scanner for the file Scanner dataReader = null ; try { dataReader = new Scanner(readFile); } catch (IOException e) { System.out.println( "File Read error" + e); return ; } // as long as thscanner has any data left while (dataReader.hasNext()) { String line = dataReader.next(); for ( int i = 0 ; i< 13 ;i++) { line = rides[i]; } } // done with this scanner. dataReader.close(); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class LoadButton here. * * @author (your name) * @version (a version number or a date) */ public class LoadButton extends MyButton { public LoadButton() { super ( "Load" ); } public void performAction() { ( (MyWorld) getWorld()).readFromFile(); } } |