Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Search ArrayList for value and return Entire String

$
0
0
Hi guys!

New to the forum and coding Java in general, so go easy!

Basically I have been given a task to scan a large text document, an eBook for example into an arrayList.

When the document has been scanned, you need to be able to search for a word within that arrayList.

When the word has been searched for, the program needs to be able to return the entire line that the word was found in, and the number of this line.

I have pasted what I have written so far but I am massively struggling to get to grips with searching within the arrayList.

Any help would be greatly appreciated! Thanks :)/>

import java.io.*;
import java.util.*;

public class searchArrayList {

	public static void main(String[] args) throws FileNotFoundException
	{
		String fileName;
		ArrayList<String> longString = new ArrayList<String>();
		String word;
		String containingLine;

		// Declare new ArrayList
		
		try { 

			Scanner scanName = new Scanner(System.in);

			System.out.println("Enter the file you wish to scan:> ");

			fileName = scanName.nextLine();

			Scanner scan = new Scanner(new File(fileName));
		
			// While statement to add to the array list while there is text on the next line to scan
		
			while(scan.hasNext())
				{

		    		longString.add(scan.next());

				}
			
				try {
					
					Scanner scanWord = new Scanner(System.in);

					System.out.println("Enter the word you wish to search for:> ");

					word = scanWord.nextLine();
                                        
                                        //Needing some kind of search function in here to search for the word within the       arrayList
			           }
					
				} catch (NoSuchObjectException t) {
					
					System.out.println("The word you searched for has not been found.");
					
				}
				
		}
		
		catch (FileNotFoundException e) {
			
			System.err.println("File cannot be found.");
			
		}
	}		
}



Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>