I have a program in which I need to read a file word by word and save this in a String variable. This works fine (I use a while loop), but the problem is that my string variable doesn't have the same value outside the loop. If I check what the variable contains inside the while loop, I get over 30000 words, but when I try the same thing outside the while-loop, only 1 word comes up.
String ord = null;
while (fil.hasNext()) {
ord = leser.next();
System.out.println(ord); //If I print it here I get the whole file
}
System.out.println(ord); // If i try to print out the content here, I only get 1 word
I don't understand why the variable "ord" doesn't contain the same inside and outside the loop, and how can I fix this? I of course need this variable in other places in my program, but since there is only 1 word stored, the rest of my program doesn't work as intended.
StringBuilderby concatenating them.