0

I am trying to print a line from the listed file, which contains a word stated. But the program does nothing. Can someone help me with the code? Thanks

import java.io.File;
import java.util.ArrayList;
import java.util.Scanner;


public class SearchingArrayLists {
public static void main(String[] args) throws Exception {

      ArrayList names = new ArrayList();
      Scanner scan = new Scanner(new File("random.txt"));

      while (scan.hasNext()){
        names.add(scan.next());
      }

      if (names.contains("legal")){
      System.out.println(scan.next());
      }

      scan.close();

    }

}

UPDATE:

Sorry, removed the loop. the file contains random text where the word "legal" is in there. the file was read by the scanner beforehand.

Eran
  • 387,369
  • 54
  • 702
  • 768
the_no_1
  • 25
  • 1
  • 8
  • Why the empty `for` loop? – Maroun Dec 09 '14 at 13:35
  • what does `random.txt` contain? And is the file getting read by scanner? – Naman Gala Dec 09 '14 at 13:35
  • 2
    Does random.txt contain the word legal also there won't be a next() here:`System.out.println(scan.next());` because you have already iterated through the entire file. – brso05 Dec 09 '14 at 13:36
  • 1
    As a side note try [not to use raw types](http://stackoverflow.com/questions/2770321/what-is-a-raw-type-and-why-shouldnt-we-use-it). Use `ArrayList` instead of solely `ArrayList`. – Narmer Dec 09 '14 at 13:39
  • Sorry removed the loop. the file contains random text where the word "legal" is in there. the file was read by the scanner beforehand – the_no_1 Dec 09 '14 at 13:39

2 Answers2

2

System.out.println(scan.next()); will throw an exception, since you are calling it after you consumed all the input in the while (scan.hasNext()) loop.

But it may not even reach that exception if your names list doesn't contain an exact match to the String "legal".

Eran
  • 387,369
  • 54
  • 702
  • 768
1
Scanner scan = new Scanner(new File("random.txt"));
  String name = "" ;
  while (scan.hasNextLine()){
    name  = scan.nextLine();
   if (name.contains("legal")){
  System.out.println(name);
  }
}




  scan.close();

Try above code , you even don't need list. I have not compiled it , so remove if any syntax error you got.

Panther
  • 3,312
  • 9
  • 27
  • 50
  • Hey, this works good thanks but i would like it so it displays the line the word is on. I should have made this more clear. Thanks – the_no_1 Dec 09 '14 at 13:57
  • you tried this ?? is it not displaying whole line ? sorry i am not not having jvm and ide with me right now – Panther Dec 09 '14 at 13:59
  • Yeah, i tried it it prints the word. I would like it so it prints the row though – the_no_1 Dec 09 '14 at 14:01
  • One more thing if you don't mind. Do you know how to do it so it counts how many times the word is in there? – the_no_1 Dec 09 '14 at 14:15
  • http://stackoverflow.com/questions/767759/occurrences-of-substring-in-a-string refer this for getting in each line and you can put variable outside loop and keep adding it. – Panther Dec 09 '14 at 14:35
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/66488/discussion-between-the-no-1-and-panther). – the_no_1 Dec 09 '14 at 14:39