Originally posted by: emmpee
Sorry man, don't have the time/energy to go through that much code, especially without tabstops
If you post it here, you might have more luck.
Here goes nothing.
I'm trying to sort by a specific field within db.txt that is read into an array by the tokenizer. The string reply is parsed into an int and I want to send that into to the compareTo method so it can use it as the thing it compares. I've been messing with this thing for 3 full days and I'm going out of my mind.
db.txt is below the program
//start
import java.util.*;
public class HW4
{
final static int MAX = 4;
final static int BIG = 16;
static int count;
public static void main(String[] asd)
{
DataBase[] dv = new DataBase[BIG];
readArray(dv);
spark(dv);
System.out.println();
}
public static int[] spark(DataBase[] x)
{
int[] answer = new int[1];
System.out.println("How would you like to sort the database?"
+ "\n1 = First name" + "\n2 = Last name"
+ "\n3 = Social Security number" + "\n4 = Age"
+ "\n5 = Exit");
StreamReader reader = new StreamReader(System.in);
BufferedReader buff = new BufferedReader(reader);
String s = buff.readLine();
int num = Integer.parseInt(s);
switch (num)
{
case 1: System.out.println("\nSorting by first name...\n");
final int sortby = 0;
// DataBase.assign(num);
answer[0] = 1;
Rays.sort(x);
printArray(x); break;
case 2: System.out.println("\nSorting by last name...\n");
Rays.sort(x);
printArray(x); break;
case 3: System.out.println("\nSorting by Social Security number...\n");
Rays.sort(x);
printArray(x); break;
case 4: System.out.println("\nSorting by age...\n");
Rays.sort(x);
printArray(x); break;
case 5: System.out.println("\nGoodbye.\n");
System.exit(1);
break;
default: System.out.println("Invalid entry, please retry.\n");
spark(x); break;
}
continyou(x);
return answer;
}
public static void continyou(DataBase[] x)
{
//readArray(x);
System.out.println("\nWould you like to continue?\n1 = Yes\n2 = No");
StreamReader reader = new StreamReader(System.in);
BufferedReader buff = new BufferedReader(reader);
String t = buff.readLine();
int num2 = Integer.parseInt(t);
if (num2 == 1)
{
spark(x);
} else
if (num2 == 2)
{
System.out.println("Goodbye.");
System.exit(1);
}
else
{
System.out.println("Invalid entry.");
continyou(x);
}
}
public static void readRecord(DataBase db, String line)
{
StringTokenizer toke = new StringTokenizer(line);
int j = 0;
while(toke.hasMoreTokens() )
{
db.item[j] = toke.nextToken();
j++;
}
}
public static void readArray(DataBase[] dv)
{
count = 0;
DataBase data ;
FileStringReader f = new FileStringReader("db.txt");
String line;
do
{
line = f.readLine();//reads a record
//System.out.println(line);
/*
* tests for null lines and blank lines so that they are not included
* in the array dv
*/
if(line != null && line.trim().length() != 0)
{
data = new DataBase();
readRecord(data, line);//data is a record w/4 fields
dv[count] = data;
count++;
}
}while(line != null);
}
public static void printArray(DataBase[] dv)
{
for(int j = 0; j < BIG; j++)
{
printRecord(dv[j]);
}
}
public static void printRecord(DataBase db)
{
for(int j = 0; j< MAX; j++)
{
System.out.print(db.item[j] + "\t" );
}
System.out.println();
}
}
class DataBase implements Comparable
{
final int MAX = 4;
String[] item = new String[MAX];
public int compareTo(Object obj)
{
DataBase d2 = (DataBase)obj;
DataBase d1 = (DataBase)this;
if(d1.item[0].compareTo(d2.item[0]) > 0)
{
return 1;
}
else if(d1.item[0].compareTo(d2.item[0]) < 0)
{
return -1;
}
else
{
return 0;
}
}
}
class Rays
{
public static void sort(Comparable[] x)
{
boolean sorted = false;
Comparable temp;
while (!sorted)
{
sorted = true ;
for(int j = 0; j < x.length - 1; j++)
{
//prototype is "public int compareTo(Object obj)"
if(x[j].compareTo(x[j+1]) > 0 )
{
temp = x[j];
x[j] = x[j+1];
x[j+1] = temp;
sorted = false;
}
}
}
}
}
***********************************************************************
what follows is db.txt
************************************************************************
marin gil 269972639 34
abrams rita 005456788 17
nottobe tobe 029209330 19
backus jim 379228370 23
lucky lady 039470923 57
canada oh 437983890 46
caliper surgical 457474709 18
dante ron 090229292 29
english rum 457090290 35
goto state 507348278 19
home hume 387928783 23
identity crie 236982373 56
jackson andy 109290029 36
korn cob 329480928 25
faulty tower 439287983 12
abrams sol 098337377 54