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

2014/12/31

Searching Strings

Hersov Hersov

2014/12/31

#
Hey there, I need some help with the subject of searching a string. I have a piece of code which im confused as to why it doesnt work. Sorry if it turns out to be a simple mistake but I'm not familiar with this area of coding. This code is meant to search the string and find the 3rd number in it. Cheers in advance The code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
/**
 * Write a description of class Chicken here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Chicken extends Actor
{
    /**
     * Act - do whatever the Chicken wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        GreenfootImage hiho = new GreenfootImage ( "I am Greg" ,50, Color.BLACK, Color.WHITE);   
        setImage(hiho);
         
System.out.println(hiho.charAt(3));
         
    }   
}
Hersov Hersov

2014/12/31

#
Actually I realised my mistake and I have fixed it. What im now having problems with is that I cannot then identify it. This is the line of code that is not working.
1
if (chicken.charAt(3) ="L")
The error message highlights the "(3)". Does anyone know whats wrong? If you want me to write the whole code in then I will because its slightly different than the one above. Cheers in advance!
Super_Hippo Super_Hippo

2014/12/31

#
A = is never a comparison, == is. Usually one doesn't use == for strings though.
danpost danpost

2014/12/31

#
What is the error message? If 'chicken' is declared a String, will it always contain at least four characters (which is the character number you are trying to compare)? BTW, Super_Hippo is correct about comparing String objects -- using '==' will check to see if the two String objects are the same object. It will not compare the character sequences, only its location in memory. To compare String objects for content, use the java.lang.String instance method 'equals'. For comparing characters, however, using '==' is fine; however, literal characters are enclosed in single quotes, not double quotes. Hence, your line above should possibly be:
1
if (checken.length() > 3 && chicken.charAt(3) == 'L')
Hersov Hersov

2015/1/1

#
Cheers for the help guys. Now that I know that, how do I then edit the letter, I want to say if the if statement is true, then change the letter to A. I have attached my code. And Would it be possible for you to explain the chicken.length part, the string is longer than 4 letters if thats what it is. Cheers in advance! Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
String chicken = "I Like Life";
 
    public void act()
    {
         
         
        if (Greenfoot.mouseClicked(this))
        {
            System.out.println(chicken.charAt(3));
             
            if (chicken.length() > 3 && chicken.charAt(3) == 'L')
            {
               chicken.charAt(3) = "A" ;
            }
        }
 
    }
danpost danpost

2015/1/1

#
Yes -- the 'chicken.length' part returns the length of the string (in the case of "I Like Life", that would be 11). It does not hurt to have that in there even if your string currently is longer than 3 (never know, you may decide to alter the length of the string later). 'charAt' returns the value of the character at the give index -- as you have line 13 above, you are saying:
1
'L' = "A";
which obviously does not make any sense. Also, you still do not seem to understand that a character must be enclosed in single quotes, as double quotes are used for String objects. To change the 'L' in your string to an 'A', you need to break the string up into parts or convert it to a character array to work with and then revert it back to a string.
1
2
3
4
5
6
7
String chicken = "I Like Life";
// break apart
chicken = chicken.substring(0, 2)+"A"+chicken.substring(3);
// double conversion
char[] array = chicken.toCharArray();
array[2] = 'A';
chicken = new String(array);
Super_Hippo Super_Hippo

2015/1/1

#
If the string wouldn't be at least 4 characters long, then 'chatAt(3)' will return an error (IndexOutOfBoundsException). Btw, the fourth letter in your string is an "i". I think line 13 doesn't work as you want it. (Although I don't exactly know why you want to change an "L" to an "A" if the L was on the specified position of the string. I didn't test it. I am not 100% sure if this substring method changes the string itself. If it does, you would need to create a new string from the other.
1
chicken = chicken.substring(0,3) + "A" + chicken.substring(4);
danpost danpost

2015/1/1

#
@Hersov @Super_Hippo The first character in a string is at index '0'. The 'L' (in 'Like') is at index '2'. Counting in java usually starts at zero.
Super_Hippo Super_Hippo

2015/1/1

#
I know. I wrote the code like so that he can replace line 13 with it. Then the string won't be changed because there isn't an L at the right position. I don't know what he wants to do, if he has other strings which he compares, too. Then it maybe makes more sense to adjust the strings and not the code. But of course it also could be that he didn't know that it starts with 0...
danpost danpost

2015/1/1

#
@Super_Hippo, I had realized that you already knew this after re-reading the first couple lines of your post. It was just that the code you supplied had the incorrect indecis for changing the 'L' to an 'A'.
You need to login to post a reply.