Reading Data From Text File

Having trouble reading in data from a text file

Page 1 of 1

10 Replies - 4629 Views - Last Post: 30 January 2009 - 03:02 AM Rate Topic: -----

#1 klmr3d  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 05-November 08

Reading Data From Text File

Posted 20 January 2009 - 11:37 PM

Hey guys,

I am trying to read in data from a text file. How it works (well supposed to heh) is like this... I have a text file with info for 13 people (each person has 15 attributes). The text file starts with the number 13 so that the first line read in is the number 13, and the program uses that number as a loop counter. I have displayed the Employment.java code and the Print.java code. Everything is done in employee and then print is used to display it.

Also the text file contents have been displayed.

The code compiles fine, but I get a run time error... here it is:
init:
deps-jar:
compile:
run:
Begin Month: Begin Day: Begin Year: Name: Address: Phone #: SSN: Age: Sex: Employer Name: Employer Address: Employer Phone #: Years of Employment: Title: Salary:
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Found I-O problem java.lang.NullPointerException
BUILD SUCCESSFUL (total time: 0 seconds)

Employment.java
/* Todd Riethmiller
 * January 19th 2009
 * Data Structures
 * Professor Kenison
 * Read in data from a text file and store it in an array
*/
package Employee;

import java.io.*;

public class Employment
{
	// Create variables
	private String month;
	private String day;
	private String year;
	private String name;
	private String addy;
	private String number;
	private String ssn;
	private String age;
	private String sex;
	private String ename;
	private String eaddy;
	private String enumber;
	private String nyear;
	private String title;
	private String salary;
	
   public void readEmploy()
	{
		try
		{   // Set up to read in from a text file
			File inFile = new File("EMPLOYEE.txt");
			FileReader fr = new FileReader(inFile);
			BufferedReader br = new BufferedReader(fr);
			
			// Variables to count items and read them in
			String numPpl;
			int pplCount;
			int i;
			
			// Read in how many items there are
			numPpl = br.readLine();
			pplCount = Integer.parseInt(numPpl);
			Employment [] thePpl = new Employment[pplCount];
			
			// Read in the items as separated by the deliminator
			for (i = 0; i < pplCount; i++)
			{
				thePpl[i].setMonth(br.readLine());
				thePpl[i].setDay(br.readLine());
				thePpl[i].setYear(br.readLine());
				thePpl[i].setName(br.readLine());
				thePpl[i].setAddy(br.readLine());
				thePpl[i].setNumber(br.readLine());
				thePpl[i].setSSN(br.readLine());
				thePpl[i].setAge(br.readLine());
				thePpl[i].setSex(br.readLine());
				thePpl[i].setEname(br.readLine());
				thePpl[i].setEaddy(br.readLine());
				thePpl[i].setEnumber(br.readLine());
				thePpl[i].setNyear(br.readLine());
				thePpl[i].setTitle(br.readLine());
				thePpl[i].setSalary(br.readLine());
			}
			// Print out each line
			for (i = 0; i < pplCount; i++)
			{
				System.out.println(thePpl[i]);
			}
			// Finished reading so close file
			br.close();
		}
		// Break program if problem found
		catch (Exception e)
		{
			System.out.println("Found I-O problem " + e);
		}
	}
   
   // Mutators
   public void setMonth(String month)
   {
	   this.month = month;
   }
   
   public void setDay(String day)
   {
	   this.day = day;
   }
   
   public void setYear(String year)
   {
	   this.year = year;
   }
   
   public void setName(String name)
   {
	   this.name = name;
   }
   
   public void setAddy(String addy)
   {
	   this.addy = addy;
   }

   public void setNumber(String number)
   {
	   this.number = number;
   }

   public void setSSN(String ssn)
   {
	   this.ssn = ssn;
   }

   public void setAge(String age)
   {
	   this.age = age;
   }

   public void setSex(String sex)
   {
	   this.sex = sex;
   }

   public void setEname(String ename)
   {
	   this.ename = ename;
   }

   public void setEaddy(String eaddy)
   {
	   this.eaddy = eaddy;
   }

   public void setEnumber(String enumber)
   {
	   this.enumber = enumber;
   }

