java - Issues with String Array Concatenation -
i'm trying concatenate strings in array. want example ["a","b","c"]
become "abc"
. here's 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 = 0; < antal; i++){ system.out.print("ange ord: "); string nyttelement = scanner.nextline(); ordlista[i] = nyttelement; } string resultat; for(int = 0; < antal; i++){ resultat = resultat+ordlista[i]; } }
i following error:
laggaihop.java:21: error: variable resultat might not have been initialized resultat = resultat+ordlista[i];
why this? have initialized variable in row string resultat;
thanks.
no have declared variable. initializing means have given initial value. can replacing line with:
string resultat = "";
or can in 2 separate steps:
string resultat; //declaration resultat = ""; //initializiation
as far know, java initializes fields automatically. local variables, less automation. because assumed error-prone.
Comments
Post a Comment