简体   繁体   中英

Reading in from a text file, with character limit

I am trying to read in text from a text file, it's supposed to read in every word delimited by just a space(ie 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 

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));
  }

}

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, eg

do {
    SecondarrayList.add(list.get(i));
} while (findlenghtofListinChars(SecondarrayList) < 100);

will become

if(findlenghtofListinChars(SecondarrayList) < 100){
    SecondarrayList.add(list.get(i);
}else{
    break;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM