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

2018/4/26

Reading a file

danteinferno danteinferno

2018/4/26

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

2018/4/26

#
Line 66 looks backward.
danteinferno danteinferno

2018/4/26

#
danpost wrote...
Line 66 looks backward.
What do you mean?
danpost danpost

2018/4/26

#
danteinferno wrote...
What do you mean?
You are reading data into a the line variable and then replacing it with the (probably null) value of an element of rides. I would think that you would set the element of rides to that which was read and put in the line variable. Another issue I just noticed about that part of the code is that you are using a for loop inside a while loop. As such you are iterating over the entire rides array with each line of data read in, which certainly cannot be right. Initialize an int counter before the while loop and increment it with each line successfully read in (dropping the for loop) and you can read the data directly into the array with:
1
2
3
4
5
6
int i = 0;
while (dataReader.hasNext())
{
    rides[i] = dataReader.next();
    i++;
}
or more simply
1
2
int i = 0;
while (dataReader.hasNext()) rides[i++] = dataReader.next();
You need to login to post a reply.