7 Replies - 3973 Views - Last Post: 11 April 2011 - 06:17 PM Rate Topic: -----

#1 Themanye  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 28-February 11

!Error illegal start of expression!

Posted 10 April 2011 - 11:59 PM

I am receiving an error message saying illegal start of expression, beginning with "public double getTotal()" I dont know why i am getting this error maybe because i have a method inside a method? could someone please help and explain what is going wrong.

thank you.


public class astronomyNumbers1
{
 
  private double[] numbers;
  
   public astronomyNumbers1(String filename) throws IOException
	{
	 
      final int SIZE = 12;
		double[] numbers = new int[SIZE];
		int index = 0;
      
		
		// Create the necessary objects for file input.
      File file = new File(filename);
      Scanner inputFile = new Scanner(file);
		
		//read the file contents in to the array
		while (inputfile.hasNext() && index < numbers.length)
		{
		    numbers[index] = inputfile.nextInt();
		    index++;
		}
		
		
	public double getTotal()
   {
      double total = 0.0;   // Accumulator

      // Add up all the values in the array.
      for (double value : numbers)
         total += value;

      // Return the total.
      return total;
   }

   /**
    * The getAverage method returns the average of the 
    * elements in the array. 
    */

   public double getAverage()
   {
      return getTotal() / numbers.length;
   }

   /**
    * The getHighest method returns the highest value  
    * stored in the array.
    */

   public double getHighest()
   {
      // Store the first value in the array in
      // the variable highest.
      double highest = numbers[0];

      // Search the array for the highest value.
      for (int index = 1; index < numbers.length; index++)
      {
         if (numbers[index] > highest)
            highest = numbers[index];
      }

      // Return the highest value.
      return highest;
   }

   /**
    * The getLowest method returns the lowest value    
    * stored in the array.
    */

   public double getLowest()
   {
      // Store the first value in the sales array in
      // the variable lowest.
      double lowest = numbers[0];

      // Search the array for the lowest value.
      for (int index = 1; index < numbers.length; index++)
      {
         if (sales[index] < lowest)
            lowest = numbers[index];
      }

      // Return the lowest value.
      return lowest;
	}
		inputFile.close();
}
		}



Is This A Good Question/Topic? 0
  • +

Replies To: !Error illegal start of expression!

#2 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,021
  • Joined: 15-July 08

Re: !Error illegal start of expression!

Posted 11 April 2011 - 12:16 AM

You cannot have methods inside of methods...it just does not work. If you are having scoping errors, try using instance variables.
Was This Post Helpful? 0
  • +
  • -

#3 Themanye  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 28-February 11

Re: !Error illegal start of expression!

Posted 11 April 2011 - 12:18 AM

where is the method that i have inside the other method? and how do i fix it?
Was This Post Helpful? 0
  • +
  • -

#4 MrLuke187  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 81
  • View blog
  • Posts: 237
  • Joined: 09-July 10

Re: !Error illegal start of expression!

Posted 11 April 2011 - 12:20 AM

In your code, all your methods are in your constructor. This won't work. Put them in the class-Body, not in the constructor.

Greetings: Luke
Was This Post Helpful? 1
  • +
  • -

#5 softwareEngineer()  Icon User is offline

  • D.I.C Head

Reputation: -28
  • View blog
  • Posts: 247
  • Joined: 08-April 11

Re: !Error illegal start of expression!

Posted 11 April 2011 - 06:00 AM

What I see is a problem with not having a "for" loop at line 31; you can't just declare a for loop and leave it like that.

Is there something else you meant to do there?
Was This Post Helpful? 0
  • +
  • -

#6 sakshamkum  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 19
  • View blog
  • Posts: 232
  • Joined: 09-June 09

Re: !Error illegal start of expression!

Posted 11 April 2011 - 08:20 AM

the for doesnot require block braces if there is only a single statement to be iterated
Was This Post Helpful? 1
  • +
  • -

#7 MrLuke187  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 81
  • View blog
  • Posts: 237
  • Joined: 09-July 10

Re: !Error illegal start of expression!

Posted 11 April 2011 - 09:09 AM

View Postsakshamkum, on 11 April 2011 - 04:20 PM, said:

the for doesnot require block braces if there is only a single statement to be iterated

Also, this is an advanced for-loop, the "for-each" in Java: Link

Greetings: Luke
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

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

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

Re: !Error illegal start of expression!

Posted 11 April 2011 - 06:17 PM

Actually, you do not have a method inside a method
but you have code starting by

while (inputfile.hasNext() && index < numbers.length)

inside your instance variables declaration (not in a method or constructor)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1