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

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
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 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
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 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
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 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.

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: October 31st, 2011, 11:04 PM
  4. need help fast!!
    By nwollis in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 27th, 2010, 05: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, 01:12 PM