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

2013/4/21

IOException

Ragtime Ragtime

2013/4/21

#
Hi. I'm trying to read some questions and answers from a text file. After each click, the next set of text should be displayed and it works until it reaches the last set (the last five lines in the file, which are all "0". Actually the simulation should stop there, but it doesn't). Then it throws an IOException and I don't know how to fix this... I would be grateful for some help. Thanks
String line = null;
        int c=0;
        try
        {
            if((line = reader.readLine()) != null)
            {
                if(line=="0") Greenfoot.stop();
                Q.settext(line);
                A1.settext(reader.readLine());
                A2.settext(reader.readLine());
                A3.settext(reader.readLine());
                c=Integer.parseInt(reader.readLine());
            }
        }
        catch (FileNotFoundException e) {throw new RuntimeException("File not found");}
        catch (IOException e) {throw new RuntimeException("IOException");}
        finally
        {
            if(file != null)
            {
                try
                {
                    file.close();
                }
                catch (IOException e) {e.printStackTrace();}
            }
        }
Gevater_Tod4711 Gevater_Tod4711

2013/4/21

#
I think Greenfoot.stop() stops the game after executing the current act cycle. So the lines 8 to 12 are executed although you stop the scenario. You should try to let the method break up using a return statement:
if(line=="0") {
    Greenfoot.stop();
    return;
}
Ragtime Ragtime

2013/4/21

#
It still throws the IOException.... Even if the lines 8 to 12 were executed, in the file the last lines are all "0", so it shouldn't throw any errors I think, the lines are not empty. To be sincere, I don't even know what the IOException means...
davmac davmac

2013/4/21

#
Also, you should pretty much never compare strings using '=='. Use .equals(...), i.e.
    if(line.equals("0")) {  
        Greenfoot.stop();  
        return;  
    }  
Gevater_Tod4711 Gevater_Tod4711

2013/4/22

#
Is there any difference between == and equals using String? There of course is one if I use classes that are not the basic classes of java but I think using Strings there should be no difference. At least I've never seen any case in which == and equals returned different values (using Strings).
davmac davmac

2013/4/22

#
Is there any difference between == and equals using String?
Yes. '==' compares reference equality, .equals() compares the actual string contents.
At least I've never seen any case in which == and equals returned different values (using Strings).
This precise example is one such case. If you call readLine() it will not return an existing object, so '==' will always return false even if the strings are equal.
Gevater_Tod4711 Gevater_Tod4711

2013/4/22

#
I looked in my games again: I always used it like this:
String s = "string";
if (s == "string") {
    //returns true;
}
There probably is the difference. Because of this I thought it would always work like this.
davmac davmac

2013/4/22

#
Do you really think I would say that you should use equals() instead of '==' if they were the same? Try this:
        String s = "";
        for (char c = 'a'; c <= 'f'; c++) {
            s += c;
        }
        System.out.println(s);
        if (s == "abcdef") {
            System.out.println(s + " == abcdef");
        }
        else {
            System.out.println(s + " != abcdef");
        }
danpost danpost

2013/4/22

#
Or, just try this
String s = "a;bldk";
String t = new String(s);
System.out.println(s == t);
davmac davmac

2013/4/22

#
I was trying to construct something closer to a real-world example. The point is that one string is static, the other is dynamically constructed. In general, a dynamically constructed string is not going to be '==' to another string even if the strings are equal. I'm using 'constructed' in the traditional sense here, not the Java sense.
Ragtime Ragtime

2013/4/22

#
It works now, thanks. I used the equals() and return statements. I also replaced "0" by "stop" in the file (I don't know if this had any effect)
You need to login to post a reply.