2009-03-06 15:43:07 UTC
Exception in thread "main" java.lang.Array IndexOutOf BoundsException: 20 at sort.main(sort.java:14)
(NOTE: I added some spaces to make it easier to read)
This is the code:
import java.util.Scanner;
import java.io.File;
class sort
{
public static void main(String[]args) throws Exception
{
Scanner sc = new Scanner(new File("file.input"));
obj[] list = new obj[20];
int count=0;
while(sc.hasNext())
{
list[count] = new obj(sc.nextDouble(), sc.next());
count++;
}
sort(list);
for(int i=0;i
}
public static class obj
{
private double d;
public String s;
public obj(double dd, String ss)
{
d = dd;
s = ss;
}
double getD()
{
return d;
}
String getS()
{
return s;
}
}
static void sort(obj a[])
{
for (int i = a.length; --i>=0; )
{
boolean flipped = false;
for (int j = 0; j {
if (a[j].getD() > a[j+1].getD())
{
obj T = a[j];
a[j] = a[j+1];
a[j+1] = T;
flipped = true;
}
}
if (!flipped)
{
return; }
}
}
}
This is what the program is supposed to do:
There is a file that consists of a sequence r1 s1 r2 s2 r3 s3 ..., where ri is a real number of type double, and si is a String of symbols (that does not include empty spaces in the string its self). If one constructs a pair out of elements ri and si, for each index i, and then sorts these pairs in such a way that first elements of the pairs will be in monotonically increasing order, then the second components of the pairs, separated by empty spaces, form a text to create a paragraph.
So the first thing I have to do is scan the file, and make pairs out of the ri and si, but then I have to sort them by the doubles, ri. Once sorted, I have to print out the text, just the si.
The first few lines of this file look like this:
.3670360229982874 be 2.1758656832275705 of 7.558754198292892 in 7.414471768174697 behind 1.8838317339124944 know 1.20303007486753 hearing 7.734195638483449 and 7.1822892709521495 carried 6.53492389798644 other, 7.237144368426898 of
(note, the text above is all on one line. The word wrap is just breaking it up)
Thanks for your help!