Sahand
Sahand

Reputation: 8380

Issues with String Array Concatenation

I'm trying to concatenate all strings in an array. I want for example ["a","b","c"] to become "abc". Here's my code:

import java.util.Scanner;
import java.util.ArrayList;

public class Laggaihop 
{
    private static Scanner scanner = new Scanner( System.in );
    public static void main(String[] args)
    {
        System.out.print("Ange antal ord: ");
        String input = scanner.nextLine();
        int antal = Integer.parseInt(input);
        String[] ordlista;
        ordlista = new String[antal];
        for(int i = 0; i < antal; i++){
            System.out.print("Ange ord: ");
            String nyttelement = scanner.nextLine();
            ordlista[i] = nyttelement;
        }
        String resultat;
        for(int i = 0; i < antal; i++){
            resultat = resultat+ordlista[i];
        }


    }

I get the following error:

Laggaihop.java:21: error: variable resultat might not have been initialized
            resultat = resultat+ordlista[i];

Why is this? I have initialized the variable in the row String resultat;

Thanks.

Upvotes: 0

Views: 76

Answers (4)

user3248346
user3248346

Reputation:

All local variables need to be initalised before being used. Doing so will solve the compilation error. In your program you have declared the variable resultat but have not initialised it.

Section 4.12.5 in Java 8 Language Specification states:

A local variable (§14.4, §14.14) must be explicitly given a value before it is used, by either initialization (§14.4) or assignment (§15.26), in a way that can be verified using the rules for definite assignment (§16 (Definite Assignment)).

Upvotes: 1

blocjager
blocjager

Reputation: 77

You should initialize the String resultat. The final code would look like:

import java.util.Scanner;
import java.util.ArrayList;

public class Laggaihop
{
    private static Scanner scanner = new Scanner( System.in );
    public static void main(String[] args)
    {
        System.out.print("Ange antal ord: ");
        String input = scanner.nextLine();
        int antal = Integer.parseInt(input);
        String[] ordlista;
        ordlista = new String[antal];
        for(int i = 0; i < antal; i++){
            System.out.print("Ange ord: ");
            String nyttelement = scanner.nextLine();
            ordlista[i] = nyttelement;
        }
        String resultat="";
        for(int i = 0; i < antal; i++){
            resultat = resultat+ordlista[i];
        }
        System.out.println(resultat);
    }
}

This should solve your issue.

Upvotes: 0

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 477676

No you have declared the variable. Initializing means you have given it an initial value. You can do this by replacing the line with:

String resultat = "";

Or you can do it in two separate steps:

String resultat; //declaration
resultat = "";   //initializiation

As far as I know, Java only initializes fields automatically. For local variables, it does less automation. Probably because this is assumed to be error-prone.

Upvotes: 3

Spidey
Spidey

Reputation: 974

String resultat;
resultat=resultat+something;
                   ^what is value in here?

Or

String resultat; //declared
resultat="something";//initialized

Upvotes: 1

Related Questions