Reputation: 5863
I have a simple java statement along the lines of:
String[] ykl = yklList.get(0).split(" ");
where, yklList is an ArrayList containing sentences which are split into words. The above works fine.
When I now try and loop this:
Loop:1
for (int i=0;i<=4;i++)
{
String[] ykl = yklList.get(i).split(" ");
}
It does not seem to work and throws me a compile error. Is the above loop wrong?
I have another for loop after the above:
Loop:2
for (String ykl : yklList)
{
//do something
}
It throws me a compile error here saying:
cannot find symbol
symbol : variable ykl
location: class test
for (String ykl : yklList)
Presumably Loop 1 went wrong somewhere?
Edit:
The "full" code looks something like this:
for (int i=0;i<=yklList.size()-1;i++)
{
String[] ykl = yklList.get(i).split(" ");
}
for (String y : ykl)
{
t.add(y);
}
and the error is:
cannot find symbol
symbol : variable ykl
location: class test
for (String y : ykl)
Upvotes: 1
Views: 159
Reputation: 132
In your appendix, the second cycle does not know about the variable ykl, as defined in the previous cycle and after leaving it - it is destroyed.
If variables were not removed - after a while all of the available computer memory would be filled, which would cause an error of the program. If only you were given the opportunity dal unnecessary variables - that is allowed a situation where you forgot to remove unneeded variables and memory, again, would be full. In this context, Java simplifies the development and can not think about what you are then permennye not removed.
To correct the error:
ArrayList <String> yklList = new ArrayList <String> ();
ArrayList <String> t = new ArrayList <String> ();
for (int i = 0; i <yklList.size (); i + +)
{
String [] ykl = yklList.get (i). Split ("");
for (String y: ykl)
t.add (y);
}
or: If you fill out absolutely all characters t of yklList:
ArrayList<String> yklList = new ArrayList<String>();
ArrayList<String> t = new ArrayList<String>();
String[] ykl = new String[0];
for (int i=0;i<yklList.size();i++) {
String[] tempYkl = yklList.get(i).split(" ");
String[] tempYklDub = new String[tempYkl.length];
String[] tempYklDubDub = new String[ykl.length];
for (int z=0; z< tempYkl.length; z++)
tempYklDub[z] = tempYkl[z];
for (int z=0; z<ykl.length; z++)
tempYklDubDub[z]=ykl[z];
ykl = new String[tempYklDubDub.length+tempYklDub.length];
for (int z=0; z<tempYklDubDub.length; z++)
ykl[z]=tempYklDubDub[z];
for (int z=tempYklDubDub.length, k=0; k<tempYklDub.length; z++, k++)
ykl[z]=tempYklDub[k];
}
for (String y : ykl)
t.add(y);
Upvotes: 1
Reputation: 686
You should make sure looping the right count of elements.
Try using size() for your ArrayList:
for (int i=0;i<ykl.size();i++)
{
String[] ykl = yklList.get(i).split(" ");
}
Keep in mind that every cycle overwrites your ykl
array. Defining it outside of your loop would solve this problem.
Edit: Try
String[] ykl = new String[ykl.size()];
for (int i=0;i<ykl.size();i++)
{
ykl[i] = yklList.get(i).split(" ");
}
for (String y : ykl)
{
t.add(y);
}
Upvotes: 0
Reputation: 39632
Regarding to your update:
You define ykl
in the first loop. So the variable is not in the scope of the second loop. Your code have to look like this:
for (String yklSentence : yklList) {
String[] ykl = yklSentence.split(" ");
for (String y : ykl) {
t.add(y);
}
}
By the way: If you would use an IDE like Eclipse you would see those errors in the editor!
Upvotes: 4
Reputation: 132
If I understood you correctly, you have less than 4 yklList elements. And, at the next iteration, it tries to take an element from outside the yklList
Solutions to the problem:
for (int i = 0; i <yklList.size(); i + +) {
String[] ykl = yklList.get(i).split(" ");
}
or
for (String yklListDub: yklList) {
String[] ykl = yklListDub.split(" ");
}
P.S. I'm sorry for my english)
Upvotes: 0