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

2012/7/30

Removing a letter from a String.

1
2
3
SPower SPower

2012/7/31

#
@gusbus123 Danpost's tip gives an exception, because key is null.
gusbus123 gusbus123

2012/7/31

#
btw i changed the "key.length() == 1" to "word.length() != 0" because the length of the shown string called "word" has no limited amount of characters (may add later).
danpost danpost

2012/7/31

#
Yeah, well, that was already stated above. Change what I said to
String key = Greenfoot.getKey();
if (key != null)
{
    if (key.length() == 1 && (char) 127 == key.charAt(0))
    {
        // do delete action here
    }
    // check other keys here
}
danpost danpost

2012/7/31

#
gusbus123 wrote...
btw i changed the "key.length() == 1" to "word.length() != 0" because the length of the shown string called "word" has no limited amount of characters (may add later).
I understand. But are you actually saving the 'delete' keystroke? or, are you adjusting 'word' dependent on the keystroke? EDIT: In my last post, both conditions in line 4 are used to determine if the keystroke was the 'delete' key. From there you can verify that the length of 'word' is not zero before actually deleting a character.
gusbus123 gusbus123

2012/8/1

#
ok thx. it works now.
gusbus123 gusbus123

2012/8/1

#
1 last question: is there a way to use a string to cast through another actor/world? So i have a String in the world "World" called "Left". I want to be able to use another String i created in another actor that i made to equal "Left" to referance from a variable into the world to know what the String called "Left" in the world "World" is. eg. the String in the world "World" called "Left" = "ltr"; in the actor i have a string called "reference" that = "Left"; and throgh this: World world (World) getWorld(); to cast to it and get the left. So i try to use this in the actor: world.reference; and i want it so that the actor would accually look for a variable called "Left" (as in whatever reference is equal to) in the world "World". But this wont work as it will instead look for a variable called "reference". how would i make it so that it doesnt look for the literal word "reference" and looks for what reference = to. So in this case "Left".
gusbus123 gusbus123

2012/8/1

#
Oh and for a part of my coding i was trying this:
Pacman pw = (Pacman) getWorld();
    String i="";
            switch(ID) {
                case 3: i=pw.p1Left; break;
                case 4: i=pw.p1Right; break;
                case 5: i=pw.p1Up; break;
                case 6: i=pw.p1Down; break;
                case 7: i=pw.p2Left; break;
                case 8: i=pw.p2Right; break;
                case 9: i=pw.p2Up; break;
                case 10: i=pw.p2Down; break;
            }
but it keeps on giving an error report saying "java.lang.NullPointerException" when it gets used (in my case when i press on the button with the specified ID's. and the p1Left, p1Right, etc are String variables in the world called Pacman.
SPower SPower

2012/8/1

#
A null pointer exception occors when you try to use an object which is null, like this:
Object a = null;
a.dosomething(); // exception
Look at which line it crashes, and make sure you assign a value to it. For more info about exceptions, checkout this course: http://www.greenfoot.org/scenarios/5709
gusbus123 gusbus123

2012/8/1

#
@SPower i know it returned as a null, but the specified variable (1 of them like pw.p1Left) all equal to something that ISNT null. So p1Left default is "Left", p1Right default is "Right" and wateva. But in my last post there the 'i' still returns as null when its supposed to return as "left" or wateva.
SPower SPower

2012/8/1

#
Can you please give the line where it crashes? That would be a little more clear for me...
gusbus123 gusbus123

2012/8/1

#
it one of the cases, dependent on which button with that id u clicked on. So u could just say in line 4 of that code i put in the recent one
gusbus123 gusbus123

2012/8/1

#
so basically its the part where it says i=pw."whateva is here"
SPower SPower

2012/8/1

#
A little tip:
if (pw == null)
    System.out.println("pw is null");
if (pw.p1Left == null)
    System.out.println("pw1Left is null");
// etc.
gusbus123 gusbus123

2012/8/1

#
no i dont want it to print out if they == null. I need it so the i wouldnt just somehow = null. I need i to = what its accually supposed to =. So like in case 3, i need i to accually = to what p1Left is, so in my case i need it to = "left". same with p1Right needs to = "right"
SPower SPower

2012/8/1

#
I understand, but something is null, otherwise you wouldn't get an exception. You don't have to replace the switch with these if statements, just add it for debugging, and after that, remove it.
There are more replies on the next page.
1
2
3