   public void setNyear(String nyear)
   {
	   this.nyear = nyear;
   }

   public void setTitle(String title)
   {
	   this.title = title;
   }

   public void setSalary(String salary)
   {
	   this.salary = salary;
   }

	// Accessors
	public String getMonth()
	{
		return month;
	}
	
	public String getDay()
	{
		return day;
	}
	
	public String getYear()
	{
		return year;
	}
	
	public String getName()
	{
		return name;
	}
	
	public String getAddy()
	{
		return addy;
	}

	public String getNumber()
	{
		return number;
	}

	public String getSSN()
	{
		return ssn;
	}

	public String getAge()
	{
		return age;
	}

	public String getSex()
	{
		return sex;
	}

	public String getEname()
	{
		return ename;
	}

	public String getEaddy()
	{
		return eaddy;
	}

	public String getEnumber()
	{
		return enumber;
	}

	public String getNyear()
	{
		return nyear;
	}

	public String getTitle()
	{
		return title;
	}

	public String getSalary()
	{
		return salary;
	}

	// Method to create appropriate spacing
	public static String mySetw(String s, int size)
	  {
		String x = s;
		for (int i = s.length(); i < size; i++)
			x += " ";
		return x;
	  } 
	
	// Method to print out data in aligned format
	public String toString()
	{
		return mySetw(getMonth(), 15) + mySetw(getDay(), 15) + mySetw(getYear(), 15) + mySetw(getName(), 15) + mySetw(getAddy(), 15) + mySetw(getNumber(), 15) + mySetw(getSSN(), 15) + mySetw(getAge(), 15) + mySetw(getSex(), 15) + mySetw(getEname(), 15) + mySetw(getEaddy(), 15) + mySetw(getEnumber(), 15) + mySetw(getNyear(), 15) + mySetw(getTitle(), 15) + mySetw(getSalary(), 15);
	}
}



Print.java
/* Todd Riethmiller
 * January 19th 2009
 * Data Structures
 * Professor Kenison
 * Display the data from Employment.java
*/
package Employee;

import java.io.*;

public class Print
{
	public static void main(String[] args) throws IOException 
	{
		// Create column headers for the data being read in
		Employment test = new Employment();
		System.out.println("Begin Month:			 " + "Begin Day:			 " + "Begin Year:		  " + "Name:		  " + "Address:		  " + "Phone #:		  " + "SSN:		  " + "Age:		  " + "Sex:		  " + "Employer Name:		  " + "Employer Address:		  " + "Employer Phone #:		  " + "Years of Employment:		  " + "Title:		  " + "Salary:		  ");
		System.out.println("-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------");
		test.readEmploy();
	}
}



Here is the text file contents
I'm using the code to just make it visually nicer...
13
12
3
2006
JOHN SMITH
132 ELM STREET
605-932-6541
002456721
23
M
GILLETTE RAZOR
123 ANDOVER AVE
617-884-0987
2
RAZOR TESTER
15600
11
10
2004
MARY JONES
32 BAY AVE.
617-543-4421
004234562
25
F
CITY OF MANCHESTER
432 ELM ST.
603-622-1717
3
JANITOR
14500
1
23
1987
TOM KELLEY
11 HIGH ST.
603-892-3317
021345654
43
M
JONES TRUCKING
487 LOWELL ST.
508-989-6543
21
TRUCK DRIVER
25900
1
32
1990
SUSAN DESPARATE
321 KINGS HGY.
603-421-9865
165437689
41
F
KELLY GIRLS
23 WORKING WAY
603-896-6655
18
SECRETARY
15000
2
12
1998
BILLY WORKER
63 RAND AVE		
603-555-2342
432765431
37
M
KELLY GIRLS
23 WORKING WAY
603-896-6655
10
SECRETARY
14500
1
4
1983
TESS TRUEHEART
952 WINSTON BLVD.
617-543-7781
009645439
55
F
DAILY PLANET
4567 NEWSPAPER AVE.
414-506-9876
25
REPORTER
78000
2
15
1963
MAXWELL SMART
1 CHOAS AVE	   
904-326-6741
007007007
70
M
FBI
231 HIDDEN LANE
800-700-7007
45
SECRET AGENT
70070
1
26
2006
FRED SAVAGE
2 WONDER WAY
421-654-8767
001234567
13
M
DAILY PLANET
4567 NEWSPAPER AVE.
414-506-9876
2
NEWSPAPER BOY
2006
12
7
1970
DEAN SMITH
2 NCAA WAY
508-672-0091
020365768
59
M
UNC
DEAN SMITH ARENA
508-672-0029
35
BASKETBALL
98000
2
14
1979
TARK SHARK
1 NCAA WAY
919-825-5438
098763231
61
M
UNLV
1 RATING AVE
919-825-5444
29
BASKETBALL
99999
2
11
1993
CONNIE CHUNG
3 MUMBLE WAY
826-541-2311
006754324
38
F
NBC
STUDIO CENTER
826-541-0098
12
REPORTER
75000
3
11
1996
BARBARA BUSH
1 MOTHERLY WAY
909-946-8765
004532344
57
F
MEALS ON WHEELS
2 POVERTY LANE
909-946-8854
12
VOLUNTEER
0
1
30
2004
GEORGE H. BUSH
1 MOTHERLY WAY
909-946-8765
004532343
59
M
MEALS ON WHEELS
2 POVERTY LANE
909-946-8854
4
PAID FUNDRAISER
30000



