Results 1 to 3 of 3
  1. #1
    herpeslurpy is offline Member
    Join Date
    Nov 2012
    Posts
    40
    Rep Power
    0

    Default Help finding a substring in a text file with multiple lines and printing all of them.

    I need to read from a text file with multiple lines, and find all lines with an occurrence of that string, and display them.
    So far, this is what I have:
    Java Code:
    import java.util.*;
    import java.io.*;
    public class find {
    	public static void main(String[] args) {
    		Scanner input = new Scanner(System.in);
    		String find, line = null;
    		String fileName = "findIt.txt";
    		Scanner inputStream = null;
    		
    		try{
    			inputStream = new Scanner (new File(fileName));
    		}catch(Exception e){
    			System.out.println("Couldn't find " +fileName);
    		}
    		
    		System.out.println("What are you looking for?");
    		find = input.nextLine();
    		
    		while(inputStream.hasNextLine()){
    			line = inputStream.nextLine();
    			System.out.println(line);
    				if(line.indexOf(find) != -1){
    					System.out.println(line);
    			}
    		}
    		inputStream.close();
    	}
    }
    The text file I'm using is just random babble, with "asdf" randomly throughout it. It seems to display each line with "asdf" twice before it goes to the next line, and only displays lines without "asdf" once, although all of that is still in the original order. I can tell that the problem is with my looping, but I'm
    can't tell where the problem is exactly. If anyone can help, thanks.

  2. #2
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    2

    Default Re: Help finding a substring in a text file with multiple lines and printing all of t

    couldnt you have a data structure, and every time you find the instance of said search criteria add that line to the data structure, then input stream !haveNextLine, print out every element in the data structure?

  3. #3
    herpeslurpy is offline Member
    Join Date
    Nov 2012
    Posts
    40
    Rep Power
    0

    Default Re: Help finding a substring in a text file with multiple lines and printing all of t

    Quote Originally Posted by monkeyjr97 View Post
    couldnt you have a data structure, and every time you find the instance of said search criteria add that line to the data structure, then input stream !haveNextLine, print out every element in the data structure?
    Thanks, but I actually managed to figure it out just after I posted it. I had it set to look for anything with an index that != -1, so it gave me everything.I added this:

    Java Code:
    import java.util.*;
    import java.io.*;
    public class find {
    	public static void main(String[] args) {
    		Scanner input = new Scanner(System.in);
    		String find, line = null;
    		String fileName = "findIt.txt";
    		Scanner inputStream = null;
    		
    		try{
    			inputStream = new Scanner (new File(fileName));
    		}catch(Exception e){
    			System.out.println("Couldn't find " +fileName);
    		}
    		
    		System.out.println("What are you looking for?");
    		find = input.nextLine();
    		
    		while(inputStream.hasNextLine()){
    			line = inputStream.nextLine();
    			if(line.indexOf(find) >= 0){
    				System.out.println(line);
    			}else if(line.indexOf(find) < 0){
    					System.out.println("Couldn't find anything with that");
    					break;
    			}
    		}
    		inputStream.close();
    	}
    }
    Thanks for the suggestion, anyways.

Similar Threads

  1. Replies: 1
    Last Post: 02-25-2013, 04:41 AM
  2. PDFBox multiple text lines and fields
    By igorland in forum New To Java
    Replies: 1
    Last Post: 02-02-2013, 01:21 PM
  3. Sorting and printing multiple lines of data
    By ManInTheMiddle in forum New To Java
    Replies: 1
    Last Post: 03-27-2012, 09:43 PM
  4. Writing multiple lines to text file at once
    By Plex in forum New To Java
    Replies: 14
    Last Post: 11-08-2010, 09:17 PM
  5. Deleting multiple lines of text from a file
    By barman in forum New To Java
    Replies: 4
    Last Post: 06-29-2010, 09:12 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •