6 Replies - 378 Views - Last Post: 02 October 2012 - 09:13 PM Rate Topic: -----

#1 invidian  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 02-October 12

non-static method toString() cannot be referenced from a static contex

Posted 02 October 2012 - 08:45 PM

   import java.text.DecimalFormat;
   public class StudentInvoice
   {
      private String studentName, studentNumber;
      private double tuitionFees, scholarships;
   	
      public StudentInvoice(String name, String number,
      							double tuition, double scholar)
      {
         studentName = name;
         studentNumber = number;
         tuitionFees = tuition;
         scholarships = scholar;
      }
      public String getName()
      {
         return studentName;
      }
      public boolean setName(String name)
      {
         if (name == null)
         {	
            return false;
         }
         else
         {
            return true;
         }
      }
      public String getStudentNumber()
      {
         return studentNumber;
      }
      public boolean setStudentNumber(String number)
      {
         if (number == null)
         {
            return false;
         }
         else 
         {
            return true;
         }
      }
      public double getTuitionFees()
      {
         return tuitionFees;
      }
      public boolean setTuitionFees(double tuition)
      {
         if (tuition >= 0)
         {
            return true;
         }
         else 
         {
            return false;
         }
      }
      public double getScholarships()
      {
         return scholarships;
      }
      public boolean setScholarships(double scholar)
      {
         if (scholar >= 0)
         {
            return true;
         }
         else
         {
            return false;
         }
      }
      public double adjustTuitionFees(double tuition)
      {
         if(tuition + tuitionFees >= 0)
         {
            tuitionFees += tuition;
         }
         return tuitionFees;
      }
      public double adjustScholarships(double scholar)
      {
         if (scholar + scholarships >= 0)
         {
            scholarships += scholar;
         }
         return scholarships;
      }
      public String toString()
      {
         DecimalFormat d = new DecimalFormat("$#,##0.00");
         String output = "Name: " + studentName + "\n"
            + "ID Number: " + studentNumber + "\n"
            + "Tuition & Fees: " + d.format(tuitionFees) + "\n"
            + " Scholarships: " + d.format(scholarships) + "\n";
      	
         if (tuitionFees >= scholarships)
         {
            double difference = tuitionFees - scholarships;
            return output + "You owe: " + d.format(difference);
         }
         else
         {
            double difference = scholarships - tuitionFees;
            return output + "Please pick up your check for: " + d.format(difference);
         }
      }
   }



and the problem lies in
[code]
import java.util.ArrayList;

public class StudentInvoiceList
{
ArrayList<StudentInvoice> aList = new ArrayList<StudentInvoice>();
public StudentInvoiceList (ArrayList<StudentInvoice> stdArray)
{
aList = stdArray;
}
public double avgTuitionFees()
{
int i = 0;
double sum = 0;
while (i < aList.size())
{
sum += aList.get(i).getTuitionFees();
i++;
}
return sum/aList.size();
}
public double percentScholarships()
{
int count = 0;
int k = 0;

while(k < aList.size())
{
double scholar = aList.get(k).getScholarships();
if(scholar > 0)
{
count++;
}
}
return count/aList.size();
}
public double avgScholarships()
{
int t = 0;
double avg = 0;
while (t < aList.size())
{
avg += aList.get(t).getScholarships();
t++;
}
return avg/aList.size();
}
public String tostring()
{
String output;
output = StudentInvoice.toString();
return output;
}
}
[\code]

Is This A Good Question/Topic? 0
  • +

Replies To: non-static method toString() cannot be referenced from a static contex

#2 lilVaratep  Icon User is offline

  • D.I.C Head

Reputation: 20
  • View blog
  • Posts: 207
  • Joined: 09-October 11

Re: non-static method toString() cannot be referenced from a static contex

Posted 02 October 2012 - 08:48 PM

Which lines is the compiler giving errors about? Hard to see without that second set of code bracketed.
Was This Post Helpful? 0
  • +
  • -

#3 invidian  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 02-October 12

Re: non-static method toString() cannot be referenced from a static contex

Posted 02 October 2012 - 08:59 PM

[code]
public String tostring()
{
String output;
output = StudentInvoice.toString();
return output;
}
[/I do not know how to call a toString() from one class to another.
Was This Post Helpful? 0
  • +
  • -

#4 Ap0C552  Icon User is offline

  • D.I.C Regular

Reputation: -7
  • View blog
  • Posts: 314
  • Joined: 08-December 10

Re: non-static method toString() cannot be referenced from a static contex

Posted 02 October 2012 - 09:03 PM

I can't remember what the default toString method returns but maybe override with your own toString method
Was This Post Helpful? 0
  • +
  • -

#5 invidian  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 02-October 12

Re: non-static method toString() cannot be referenced from a static contex

Posted 02 October 2012 - 09:04 PM

public String tostring()
{
String output;
output = StudentInvoice.toString();
return output;
}



does it have to be a super String thing?
Was This Post Helpful? 0
  • +
  • -

#6 guido-granobles  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 171
  • View blog
  • Posts: 617
  • Joined: 02-December 09

Re: non-static method toString() cannot be referenced from a static contex

Posted 02 October 2012 - 09:07 PM

The tostring() method should be toString(). Beside that you are using the toString method of the StudentInvoice class like it were a static method. You should use that method with an instance of the
StudentInvoice class, something like:


//some where in StudentInvoiceList class.
StudentInvoice  invoice = new StudentInvoice();
//other stuff with invoice

public String toString(){
 return invoice.toString();
}


Was This Post Helpful? 0
  • +
  • -

#7 invidian  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 02-October 12

Re: non-static method toString() cannot be referenced from a static contex

Posted 02 October 2012 - 09:13 PM

I just got it compile with
      public String tostring()
      {
      	StudentInvoice students = new StudentInvoice("hey", "Hey", 1, 2);
      	String output;
      	output = students.toString();
         return output;
      }


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1