Results 1 to 11 of 11
- 06-24-2012, 02:46 PM #1
Member
- Join Date
- Jun 2012
- Posts
- 6
- Rep Power
- 0
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
I can't operate with the values from 'lista' in any way..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++; } } }
Last edited by kraster; 06-24-2012 at 05:23 PM.
-
Re: Text file to Array working values
What happens? What errors, if any, do you see?
-
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.
- 06-24-2012, 04:17 PM #4
Member
- Join Date
- Jun 2012
- Posts
- 6
- Rep Power
- 0
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:)
-
Re: Text file to Array working values
- 06-24-2012, 04:22 PM #6
Member
- Join Date
- Jun 2012
- Posts
- 6
- Rep Power
- 0
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.
- 06-24-2012, 04:35 PM #7
Senior Member
- Join Date
- Apr 2010
- Location
- Belgrade, Serbia
- Posts
- 278
- Rep Power
- 4
Re: Text file to Array working values
^Instead of using for example lista[i] you should use lista.get(i)
- 06-24-2012, 04:40 PM #8
Member
- Join Date
- Jun 2012
- Posts
- 6
- Rep Power
- 0
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)
-
Re: Text file to Array working values
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.
- 06-24-2012, 05:15 PM #10
Member
- Join Date
- Jun 2012
- Posts
- 6
- Rep Power
- 0
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.
-
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
-
Getting Java To Use Values In A Text File
By Gum in forum New To JavaReplies: 1Last Post: 05-30-2012, 03:29 PM -
Extracting values from a text file
By kryptonian03 in forum New To JavaReplies: 4Last Post: 03-22-2011, 06:13 AM -
need to input values from a text file into an array and count values
By pds8475 in forum New To JavaReplies: 14Last Post: 01-22-2011, 02:36 PM -
Java 2D array get values from text fields
By raverz1tildawn in forum Java 2DReplies: 3Last Post: 01-08-2011, 12:56 AM -
[SOLVED] getting values from a text file
By dav9999 in forum New To JavaReplies: 8Last Post: 04-01-2008, 01:51 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks