Question

This is my code. I know how to use the concat operator, but not with arrays or arraylist.

import java.util.*;
class stringProject {
    static String concat(String str1, String str2) {
        return str1.concat(str2);
    }

    public static void main(String[] args) 
    {
      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.");
    }
    System.out.println(projectStrings.get(1).concat(projectStrings.get(0)));
    }
}

And my errors are:

File: C:\Users\Daniel\stringProject.java  [line: 23]
Error: Syntax error on token "println", Identifier expected after this token
File: C:\Users\Daniel\stringProject.java  [line: 23]
Error: Syntax error on tokens, AnnotationName expected instead
File: C:\Users\Daniel\stringProject.java  [line: 23]
Error: Syntax error on token ")", delete this token
File: C:\Users\Daniel\stringProject.java  [line: 23]
Error: Syntax error, insert "Type VariableDeclaratorId" to complete FormalParameter
File: C:\Users\Daniel\stringProject.java  [line: 23]
Error: Syntax error, insert ")" to complete MethodDeclaration
File: C:\Users\Daniel\stringProject.java  [line: 25]
Error: Syntax error on token "}", delete this token
Was it helpful?

Solution

The main problems in your current code:

  1. You have to define you're working with a list of Strings. You can do this by adding the String generic in your ArrayList declaration, otherwise you're declaring a raw ArrayList, thus all the elements will be treated as Objects:

    ArrayList<String> projectStrings = new ArrayList<String>();
    

    or the Java 7 way, using the diamond operator:

    ArrayList<String> projectStrings = new ArrayList<>();
    
  2. You haven't defined a concat method in your code. Probably you want to use String#concat:

    System.out.println(projectStrings.get(1).concat(projectStrings(0)));
    

Another way to concat Strings 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

OTHER TIPS

All you need is simply

ArrayList<String> projectStrings = new ArrayList<>();
//populate as needed
System.out.println(projectStrings.get(0) + projectStrings.get(1));

if you want to use ArrayList.

Note my use of generics. The ArrayList you defined is a collection of Object. Mine is a collection of String. Doing it your way costs you compile-time guarantees and also makes some of the other answers unworkable.

I think you just need to understand how ArrayList's work. You are probably trying to do this:

ArrayList<String> projectStrings = new ArrayList<String>();

...

System.out.println(projectStrings.get(1).concat(projectStrings.get(0)));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top