Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Illegal Start of Expression Error. Any help is appreciated.

  1. #1
    Junior Member
    Join Date
    Dec 2009
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Illegal Start of Expression Error. Any help is appreciated.

    Hello everyone at JavaProgrammingForums!

    I am brand new to Java and any help regarding this seemingly basic problem that I've encountered would be greatly appreciated. Thanks, in advance!

    I'm trying to write an array within the class MyArray which is used in a separate main class.

    I'm getting an "illegal start of expression" error at line 5. The arrow indicating where the error lies is pointing to the closing parentheses of the for loop.

    I haven't encountered this error until now so i'm really not too sure what to do. Here's the code:

    public class MyArray {
      int i;
     
    MyArray () {
      double array[] = new double[MAX];
     
      for (i=0; i<MAX; i=++) {
         system.out.println("what number");
         double num = Double.parseDouble(in.readLine());
    			array[i] = num;
    }
    }
    }

    Again, thanks in advance,

    Sam


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Illegal Start of Expression Error. Any help is appreciated.

    The first issue I can see is that the MAX variable isn't declared or initialized anywhere. I would change your constructor to take an integer and use that to construct the appropriate sized array. Secondly, it's very bad practice to have the loop counter as an object field as you have it there (though, this wouldn't give any compile/running errors). Use a local variable for such a simple task as this. Conversely, you'll probably want the double array to stick around, so I would put that as a field. The last issue is you're trying to use in.nextLine(), but you haven't declared or initialized what in is. I'm guessing you want a Scanner object.

    import Java.util.Scanner;
     
    public class MyArray
    {
         double array[];
     
         MyArray(int max)
         {
              array[] = new double[max];
              Scanner in = new Scanner(System.in);
              for (int i = 0; i < max; i++)
              {
                   System.out.println("What number");
                   double num = Double.parseDouble(in.nextLine());
                   array[i] = num;
              }
         }
    }

  3. The Following User Says Thank You to helloworld922 For This Useful Post:

    SKhoujinian91 (December 8th, 2009)

  4. #3
    Junior Member
    Join Date
    Dec 2009
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Illegal Start of Expression Error. Any help is appreciated.

    Thank you! Your solution did exactly what was needed!

Similar Threads

  1. Help creating expression tree
    By nellaf in forum Java SE APIs
    Replies: 1
    Last Post: December 3rd, 2009, 08:54 AM
  2. Creating an Expression Tree
    By vluong in forum Collections and Generics
    Replies: 0
    Last Post: November 28th, 2009, 11:41 PM
  3. any help is much appreciated
    By Schmitz14 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 8th, 2009, 07:51 PM
  4. nextLine problems, any help appreciated
    By Schmitz14 in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: October 5th, 2009, 01:23 AM
  5. Need a little help. would be greatly appreciated
    By ryan29121 in forum Java Theory & Questions
    Replies: 4
    Last Post: September 27th, 2009, 02:03 PM