0

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.

0

4 Answers 4

3

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.

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

Comments

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

Or

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

Comments

1

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)).

1 Comment

Based on the question, I think the OP is confusing initializing with declaring...
0

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.

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.