Java append instances of a String to a String which separated by commas -
Java append instances of a String to a String which separated by commas -
so have string variable meant hold names of cars separated commas.
string cars = "";
what want append cars string. way new auto added:
string newcar1 = "mini"; string newcar2 = "landrover"; appendtocars(newcar1); appendtocars(newcar2);
then have this, need help with.
public void appendtocars(string newcar) { cars = cars + "," + newcar; }
so output should be:
mini,landrover
but it's:
[,]mini
been racking brain hours figuring out how it, can't result want.
im using junit test reads :
@test public void testappendtocars() { system.out.println("appendtocars"); string newcar1 = "mini"; string newcar2 = "landrover"; string expresult = newcar1 + "," + newcar2; testdel.appendtocars(newcar1); testdel.appendtocars(newcar2); string result = testdel.getcars(); assertequals("delivery notes incorrectly stored", expresult, result);
i think have variable scope issue. illustration uses code takes scope consideration:
public class temp { static string cars = ""; public static void appendtocars(string something) { if (cars.equals("")){ cars = something; } else { cars= cars + "," + something; } } public static void main(string[] args){ string newcar1 = "mini"; string newcar2 = "landrover"; appendtocars(newcar1); appendtocars(newcar2); system.out.println(cars); } }
this class homecoming following:
mini,landrover
java string variables instance delimiter
Comments
Post a Comment