Results 1 to 11 of 11
  1. #1
    kraster is offline Member
    Join Date
    Jun 2012
    Posts
    6
    Rep Power
    0

    Default Text file to Array working values

    So i got a 100.000 entries in a text file, each on a row. I wanna calculate the inversions of them but seems there is a problem
    Java Code:
    import java.io.*;
    import java.util.*;
    
    
    public class Inversions{
    
        public static void main(String [] args){
        
        Scanner s = new Scanner("list.txt");
        ArrayList<String> lista = new ArrayList<String>();
        while (s.hasNext()){
            lista.add(s.next());
            
        }
        s.close();
        int x=0;
        for(int i=0;i<lista.size();i++)
        {
            for(int j=i+1;j<=lista.size();j++){
            
                if(lista.get(i).compareTo(lista.get(j))>0) x++;
            
            }        
        }
        
        
    
    
    }
    I can't operate with the values from 'lista' in any way..
    Last edited by kraster; 06-24-2012 at 05:23 PM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Text file to Array working values

    Also some general suggestions:
    • Don't use a count variable, but rather simply get the List's size via its size() method.
    • Lists such as ArrayLists are like arrays in that they are 0-based. Their indexes start at 0 and go up to size() - 1.
    • For this reason, for loops that iterate through Lists such as ArrayLists usually go from index 0 to index < myList.size(), not index <= myList.size(); as the latter will result in an ArrayIndexOutOfBoundsException.

  4. #4
    kraster is offline Member
    Join Date
    Jun 2012
    Posts
    6
    Rep Power
    0

    Default Re: Text file to Array working values

    array required, but java.util.ArrayList<java.lang.String> found // where i do the if. and thanks for the tips btw:)

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Text file to Array working values

    Quote Originally Posted by kraster View Post
    array required, but java.util.ArrayList<java.lang.String> found // where i do the if. and thanks for the tips btw:)
    Ah yes, OK. You are trying to use an ArrayList as if it were an array. You don't use array indices [i] with Lists such as ArrayLists, but rather you use the get(i) method.

  6. #6
    kraster is offline Member
    Join Date
    Jun 2012
    Posts
    6
    Rep Power
    0

    Default Re: Text file to Array working values

    can u write down that specific line? Cause i didn't really get it how to implement the get method. So all i wanna do that line is to compare 2 numbers and in case of first being bigger to increase x.

  7. #7
    cselic is offline Senior Member
    Join Date
    Apr 2010
    Location
    Belgrade, Serbia
    Posts
    278
    Rep Power
    5

    Default Re: Text file to Array working values

    ^Instead of using for example lista[i] you should use lista.get(i)

  8. #8
    kraster is offline Member
    Join Date
    Jun 2012
    Posts
    6
    Rep Power
    0

    Default Re: Text file to Array working values

    ^^ it works. but now when i try to run, it says : java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
    at java.util.ArrayList.rangeCheck(ArrayList.java:604)
    at java.util.ArrayList.get(ArrayList.java:382)
    at Inversions.main(Inversions.java:21)

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Text file to Array working values

    Quote Originally Posted by kraster View Post
    ^^ it works. but now when i try to run, it says : java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
    at java.util.ArrayList.rangeCheck(ArrayList.java:604)
    at java.util.ArrayList.get(ArrayList.java:382)
    at Inversions.main(Inversions.java:21)
    Please re-read my second post to this thread. Also, you'll want to put in some code to test that data is being read in correctly. In the loop where you read in information from the text file, consider printing out each line to the console just temporarily to be sure it's working OK. Once you've got all working, you'll then remove this test code.

  10. #10
    kraster is offline Member
    Join Date
    Jun 2012
    Posts
    6
    Rep Power
    0

    Default Re: Text file to Array working values

    I'm new to java and don't really know what to write. System.out.println of what in that while? Cause lista returns list.txt.. I had a version with bufferedreader working but that displayed me only like 100 out of 100.000.. and i've took ur advices since i first saw them(related to ur 2nd post)

    /edit ; edited my first post with what i got till now.
    Last edited by kraster; 06-24-2012 at 05:23 PM.

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Text file to Array working values

    I was suggesting something like this:

    Java Code:
        while (s.hasNext()){
            String line = s.next();
            lista.add(line);
    
            // the following is to test to make sure that you're reading the file correctly
            System.out.println("line: " + line); // delete this line of code once you know all works well.
    
        }

Similar Threads

  1. Getting Java To Use Values In A Text File
    By Gum in forum New To Java
    Replies: 1
    Last Post: 05-30-2012, 03:29 PM
  2. Extracting values from a text file
    By kryptonian03 in forum New To Java
    Replies: 4
    Last Post: 03-22-2011, 06:13 AM
  3. Replies: 14
    Last Post: 01-22-2011, 02:36 PM
  4. Java 2D array get values from text fields
    By raverz1tildawn in forum Java 2D
    Replies: 3
    Last Post: 01-08-2011, 12:56 AM
  5. [SOLVED] getting values from a text file
    By dav9999 in forum New To Java
    Replies: 8
    Last Post: 04-01-2008, 01:51 AM

Posting Permissions

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