Results 1 to 8 of 8
  1. #1
    OmegaZero69 is offline Member
    Join Date
    Mar 2013
    Posts
    12
    Rep Power
    0

    Default Adding two text files to a ArrayList and searching for a name

    Hi I am having a little trouble with my code I have written the code but when i run the code it gives me a out of memory error. Is there something missing or did I put something in the wrong place


    Java Code:
    import java.io.*;
    import java.io.FileNotFoundException;
    import java.util.*;
    
    
    public class NameSearch {
    
    	/**
    	 * @param args
    	 * @throws FileNotFoundException 
    	 */
    	public static void main(String[] args) throws FileNotFoundException {
    		// TODO Auto-generated method stub
    		ArrayList<String> namesList = new ArrayList <String>();
    		
    		String childname;
    		Scanner keyboard = new Scanner(System.in); 
    		File file1 = new File("BoyNames.txt");
    		Scanner inputFile1 = new Scanner(file1);
    		File file2 = new File("GirlNames.txt");
    		Scanner inputFile2 = new Scanner (file2);
    		
    		while (inputFile1.hasNext()){
    			namesList.add("BoyNames.txt");
    			namesList.add("GirlNames.txt");
    		}
    		
    		System.out.println("Please enter a Boy's or Girl's name?");
    		childname = keyboard.toString();
    		
    		if(namesList.equals(childname)){
    			System.out.println("The name you have choosen is in the list of popular names");
    		}
    		else{
    			System.out.println("The name you have choosen is not the list of popular names");
    		}
    	}
    
    }
    Last edited by OmegaZero69; 03-18-2013 at 01:01 AM.

  2. #2
    angstrem is offline Member
    Join Date
    Mar 2013
    Posts
    11
    Rep Power
    0

    Default Re: Adding two text files to a ArrayList and searching for a name

    Java Code:
    while (inputFile1.hasNext()){
                namesList.add("BoyNames.txt");
                namesList.add("GirlNames.txt");
            }
    Pleasy, describe each line of this code sample. What each of these three lines intended to do? And what do each of them really do?

  3. #3
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    335
    Rep Power
    4

    Default Re: Adding two text files to a ArrayList and searching for a name

    Hi OmegaZero69, welcome to the forums.

    I can see where angstrem is going with the comment above but I suspect you may need a bit of extra guidance.

    The while loop checks if 'inputFile1' has another token (entry). If it does then it adds the text "BoyNames.txt" and "GirlNames.txt" to the String array.
    The problem you have is 'hasNext()' doesn't cause the program to move to the next token within the file. The effect of this is the program continues to reference the first token and so always see another token.
    What I think you intend to do is add the token to the array and move to the next. The 'next()' method return the String value of the token which can be placed n the array.

    Another thing to not is at the moment only inputFile1 is being checked so the file "GirlNames.txt" is never checked.

    Your code on line 31 is also going to cause issues. Instead you want to use a loop or iterator to go through the array comparing each value to the user input.

    Regards.

  4. #4
    OmegaZero69 is offline Member
    Join Date
    Mar 2013
    Posts
    12
    Rep Power
    0

    Default Re: Adding two text files to a ArrayList and searching for a name

    Thank you for your replies.

    I have fixed my code a little more and my problem now is that when I make it do the name search it displays both messages of " the name is found" and "not found" the number of times that if looked in the array. How can I rewrite it so it checks and only display one message instead of the 400 messages? Do I have to put "if-else" statements inside the other "if-else" statements or use something else.

    Java Code:
    import java.io.*;
    import java.io.FileNotFoundException;
    import java.util.*;
    
    
    public class NameSearch {
    
    	/**
    	 * @param args
    	 * @throws FileNotFoundException 
    	 */
    	public static void main(String[] args) throws FileNotFoundException {
    		// TODO Auto-generated method stub
    		ArrayList<String> namesList = new ArrayList <String>();
    		
    		String childname;
    		
    		Scanner keyboard = new Scanner(System.in); 
    		File file1 = new File("BoyNames.txt");
    		Scanner inputFile1 = new Scanner(file1);
    		File file2 = new File("GirlNames.txt");
    		Scanner inputFile2 = new Scanner (file2);
    		
    		while (inputFile1.hasNext()){
    			namesList.add(inputFile1.next());
    		}
    		
    		inputFile1.close();
    		
    		while (inputFile2.hasNext()){
    			namesList.add(inputFile2.next());
    		}
    		inputFile2.close();
    		
    		System.out.println("Please enter a Boy's or Girl's name?");
    		childname = keyboard.nextLine();
    		
    		
    		for (int i = 0; i < namesList.size(); i++){
    		
    		if(namesList.get(i).contains(childname)){
    			System.out.println("The name you have choosen is in the list of popular names");
    		}
    		else{ 
    			
    			System.out.println("The name you have choosen is not the list of popular names");
    		}
    		
    		}
    	}
    Last edited by OmegaZero69; 03-18-2013 at 01:04 AM.

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: Adding two text files to a ArrayList and searching for a name

    It's simple. Since you have an if statement inside a loop it will execute the if statement for each item in the list. But you only want to search for the name in the list once. So what do you think you need to do?

    By the way when you repeat code (such as reading the text files) you should be thinking about how to turn that code into a method and calling it twice.

  6. #6
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default Re: Adding two text files to a ArrayList and searching for a name

    You should also consider print the message (either the name is found or not) outside the for-loop. Use the for-loop only to check whether the name is found or not and when you found the name you can exit the loop. So that you don't have to wait until all lines are iterated.

  7. #7
    tamilarasi is offline Member
    Join Date
    Nov 2012
    Location
    India
    Posts
    70
    Rep Power
    0

    Default Re: Adding two text files to a ArrayList and searching for a name

    you change this code

    Java Code:
    for (int i = 0; i < namesList.size(); i++){
             
            if(namesList.get(i).contains(childname)){
                System.out.println("The name you have choosen is in the list of popular names");
            }
            else{
                 
                System.out.println("The name you have choosen is not the list of popular names");
            }
             
            }
    into this
    Java Code:
    if(namesList.contains(childname)){
                System.out.println("The name you have choosen is in the list of popular names");
            }
            else{
                System.out.println("The name you have choosen is not the list of popular names");
            }
    And you refer this link:

    Java arraylist contains

    And this link got how to check the string contains in arraylist..
    Regards
    Android developer at Trinay Technology Solutions,http://www.trinaytech.com,5705750475

  8. #8
    OmegaZero69 is offline Member
    Join Date
    Mar 2013
    Posts
    12
    Rep Power
    0

    Default Re: Adding two text files to a ArrayList and searching for a name

    Thank you for the responses and thank you tamilarsi the link helped me understand a little more now the code is working a lot better now i need to make some minor changes but that I can handle. Thank you again everybody.

Similar Threads

  1. Searching Text Files Then Removing (My First Post!)
    By technoreject in forum New To Java
    Replies: 12
    Last Post: 06-29-2011, 06:51 AM
  2. Searching through folder to find text inside all files
    By dazzabiggs in forum New To Java
    Replies: 3
    Last Post: 05-03-2011, 01:20 PM
  3. Searching and changing ArrayList
    By sinister in forum New To Java
    Replies: 36
    Last Post: 03-31-2011, 02:55 PM
  4. Replies: 2
    Last Post: 04-21-2008, 11:43 AM
  5. Searching an arraylist
    By adelgado0723 in forum New To Java
    Replies: 1
    Last Post: 04-15-2008, 01:09 PM

Tags for this Thread

Posting Permissions

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