views:

80

answers:

4

i've an error here , and couldn't solve it :

String generate_card (String Fname , String Lname ,int  card_id   ){
    String cardId = null ;
    cardId = Fname.charAt(0) + Fname.charAt(0) + card_id++  ;

return  cardId;

}

A: 

I think this is in java,hence the charAt() method

the error here is that you didn't create a new string object,you just assigned it to null

you should have written the following in the second line:

String cardId = "";
Phobia
that shouldn't be the problem
Roflcoptr
Why would this be necessary? He already declared cardId, and he's attempting to define what its value?
ItzWarty
-1 It's not a problem; he reassigns the var in the next line.
Vuntic
+4  A: 

Perhaps try

cardId =  Character.toString(Fname.charAt(0)) +  Character.toString(Fname.charAt(0)) + card_id++;

Java(I assume) can't concatenate two chars and an int.

Igor Zevaka
Or alternatively cardId = "" + Fname.charAt(0) + Fname.charAt(0) + card_id++;
Vuntic
i've found the answer String generate_card (String Fname , String Lname ,int card_id ){ String cardId = null ; cardId = Fname.charAt(0) + Lname.charAt(0) + String.valueOf (card_id++ ) ; return cardId; }
soad el-hayek
@Igor: sry but this won't work. you cant call toString on the primitive char
Roflcoptr
Edited code, had a mild confusion with C# where `'c'.ToString()` is legal.
Igor Zevaka
yes now it works, but its a little bit complicated ;)
Roflcoptr
A: 

The problem is that Java won't automatically convert from int to String. This is necessary because you're trying to assing a new value to the String cardId. You could use:

cardId = Fname.charAt(0).toString() + Fname.charAt(0).toString() + Integer.toString(card_id++);

Note: by Java conventions your variable names and parameters shouldn't start with a capital letter

Roflcoptr
A: 

i've found the answer :

String generate_card (String Fname , String Lname ,int  card_id   ){
    String cardId = null ;
    cardId = Fname.charAt(0) + Lname.charAt(0) + String.valueOf(card_id++ ) ;

return  cardId;

}

soad el-hayek