简体   繁体   中英

I/O with an array list

I was just wondering if there is a way to loop through a text file until a particular string is found. For example, say you have a text file with the following in it:

banana

apple

grapes

melon

orange

cherries

strawberry chocolate vanilla

I basically want to write a program that loops through the input file until it gets to a particular string the user specifies and then stores the next line in an array list. So, basically say if I imputed cherries I want it to store strawberry, chocolate, vanilla in an array list. For the life of me I cannot figure out how to do this though, so anything would be appreciated. What I have so far is below.

public static void main(String[] args) throws IOException {
        String line;
        ArrayList<String> names = new ArrayList<>();
        Scanner in = new Scanner(System.in);
        System.out.print("Enter the input file: ");
        String input = in.next();
        FileReader file = new FileReader(input);

        BufferedReader reader = new BufferedReader(file);

        System.out.print("What fruit do you want: ");
        String fruit = in.next();
        line = reader.readLine();


        while ((line != null)) {
            if(line.equals(fruit){
          }
            }

Just loop through the input until the searched line is encountered and store every line found afterwards in the list.

    String line;

    while((line = reader.readLine()) != null)
        if(line.equals(fruit))
            break;

    ArrayList<String> lines = new ArrayList<>();
    while((line = reader.readLine()) != null)
        lines.add(line);

if I understand your question correctly, I have an idea that you can start from

Scanner scanner = new Scanner(file);
List<String> list = new ArrayList<String>();
while (scanner.hasNextLine()) { // read the entire file into list but it consumes time especially if the file is big (not perfect choice)
  list.add(scanner.nextLine());
}

now what you can do

// add boolean to announce the occurrance of the word
boolean found = false;
for(String word: list){ // then you have a greater control over it to search
    if(word.equals(fruit)){
       found = true;
    }
    if (found) {
        // start taking the rest of the array into a new array or whatever you want to do
    }
}

You could use a boolean to indicate when the value is found and when it is true you can add the line in the List :

 boolean isFound = false;
 String line;
 while ((line=reader.readLine())!= null) {
       if(!isFound && line.equals(fruit){
         isFound = true;
       }
       else if (isFound){
         names.add(line);
       }
 }

Create flag isFound. Set it to true when you find the string. It will then process on the next loop and set to false like so.

bool isFound=false;       
while ((line != null)) {
          if(isFound==true){
              names.add(string)
              isFound=false;
          }
            if(line.equals(fruit){
              isFound=true;
          }
            }

check it out:

public static void main(String[] args) throws IOException {
    String line;
    List<String> names = new ArrayList<>();
    Scanner in = new Scanner(System.in);
    System.out.print("Enter the input file: ");
    String input = in.next();
    FileReader file = new FileReader(input);

    BufferedReader reader = new BufferedReader(file);

    System.out.print("What fruit do you want: ");
    String fruit = in.next();

    boolean found=false;
    while ((line = reader.readLine()) != null){
        if (line.equals(fruit) || found) {
            names.add(line);
            found=true;
        }
    }
}

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