I know this is a lot, but I've been racking my brain trying to figure out why this isn't working. I've done similar programs, but with less attributes per person. I really hope someone can help me out with this. Thanks!

Red

Is This A Good Question/Topic? 0
  • +

Replies To: Reading Data From Text File

#2 DaneAU  Icon User is offline

  • Great::Southern::Land
  • member icon

Reputation: 284
  • View blog
  • Posts: 1,617
  • Joined: 15-May 08

Re: Reading Data From Text File

Posted 21 January 2009 - 01:01 AM

There is an issue with your array setup.
Found I-O problem java.lang.NullPointerException


Meaning that you are missing something in your loops... it is trying to get information from something that does not exist. Have a look into the loops you have setup and see if you can find the issue.

Also perhaps consider changing the way you are doing things and make an object of some kind out of each "group" of data class perhaps EmployeeInfo and use a constructor that takes all the attributes so that you can have nice Objects to take data from etc...

Just my 2c if you want an example let me know :)
Was This Post Helpful? 1
  • +
  • -

#3 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Reading Data From Text File

Posted 21 January 2009 - 01:04 AM

You create an Employment array but do not populate it with Employment objects.
...
for (i = 0; i < pplCount; i++) {

	thePpl[i] = new Employment();

	thePpl[i].setMonth(br.readLine());
...


Was This Post Helpful? 1
  • +
  • -

#4 klmr3d  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 05-November 08

Re: Reading Data From Text File

Posted 21 January 2009 - 10:40 AM

Hey BBQ, I would love to see an example of what you're talking about. It was a thought I had but I'm still not too great with Java

Thanks, n8wxs. I think I forget to do that a lot.
Was This Post Helpful? 0
  • +
  • -

#5 epper  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 20-January 09

Re: Reading Data From Text File

Posted 21 January 2009 - 05:13 PM

If you made the text file have all of one persons info on one line to read from the file you could use the StringTokenizer class but I dont know if it would help.
Was This Post Helpful? 0
  • +
  • -

#6 klmr3d  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 05-November 08

Re: Reading Data From Text File

Posted 21 January 2009 - 06:22 PM

Thanks everyone,

I got my file data read in, and printed out. Now I want to sort the array.
How would I use the compareTo to sort the people by age?
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Reading Data From Text File

Posted 21 January 2009 - 06:35 PM

View Postklmr3d, on 21 Jan, 2009 - 05:22 PM, said:

Thanks everyone,

I got my file data read in, and printed out. Now I want to sort the array.
How would I use the compareTo to sort the people by age?

as your age is a String you just have to return the result of the compareTo() of the String class applied to bot Employment age

class Employement implements Comparable {
....
	public int compareTo(Object o) {
		Employment other = (Employment) o;
		return getAge().compareTo(other.getAge);
	}
}



*Edited: oups look at who didn't put the correct [ code] tags :-)

This post has been edited by pbl: 21 January 2009 - 09:43 PM

Was This Post Helpful? 0
  • +
  • -

#8 kranthi_java  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 27-January 09

Re: Reading Data From Text File

Posted 27 January 2009 - 05:35 AM

Hi klmr3d

i tried this code but i got some error in this like
pplCount = Integer.parseInt(numPpl);

Error:
java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
at java.lang.Integer.parseInt(Integer.java:468)
at java.lang.Integer.parseInt(Integer.java:497)
at com.ibm.Employment.readEmploy(Employment.java:56)
at com.ibm.Print.main(Print.java:29)

so can you rectify this error.
thanks in advance


View Postklmr3d, on 20 Jan, 2009 - 10:37 PM, said:

Hey guys,

I am trying to read in data from a text file. How it works (well supposed to heh) is like this... I have a text file with info for 13 people (each person has 15 attributes). The text file starts with the number 13 so that the first line read in is the number 13, and the program uses that number as a loop counter. I have displayed the Employment.java code and the Print.java code. Everything is done in employee and then print is used to display it.

Also the text file contents have been displayed.

The code compiles fine, but I get a run time error... here it is:
init:
deps-jar:
compile:
run:
Begin Month: Begin Day: Begin Year: Name: Address: Phone #: SSN: Age: Sex: Employer Name: Employer Address: Employer Phone #: Years of Employment: Title: Salary:
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Found I-O problem java.lang.NullPointerException
BUILD SUCCESSFUL (total time: 0 seconds)

Employment.java
/* Todd Riethmiller
 * January 19th 2009
 * Data Structures
 * Professor Kenison
 * Read in data from a text file and store it in an array
*/
package Employee;

import java.io.*;

public class Employment
{
	// Create variables
	private String month;
	private String day;
	private String year;
	private String name;
	private String addy;
	private String number;
	private String ssn;
	private String age;
	private String sex;
	private String ename;
	private String eaddy;
	private String enumber;
	private String nyear;
	private String title;
	private String salary;
	
   public void readEmploy()
	{
		try
		{   // Set up to read in from a text file
			File inFile = new File("EMPLOYEE.txt");
			FileReader fr = new FileReader(inFile);
			BufferedReader br = new BufferedReader(fr);
			
			// Variables to count items and read them in
			String numPpl;
			int pplCount;
			int i;
			
			// Read in how many items there are
			numPpl = br.readLine();
			pplCount = Integer.parseInt(numPpl);
			Employment [] thePpl = new Employment[pplCount];
			
			// Read in the items as separated by the deliminator
			for (i = 0; i < pplCount; i++)
			{
				thePpl[i].setMonth(br.readLine());
				thePpl[i].setDay(br.readLine());
				thePpl[i].setYear(br.readLine());
				thePpl[i].setName(br.readLine());
				thePpl[i].setAddy(br.readLine());
				thePpl[i].setNumber(br.readLine());
				thePpl[i].setSSN(br.readLine());
				thePpl[i].setAge(br.readLine());
				thePpl[i].setSex(br.readLine());
				thePpl[i].setEname(br.readLine());
				thePpl[i].setEaddy(br.readLine());
				thePpl[i].setEnumber(br.readLine());
				thePpl[i].setNyear(br.readLine());
				thePpl[i].setTitle(br.readLine());
				thePpl[i].setSalary(br.readLine());
			}
			// Print out each line
			for (i = 0; i < pplCount; i++)
			{
				System.out.println(thePpl[i]);
			}
			// Finished reading so close file
			br.close();
		}
		// Break program if problem found
		catch (Exception e)
		{
			System.out.println("Found I-O problem " + e);
		}
	}
   
   // Mutators
   public void setMonth(String month)
   {
	   this.month = month;
   }
   
   public void setDay(String day)
   {
	   this.day = day;
   }
   
   public void setYear(String year)
   {
	   this.year = year;
   }
   
   public void setName(String name)
   {
	   this.name = name;
   }
   
   public void setAddy(String addy)
   {
	   this.addy = addy;
   }

   public void setNumber(String number)
   {
	   this.number = number;
   }

   public void setSSN(String ssn)
   {
	   this.ssn = ssn;
   }

   public void setAge(String age)
   {
	   this.age = age;
   }

   public void setSex(String sex)
   {
	   this.sex = sex;
   }

   public void setEname(String ename)
   {
	   this.ename = ename;
   }

   public void setEaddy(String eaddy)
   {
	   this.eaddy = eaddy;
   }

   public void setEnumber(String enumber)
   {
	   this.enumber = enumber;
   }

   public void setNyear(String nyear)
   {
	   this.nyear = nyear;
   }

   public void setTitle(String title)
   {
	   this.title = title;
   }

   public void setSalary(String salary)
   {
	   this.salary = salary;
   }

	// Accessors
	public String getMonth()
	{
		return month;
	}
	
	public String getDay()
	{
		return day;
	}
	
	public String getYear()
	{
		return year;
	}
	
	public String getName()
	{
		return name;
	}
	
	public String getAddy()
	{
		return addy;
	}

	public String getNumber()
	{
		return number;
	}

	public String getSSN()
	{
		return ssn;
	}

	public String getAge()
	{
		return age;
	}

	public String getSex()
	{
		return sex;
	}

	public String getEname()
	{
		return ename;
	}

	public String getEaddy()
	{
		return eaddy;
	}

	public String getEnumber()
	{
		return enumber;
	}

	public String getNyear()
	{
		return nyear;
	}

	public String getTitle()
	{
		return title;
	}

	public String getSalary()
	{
		return salary;
	}

	// Method to create appropriate spacing
	public static String mySetw(String s, int size)
	  {
		String x = s;
		for (int i = s.length(); i < size; i++)
			x += " ";
		return x;
	  } 
	
	// Method to print out data in aligned format
	public String toString()
	{
		return mySetw(getMonth(), 15) + mySetw(getDay(), 15) + mySetw(getYear(), 15) + mySetw(getName(), 15) + mySetw(getAddy(), 15) + mySetw(getNumber(), 15) + mySetw(getSSN(), 15) + mySetw(getAge(), 15) + mySetw(getSex(), 15) + mySetw(getEname(), 15) + mySetw(getEaddy(), 15) + mySetw(getEnumber(), 15) + mySetw(getNyear(), 15) + mySetw(getTitle(), 15) + mySetw(getSalary(), 15);
	}
}



Print.java
/* Todd Riethmiller
 * January 19th 2009
 * Data Structures
 * Professor Kenison
 * Display the data from Employment.java
*/
package Employee;

import java.io.*;

public class Print
{
	public static void main(String[] args) throws IOException 
	{
		// Create column headers for the data being read in
		Employment test = new Employment();
		System.out.println("Begin Month:			 " + "Begin Day:			 " + "Begin Year:		  " + "Name:		  " + "Address:		  " + "Phone #:		  " + "SSN:		  " + "Age:		  " + "Sex:		  " + "Employer Name:		  " + "Employer Address:		  " + "Employer Phone #:		  " + "Years of Employment:		  " + "Title:		  " + "Salary:		  ");
		System.out.println("-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------");
		test.readEmploy();
	}
}



Here is the text file contents
I'm using the code to just make it visually nicer...
13
12
3
2006
JOHN SMITH
132 ELM STREET
605-932-6541
002456721
23
M
GILLETTE RAZOR
123 ANDOVER AVE
617-884-0987
2
RAZOR TESTER
15600
11
10
2004
MARY JONES
32 BAY AVE.
617-543-4421
004234562
25
F
CITY OF MANCHESTER
432 ELM ST.
603-622-1717
3
JANITOR
14500
1
23
1987
TOM KELLEY
11 HIGH ST.
603-892-3317
021345654
43
M
JONES TRUCKING
487 LOWELL ST.
508-989-6543
21
TRUCK DRIVER
25900
1
32
1990
SUSAN DESPARATE
321 KINGS HGY.
603-421-9865
165437689
41
F
KELLY GIRLS
23 WORKING WAY
603-896-6655
18
SECRETARY
15000
2
12
1998
BILLY WORKER
63 RAND AVE		
603-555-2342
432765431
37
M
KELLY GIRLS
23 WORKING WAY
603-896-6655
10
SECRETARY
14500
1
4
1983
TESS TRUEHEART
952 WINSTON BLVD.
617-543-7781
009645439
55
F
DAILY PLANET
4567 NEWSPAPER AVE.
414-506-9876
25
REPORTER
78000
2
15
1963
MAXWELL SMART
1 CHOAS AVE	   
904-326-6741
007007007
70
M
FBI
231 HIDDEN LANE
800-700-7007
45
SECRET AGENT
70070
1
26
2006
FRED SAVAGE
2 WONDER WAY
421-654-8767
001234567
13
M
DAILY PLANET
4567 NEWSPAPER AVE.
414-506-9876
2
NEWSPAPER BOY
2006
12
7
1970
DEAN SMITH
2 NCAA WAY
508-672-0091
020365768
59
M
UNC
DEAN SMITH ARENA
508-672-0029
35
BASKETBALL
98000
2
14
1979
TARK SHARK
1 NCAA WAY
919-825-5438
098763231
61
M
UNLV
1 RATING AVE
919-825-5444
29
BASKETBALL
99999
2
11
1993
CONNIE CHUNG
3 MUMBLE WAY
826-541-2311
006754324
38
F
NBC
STUDIO CENTER
826-541-0098
12
REPORTER
75000
3
11
1996
BARBARA BUSH
1 MOTHERLY WAY
909-946-8765
004532344
57
F
MEALS ON WHEELS
2 POVERTY LANE
909-946-8854
12
VOLUNTEER
0
1
30
2004
GEORGE H. BUSH
1 MOTHERLY WAY
909-946-8765
004532343
59
M
MEALS ON WHEELS
2 POVERTY LANE
909-946-8854
4
PAID FUNDRAISER
30000



I know this is a lot, but I've been racking my brain trying to figure out why this isn't working. I've done similar programs, but with less attributes per person. I really hope someone can help me out with this. Thanks!

Red

Was This Post Helpful? 0
  • +
  • -

#9 klmr3d  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 05-November 08

Re: Reading Data From Text File

Posted 29 January 2009 - 08:37 PM

Hey again,

I am having some trouble sorting still...
			//Sort 2
			public int compareTo(Object o)
			{
				Employment other = (Employment) o;
				return getAge().compareTo(other.getAge);
			}



i get this error...

Compiling 1 source file to C:\Documents and Settings\T-Minus\Desktop\RiethmilAssign2\RiethmilAssign1\RiethmilEmployee\build\classes
C:\Documents and Settings\T-Minus\Desktop\RiethmilAssign2\RiethmilAssign1\RiethmilEmployee\src\Employee\Employment.java:252: cannot find symbol
symbol : variable getAge
location: class Employee.Employment
return getAge().compareTo(other.getAge);
1 error
Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Reading Data From Text File

Posted 29 January 2009 - 09:43 PM

View Postklmr3d, on 29 Jan, 2009 - 07:37 PM, said:

Hey again,

I am having some trouble sorting still...
			//Sort 2
			public int compareTo(Object o)
			{
				Employment other = (Employment) o;
				return getAge().compareTo(other.getAge);
			}



i get this error...

Compiling 1 source file to C:\Documents and Settings\T-Minus\Desktop\RiethmilAssign2\RiethmilAssign1\RiethmilEmployee\build\classes
C:\Documents and Settings\T-Minus\Desktop\RiethmilAssign2\RiethmilAssign1\RiethmilEmployee\src\Employee\Employment.java:252: cannot find symbol
symbol : variable getAge
location: class Employee.Employment
return getAge().compareTo(other.getAge);
1 error


You have () missing... my fault sorry.. typed it too fast

return getAge().compareTo(other.getAge());
Was This Post Helpful? 0
  • +
  • -

#11 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2724
  • View blog
  • Posts: 11,456
  • Joined: 20-September 08

Re: Reading Data From Text File

Posted 30 January 2009 - 03:02 AM

age should really be an int. Suppose you have a 9 year old child savant who joins the firm. Try the sort then. A workaround would be

return Integer.parseInt(age) - Integer.parseInt(other.age);


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1