Reputation: 502
I am trying to read in text from a text file, it's supposed to read in every word delimited by just a space(i.e ignore everything else).
so for now, i am reading in every word from the scanner and adding it to a list. Then i am trying to add from list to SecondarrayList only if the number of characters added to the Secondarylist doesn't exist 100 characters.
Yes i am trying to iterate the first List, and make sure every word that can fit under 100 characters fits under the limit in each list and doesn't add word mid way or break up words
i ran this :
for (int i = 0; i < SecondarrayList.size(); i++) {
System.out.println(SecondarrayList.get(i));
}
but not thing happens :/
Scanner input = null;
try {
input = new Scanner(file);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
List<String> list = new ArrayList<String>();
ArrayList<String> SecondarrayList = new ArrayList<String>();
String word = null;
while (input.hasNext()) {
word = input.next();
// System.out.println(word);
list.add(word + " ");
}
for (int i = 0; i < list.size(); i++) {
// System.out.println(list.get(i));
do {
SecondarrayList.add(list.get(i));
} while (findlenghtofListinChars(SecondarrayList) < 100);
}
for (int i = 0; i < SecondarrayList.size(); i++) {
System.out.println(SecondarrayList.get(i));
}
}
}
// returns number of length of chars
public static int findlenghtofListinChars(ArrayList<String> arrayL) {
StringBuilder str = new StringBuilder("");
for (int i = 0; i < arrayL.size(); i++) {
// System.out.print(arrayL.get(i));
str = str.append(arrayL.get(i));
}
return str.length();
}
The sample output when prints word(also we can ignore "," "/" all the others as well just delimted by space)
small
donations
($1
to
$5,000)
are
particularly
important
to
maintaining
tax
exempt
Upvotes: 1
Views: 1494
Reputation: 436
In this case, the problem with your code is that you are using a while loop whose condition is always true.
This is because the input that you gave it has a character length of ~80 which will always be <100
For this reason I suggest changing the while loop of yours to become an if statement, e.g.
do {
SecondarrayList.add(list.get(i));
} while (findlenghtofListinChars(SecondarrayList) < 100);
will become
if(findlenghtofListinChars(SecondarrayList) < 100){
SecondarrayList.add(list.get(i);
}else{
break;
}
Upvotes: 0
Reputation: 154
Try this, I think you want to do something like this :
Scanner input = null;
try {
input = new Scanner(new File(file));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
List<String> list = new ArrayList<String>();
ArrayList<String> SecondarrayList = new ArrayList<String>();
String word = null;
while (input.hasNext()) {
word = input.next();
list.add(word);
}
int totalSize = 0;
for (String eachString : list) {
totalSize +=eachString.length();
if(totalSize >=100){
break;
}else{
SecondarrayList.add(eachString);
}
}
for (int i = 0; i < SecondarrayList.size(); i++) {
System.out.println(SecondarrayList.get(i));
}
}
Upvotes: 1