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

Changing a word in an arraylist problem

$
0
0
Hello, I have written a simple program that scans a text file into an ArrayList, searches for a specified word in the arraylist and prints the position of the word and the line it's on. I am trying to write some code that can make the word display in upper case when the line is printed but I am having some trouble.
Any help would be appreciated.

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


public class ArrayListSearch 
{

	public static void main(String[] args) 
	{
		File textFile = new File("Principia.txt");	
		Scanner scan = null;
		Scanner input = null;
		ArrayList<String> text = new ArrayList<String>();
		

		try
		{
			scan = new Scanner(textFile);
			input = new Scanner(System.in);
		}

		catch (FileNotFoundException ex)
		{
			System.err.println("No such name");
		}

		while (scan.hasNext())
		{
			text.add(scan.nextLine());
		}


		int line = 0;
		int count = 0;

		System.out.print("Enter word to search for: "  );

		String searchInput = input.next();

		for (String s : text)
		{
			//line counter
			line++;

			//search for word
			if (s.contains(searchInput))
			{

				//print line and position of word
				System.out.println("Line: \"" + s + "\"");
				System.out.println("Word found on line: (" + line + ") at position (" + s.indexOf(searchInput) + ")\n");

				/* Tried this to change word to upper case
				 * 
				 * int wordLocation = text.indexOf(searchInput);
				 * text.remove(searchInput);
				 * text.add(searchInput.toUpperCase());
				 * System.out.println(text);
				 *
				 * need iterator?
				 */
			}

			//number of times word appears in text
			if (s.indexOf(searchInput) > 0)
			{
				count++;
			}	


		}

		System.out.println("Number of times word(s) found: " + count + "\n");


	}
}





Viewing all articles
Browse latest Browse all 51036

Trending Articles



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