2

After meddling with arrays and countless google searches, I can't seem to find the answer.

    public static void main(String args[]){
        String[] names = new String[4]; //I want to fill this up with data from country
        country(names);
        System.out.println(names(0)) //I want this to display Madrid
    }

    public static void country(String[] names){
        names[0] = "Madrid"; 
        names[1] = "Berlin";
        return;
    }

I'm not sure if this explains what I'm trying to do.

3
  • 1
    @ElliottFrisch also, wouldn't that give a compiler-error since he uses () instead of [] to access the 0-th array element? Commented Oct 3, 2014 at 23:02
  • Sorry my mind seemed to go blank, I didnt actually notice that. It did work as intended after changing the few mistakes. Commented Oct 3, 2014 at 23:05
  • @nem Good catch, I'd just use System.out.println(Arrays.toString(names)); Commented Oct 3, 2014 at 23:05

2 Answers 2

2

You really have to work on java syntax. Your code is quite simple so it should work immediately, but you have to be careful with some details, here is a code which works fine:

public static void main(String args[]) {
    String[] names = new String[4]; //I want to fill this up with data from country
    country(names);
    System.out.println(names[0]); //I want this to display Madrid
}

public static void country(String[] names) {
    names[0] = "Madrid";
    names[1] = "Berlin";
}

As you can see I use [ ] to access a value at a specific index in an array. I don't use any return in a void method neither.

You don't need to return the array in country method because java don't pass arguments on value (see http://javarevisited.blogspot.fr/2012/12/does-java-pass-by-value-or-pass-by-reference.html)

So I really advice you to read any tutorial you can find about java syntax to improve yourself for now.

Sign up to request clarification or add additional context in comments.

2 Comments

Yeah I just started leaning java last week. I didn't notice the silly mistakes I was making until the comments pointed it out.
that's normal when you start. I don't know any good english tutorial for beginning java, but I'm sure there are some posts about it in this forum ;) Java is fun, keep going ! And use a good IDE (lot of choice, my favorite are eclipse (open source) and IntelliJ (there is a free version)), it would have inform you there was syntax mistake, there message can be very helpful, like nem said it in comment from your question.
1

Arrays are accessed using [], not (). You are also missing a semicolon in the printing statement.

Change:

System.out.println(names(0))

To:

System.out.println(names[0]);  // use [] instead of () and add a semicolon

Also, the method country(String[] names) returns void so you don't need the return statement at the end of it (it's implied).

Here's how your code should look like:

   public static void main(String args[]){
        String[] names = new String[4]; //I want to fill this up with data from country
        country(names);
        System.out.println(names[0]); // use [] instead of () and add a semicolon
    }

    public static void country(String[] names){
        names[0] = "Madrid"; 
        names[1] = "Berlin";
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.