The main problems in your current code:
You have to define you're working with a list of
String
s. You can do this by adding theString
generic in yourArrayList
declaration, otherwise you're declaring a rawArrayList
, thus all the elements will be treated asObject
s:ArrayList<String> projectStrings = new ArrayList<String>();
or the Java 7 way, using the diamond operator:
ArrayList<String> projectStrings = new ArrayList<>();
You haven't defined a
concat
method in your code. Probably you want to useString#concat
:System.out.println(projectStrings.get(1).concat(projectStrings(0)));
Another way to concat String
s is by using the +
symbol:
System.out.println(projectStrings.get(1) + projectStrings(0));
Just in case you want your current code to work, define a concat
method outside the main
method:
class stringProject {
//this method should be static in order to be used in other static methods like main
static String concat(String str1, String str2) {
//naive implementation
return str1.concat(str2);
}
public static void main(String[] args) {
//your current code here...
//since you have defined a concat method, you can use it with no problems
System.out.println(concat(projectStrings.get(1), projectStrings.get(0)));
}
}
Another advice: you don't need to add import java.lang.*
, this is added by default by the compiler, so you can remove this line.
After your code edit, you closed the main
method just before printing your output. This is how your code looks now:
public static void main(String[] args)
{ //this is where your main method starts
ArrayList<String> projectStrings = new ArrayList<String>();
projectStrings.add("The ");
projectStrings.add("quick ");
projectStrings.add("brown ");
projectStrings.add("fox ");
projectStrings.add("jumped ");
projectStrings.add("over ");
projectStrings.add("the ");
projectStrings.add("lazy ");
projectStrings.add("dog.");
} //this is where your main method ends
//this line is outside any method, thus you get those errors
System.out.println(projectStrings.get(1).concat(projectStrings.get(0)));
} //this is where your stringProject class definition ends
Solution: just move that System.out.println(...)
line inside your main
method:
public static void main(String[] args)
{ //this is where your main method starts
ArrayList<String> projectStrings = new ArrayList<String>();
projectStrings.add("The ");
projectStrings.add("quick ");
projectStrings.add("brown ");
projectStrings.add("fox ");
projectStrings.add("jumped ");
projectStrings.add("over ");
projectStrings.add("the ");
projectStrings.add("lazy ");
projectStrings.add("dog.");
//easy fix
System.out.println(projectStrings.get(1).concat(projectStrings.get(0)));
} //this is where your main method ends
} //this is where your stringProject class definition ends