Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.util.ArrayList;
- import java.util.Scanner;
- public class main {
- public static char getCharAtIndexFromString(ArrayList<String> strings, int index){
- //gaseste char-ul de la indexul dat
- //null daca indexul e mai mare decat lungimea fisierului
- int counter = 0;
- Character toReturn = null;
- for (String line : strings) {
- if(counter + line.length() > index){
- toReturn = line.charAt(index - counter);
- }
- else{
- counter += line.length();
- }
- }
- return toReturn;
- }
- public static void main(String[] args) {
- ArrayList<String> strings = new ArrayList<>();
- try {
- File myObj = new File("filename.txt");
- Scanner myReader = new Scanner(myObj);
- while (myReader.hasNextLine()) {
- String data = myReader.nextLine();
- strings.add(data);
- }
- myReader.close();
- System.out.println(getCharAtIndexFromString(strings, 12));
- } catch (FileNotFoundException e) {
- System.out.println("An error occurred.");
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement