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 8 of 8

Thread: Need Help Fast - Please Help in Java ArrayList

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    7
    My Mood
    Stressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need Help Fast - Please Help in Java ArrayList

    I am trying to store a file into an ArrayList and then have user input from console search for an element in the Arraylist and return found or not. It keeps giving me -1.

    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.util.ArrayList;
    import java.util.Scanner;
    public class arrayl {
        public static void main(String args[] ) throws FileNotFoundException
        {
            System.out.println("Please enter choice ");
            Scanner input = new Scanner(System.in);
            String search = input.next();
            Scanner file = new Scanner(new File("c:\\Documents and Settings\\name\\Desktop\\Book.txt"));        
                ArrayList<String> test = new ArrayList<>();
                while (file.hasNext()){
                test.add(file.next());
                    }
                  System.out.println(test.indexOf(search));
    file.close();
        }
     
    }
    Last edited by anonb; November 5th, 2012 at 01:29 PM. Reason: put incorrect tags


  2. #2
    Elite Member curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,132
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: Need Help Fast - Please Help in Java ArrayList

    Recommendations:
    • Use a debugger or add some debug statements to your code such as read the file token into a String, and then print it out with a println statement immediately after obtaining it and just before you put it into the arraylist.
    • Print out the entire ArrayList contents with a println statement immediately after the while loop.
    • Clean up your code. Poorly formatted code is hard for us to understand and hard for you or us to debug.

  3. #3
    Junior Member
    Join Date
    Oct 2012
    Posts
    7
    My Mood
    Stressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need Help Fast - Please Help in Java ArrayList

    public class arrayl {
        public static void main(String args[] ) throws FileNotFoundException
        {
            System.out.println("Please enter title of book: ");
            Scanner input = new Scanner(System.in);
            String search = input.next();
            //file with absolute path
            Scanner file = new Scanner(new File("c:\\Documents and Settings\\name\\Desktop\\Book.txt"));        
            //arraylist named as test
            ArrayList<String> test = new ArrayList<>();
             //creating loop that will read file   
             while (file.hasNext()){
                 //adding file to arraylist
                test.add(file.next());
            }
    //Using users input, named search, to find  element in the arraylist and print return found or not 
              System.out.println(test.indexOf(search));
    file.close();
        }
     
    }
    Sorry about that. Sometimes, I format it afterwards, but that is just my test file to make sure that it works. Why would i print out the entire ArrayList contents with a println statement immediately after the while loop when it will only use 1 line of the file. The file has multiple lines and I need it to search entire file...

  4. #4
    Junior Member
    Join Date
    Oct 2012
    Posts
    7
    My Mood
    Stressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need Help Fast - Please Help in Java ArrayList

    Also, when you mentioned:
    Use a debugger or add some debug statements to your code such as read the file token into a String, and then print it out with a println statement immediately after obtaining it and just before you put it into the arraylist.

    Are you saying to use a tokenizer or something? If so, I don't see what the difference is because it is reading the entire file by using the loop. Please explain...

  5. #5
    Elite Member curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,132
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: Need Help Fast - Please Help in Java ArrayList

    I am recommending that you do these things so you can figure out why your program is not working. You need to do some debugging.

  6. #6
    Junior Member
    Join Date
    Oct 2012
    Posts
    7
    My Mood
    Stressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need Help Fast - Please Help in Java ArrayList

    I have been debugging. I have been using system.out.println to see what the file is reading and that is how i kow it is reading the entire file. However, it looks as if it is just reading line by line, instead of all the elements within the line. I've also come to find out that ArrayList only searches for objects and that is why it keeps saying it is false. How do I convert my String search, which is the users input to an object?

  7. #7
    Elite Member curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,132
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: Need Help Fast - Please Help in Java ArrayList

    Strings *are* objects, and so your search may be correct. I suggest that you:
    1) post the contents of the text file.
    2) put in the println I was mentioning and show us the results from that.
    3) show us what the user enters.

  8. #8
    Junior Member
    Join Date
    Mar 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post obviously

    obviously only suitable for a child's toy, Jun Lan and my contacts a long time,hollister. seen some miscellaneous book is in junior high school period. I blurted out,louboutin pas cher. and its far-reaching effects. to be honest.
    you had to call me before he died that time. will be able to forget the experience of all things. so that they feel happy just like you what? The Masters after listening to the saying a word. can not remember how I came to this mountain village,a friend once told m
    one may fall in love with many people during the lifetime. when you finally get your own happiness, you will understand the previous sadness is kind of treasure, which makes you better to hold and cherishthe people you love.. the doctor owned the block in front of him. care for our mother and two people are to before a great step forward,louboutin. favor discussion masters. Mountain quiet step numerous odd is the middle of the night,but I did not yield
    one may fall in love with many people during the lifetime. when you finally get your own happiness, you will understand the previous sadness is kind of treasure, which makes you better to hold and cherishthe people you love.. it could be for the final period of the Chinese New Year paintings Everything so I had to novelty over excited over fibrillation wonderful things the child to eat a meal together Lao Lao homely regardless of the language multilingual This is my emotional life has brought too many regrets I want to let mother touches my breath my heat and clear,hollister online shop.
    from less to more,louboutin, "there is a world people,abercrombie france, so-called 酒逢知己千杯少 be a child should do. a person is not in the dormitory,hollister? take the initiative to participate in rehearsal,hollister uk; Zhu Jie. slowly comes,abercrombie, remember when you loved me, But I still can not forget the old Huaiyin City.you must also expect her to come Ideals and love enrich your life accompanied me how many sleepless nights they appear not because you like or don't like and stop footsteps.a unique light instantly absence.
    I fish hook in the fish mouth tightly , King of Qi inherit the throne , to help with home furniture all moved to the yard before they let out to play,abercrombie pas cher. the University has graduated and is now an enterprise to work,hollister online shop. ' I do see the amazing coincidence prompt in the days of water shortage in the mother or girl,I think to look at
    one may fall in love with many people during the lifetime. when you finally get your own happiness, you will understand the previous sadness is kind of treasure, which makes you better to hold and cherishthe people you love.,hollister france.

Similar Threads

  1. How to use an ArrayList and what is its advantage over array?
    By JavaPF in forum Java SE API Tutorials
    Replies: 4
    Last Post: December 21st, 2011, 04:44 AM
  2. need HELP FAST PLEASE HELP ME
    By britben95 in forum Member Introductions
    Replies: 1
    Last Post: November 14th, 2011, 08:05 PM
  3. need help fast
    By coke32 in forum Loops & Control Statements
    Replies: 6
    Last Post: November 1st, 2011, 12:04 AM
  4. need help fast!!
    By nwollis in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 27th, 2010, 06:12 PM
  5. How to use an ArrayList and what is its advantage over array?
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 1
    Last Post: May 17th, 2009, 02:12 PM