Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Scan text and add each word alphabetical listing

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Posts
    6
    Thanks
    5
    Thanked 1 Time in 1 Post

    Exclamation Scan text and add each word alphabetical listing

    I'm scanning my text and I want to add them into array list, but it is not easy. I want each word store from arrayList of a to z which is arrayList size is 26. Example:
    arrayList a store string array of: an, and, apple, ...
    arrayList b store string array of: be, become, became....
    .
    .
    .
    arrayList z store string array of: zero, zone...

    In my class, I create:

    private static ArrayList<String[]> words = new ArrayList<String[]>(26); // to store all words

    In main, I do while loop to get each words and store in words array,

    please help me out to get it correctly and what is the easier way to do this problem. Here is my code. Thank

     
     
     
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.ArrayList;
    import java.util.LinkedList;
    import java.util.Scanner;
     
     
    public class BinaryTree {
     
    	private static ArrayList<String[]> words = new ArrayList<String[]>(26);
     
    	public Node root; 
    	public void addNode(int key, String name) {
    		Node newNode = new Node(key, name);
    		if (root == null) {
    			root = newNode;
    		} else {
    			Node focusNode = root;
    			Node parent;
    			while (true) {
    				parent = focusNode;
    				if (key <focusNode.key) {
    					focusNode = focusNode.leftChild;
    					if (focusNode == null) {
    						parent.leftChild = newNode;
    						return;
    					}
    				} else {
    					focusNode = focusNode.rightChild;
    					if (focusNode == null) {
    						parent.rightChild = newNode;
    						return;
    					}
    				}
    			}
    		}
    	}
     
    	public void inOrderTraverseTree(Node focusNode){
    		if (focusNode != null) {
    			inOrderTraverseTree(focusNode.leftChild);
    			System.out.println(focusNode);
    			inOrderTraverseTree(focusNode.rightChild);
    		}
    	}
     
    //	public void insertBack(String data){
    //		
    //	}
     
    	public static void main(String[] args) throws FileNotFoundException {
     
    	//	String[] eachWord = new String[26];
     
    //		BinaryTree theTree = new BinaryTree();
    //		theTree.addNode(key, name);
     
     
    		Scanner scan = new Scanner(new File("concordance.txt"));  
    		int i = 1;
    		while(scan.hasNext()) {
    			String nextToken = scan.next();
    			if (nextToken.equalsIgnoreCase("a")) {
    				// how did I add from a to z to arrayList???
    			}
    			i++;
    		}
    //		System.out.print(l.showList());
     
    	}
     
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Scan text and add each word alphabetical listing

    It's possible to have an ArrayList that contains an ArrayList of Strings. There could be 26 ArrayLists of Strings added to the first ArrayList, one for each letter.
    You don't want to have 26 ArrayLists named a through z.
    You don't want to work with String arrays. That is what ArrayLists are for.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    sydethsam (March 4th, 2014)

  4. #3
    Junior Member
    Join Date
    Mar 2014
    Posts
    6
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: Scan text and add each word alphabetical listing

    So, what would you suggest?

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Scan text and add each word alphabetical listing

    I thought I just did make some suggestions. One ArrayList that contains 26 ArrayLists of Strings.
    If you don't understand my answer, don't ignore it, ask a question.

  6. The Following User Says Thank You to Norm For This Useful Post:

    sydethsam (March 5th, 2014)

Similar Threads

  1. Add shadow for text in JTextArea
    By startas in forum AWT / Java Swing
    Replies: 7
    Last Post: September 17th, 2013, 02:55 PM
  2. [SOLVED] Alphabetical order
    By sbjibo in forum What's Wrong With My Code?
    Replies: 12
    Last Post: May 16th, 2012, 03:20 PM
  3. Reading a text file word by word
    By dylanka in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: October 21st, 2011, 02:06 PM
  4. Replies: 2
    Last Post: June 27th, 2011, 11:14 AM
  5. Replies: 5
    Last Post: April 7th, 2011, 11:22 AM

Tags for this Thread