I keep getting a 'illegal start of an expression' and ')' expected on a loop line for a program that counts and compare ten numbers and displays the largest. I have checked for "{}" and "()" parity, and also for ";" at the end of my statements; but the following loop line keeps giving me the error:

while( [] counter <= 10);

Here's the code:

import java.util.Scanner;

class Largest//finds largest value
{
    //finds the largest integer
    public void findLargest()
    {
    //create Scanner to input   
    Scanner input = new Scanner(System.in); 
        {
        //set variables
        int [] counter = new int [10]; //number of integers
        int x; //integer entered when prompt
        int largest; //largest integer

            //initialization phase
            largest = 1;//initilize loop counter

            //processing phase
            while([] counter <= 10);//loop 10 times
            {           
            System.out.print("Enter next number");//prompt for next integer

            for (x=1; x<=numbers.compareto; x++) 
            numbers[x]=scan.nextInt ();//input next integer
            }

            if (x == 1) 
            largest = numbers[1];

            if (numbers[x] > largestNumber) 
            largest = numbers[x];

        }//loop ends

        System.out.printf("The largest number is %d\n", Largest);
    }//end method findlargest
}//end classLargest 

//Largest.java
//count program with largest displayed

public class LargestTest
{
    public static void main(String[] args )
    {
        LargestTest myLargestTest = new LargestTest();
        myLargestTest.displaylargestTest();
    }//end main
}//end class LargestTest

Recommended Answers

All 16 Replies

I'm not really familiar with java, but in c++ you need a ";" at the end of a class:

class foo
{
    ...
};<---

Could it be that?

commented: Do not show c++ knowledge. -2

yeah, appreciate that, but I don't think that is the reason for the error. ; are usually for ending a statment in Java.

The only other thing I can see is that you have a //loop ends comment one "}" after the while loop actually ends. That might lead to something later in the file. If it isn't that, then could you post a line number where the compiler comes up with the error (relative to the code you posted)? Also, I'm not sure why you have a "{" after "Scanner input = new Scanner(System.in);". It only appears to be complicating your code. But again, let me know if this is supposed to be this way, as I haven't brushed up on my java in a while.

sure, I am getting the error on this line code:

while([] counter <= 10);

this line is the loop code line that is to loop 10 times.

class Largest//finds largest value
{
    public void findLargest(){
             Scanner input = new Scanner(System.in); 
             int [] counter = new int [10]; 
             int x;
             int largest; 
             largest = 0;

while(largest <10) //loop 10 times
 { 
  System.out.print("Enter next number");//prompt for next integer
  ....
  largest++;
  }
 ....

Source code must be surrounded with code tags.

sure, I am getting the error on this line code:
while([] counter <= 10);

What are you trying to achieve with the [] ?

Don't think you can do this:

....
//set variables
int [] counter = new int [10]; //number of integers

//processing phase
while([] counter <= 10);//loop 10 times
{ 
.....

I've never seen anyone try to write a statement like that. Don't think that is valid syntax:

while([] counter <= 10);

Also, you are trying to compare an array to an integer. You can't compare an entire array to a single integer. You could however compare an array element to an integer:
while (counter <=10)

Probably not what you are trying to achieve. You probably just need to do the following:

int counter; //number of integers
....
while (counter <= 10)
{
...

counter = counter + 1; //increment counter
}

I am all flustered with this program. I thought that I had a good grasp on the concept, but evidently I don't. I have written the Test application to this, and have cleared up the bugs in that, but my object still seems to conjure more bugs. I have about 8 errors in the code and can't debug. A couple of them is in the "for" statement:

for(int x=1; x<=integers.compareto; x++);
integers = scan.nextInt();

I am getting a cannot find variable symbol integer and scan.

ohhh well, your counter is array integer type so your condition is must compare with some value.....

//processing phase
counter[2] = 5; // example
while( counter[2] <= 10);//loop 10 times
{

try this... however i dun know what you are trying to achieve.

I think I got it. I'm pretty sure that the ";" after while ([] counter <= 10) isn't supposed to be there.

I think I got it. I'm pretty sure that the ";" after while ([] counter <= 10) isn't supposed to be there.

And yea of coz u cont terminate the line after condition it is whole block of statement until you close the brace.

I want to compare the numbers in the array, so the code can display the largest number in the display.

You probably want to do something like the following. Remember to keep track of your variable names and data types. Also, don't name any of your variable names the same as the class name. This version does not prevent duplicates or prevent strings from being entered--error handling is a more "advanced" topic.

Largest.java

import java.util.Scanner;

class Largest //finds largest value
{
    
  //constructor
  public Largest(){

  } //end constructor
  
  //finds the largest integer
  public void findLargest()
  {
  //create Scanner to input
  Scanner input = new Scanner(System.in);

  //set variables
  int [] numbers = new int [10]; //array of integers
  int x; //counter
  int largestNumber; //largest integer

  //initialization phase
  largestNumber = 0;//initilize

  //processing phase
    
    System.out.println("You will be prompted for 10 numbers.");
    
    for (x=0; x<10; x++)
    {
      //prompt for integer
      System.out.print("Enter a number[" + x + "]:");
      numbers[x]=input.nextInt ();//input next integer
    
      //initialize largestNumber to first number entered by user
      if (x == 1)
        largestNumber = numbers[0];

      //if any other numbers are larger than the 1st one entered
      //replace with the new largest number
      if (numbers[x] > largestNumber)
        largestNumber = numbers[x];

    }//for loop ends

    //print out largest number
    System.out.printf("The largest number is %d \n", largestNumber);
  }//end method findlargest


} //end class Largest

LargestTest.java

public class LargestTest
{
  public static void main(String[] args )
  {
    Largest myLargest = new Largest();
    myLargest.findLargest();
  //myLargestTest.displaylargestTest();
  }//end main
}//end class LargestTest

Prevent user from entering the same number twice (duplicates).

Largest.java

import java.util.Scanner;

class Largest//finds largest value
{
    
  //constructor
  public Largest(){

  } //end constructor
  
  //finds the largest integer
  public void findLargest()
  {
  //create Scanner to input
  Scanner input = new Scanner(System.in);

  //set variables
  int [] numbers = new int [10]; //number of integers
  int x; //counter
  int largestNumber; //largest integer
  int userData; // integer entered by user
  boolean numberExists = false;

  //initialization phase
  largestNumber = 0;//initilize

  //processing phase
    
    System.out.println("You will be prompted for 10 numbers.");
    //initialize counter "x"
    x=0;
    while (x<10)
    {

      System.out.print("Enter a number[" + x + "]:");//prompt for integer
      userData=input.nextInt ();//input next integer
      
      for (int y=0; y<=x;y++)
      {
          //see if the integer already exists in the array
          //we only need to check up to the last number entered
          //by the user so we say "y<=x"

          //don't need to check if this is the first number
          if (x !=0)
          {
            if (numbers[y] == userData)
            {
              numberExists = true;
              
              System.out.println("This number was already entered: " + userData);
              System.out.println("Please choose a different number.");
            }
          }
      }

      if (numberExists == false)
      {
        //this is a unique number, so store it in the array
        numbers[x] = userData;

        //initialize largestNumber to first number entered by user
        if (x == 0)
        {

          largestNumber = numbers[0];
        }

        //if any other numbers are larger than the 1st one entered
        //replace with the new largest number
        if (numbers[x] > largestNumber)
        {
          largestNumber = numbers[x];
        }

        x = x + 1;
      }
      else
      {
          //reset numberExists to false
          numberExists = false;

         
          
          //do NOT increment the counter
      } //for loop ends

    }//while loop ends

    //print out largest number
    System.out.printf("The largest number is %d \n", largestNumber);
  }//end method findlargest


} //end class Largest

Thanks cgeier,
That worked except for the instance, so I incorporated the instance that I had to call your object and it worked like a charm. I kept getting a 'class' expected on line 1 for your test, and it probably was one of my application that I hadn't closed that was interrupting the path. Only guessing, no where close to being a Java jockey. Thanks

This one handles duplicates and only accepts integers.

import java.util.Scanner;

class Largest//finds largest value
{
    
  //constructor
  public Largest(){

  } //end constructor
  
  //finds the largest integer
  public void findLargest()
  {
  //create Scanner to input
  Scanner input = new Scanner(System.in);

  //set variables
  int [] numbers = new int [10]; //number of integers
  int x; //counter
  int largestNumber; //largest integer
  int userData = 0; // integer entered by user
  boolean numberExists = false;

  //initialization phase
  largestNumber = 0;//initilize

  //processing phase
    
    System.out.println("You will be prompted for 10 numbers.");
    //initialize counter "x"
    x=0;
    while (x<10)
    {


      System.out.print("Enter a number[" + x + "]: ");//prompt for integer
      //check to see if user input is an integer
      if (input.hasNextInt())
      {
        userData=input.nextInt ();//input next integer
      }
      else
      {
          //don't do anything with the invalid data; read it and trash it
          input.next();
          System.out.print("Invalid data entered. Enter only integers ");
          System.out.println("(-999,999,999 < x < 999,999,999).");

          //go to next iteration of loop; skipping everything below
          continue;
      }
      
      for (int y=0; y<=x;y++)
      {
          //see if the integer already exists in the array
          //we only need to check up to the last number entered
          //by the user so we say "y<=x"

          //don't need to check if this is the first number
          if (x !=0)
          {
            if (numbers[y] == userData)
            {
              numberExists = true;
              System.out.print("The number '" + userData + "'");
              System.out.println (" was already entered (index: " + y + ")");
              System.out.println("Please choose a different number.");
            }
          }
      }

      if (numberExists == false)
      {
        //this is a unique number, so store it in the array
        numbers[x] = userData;

        //initialize largestNumber to first number entered by user
        if (x == 0)
        {

          largestNumber = numbers[0];
        }

        //if any other numbers are larger than the 1st one entered
        //replace with the new largest number
        if (numbers[x] > largestNumber)
        {
          largestNumber = numbers[x];
        }

        x = x + 1;
      }
      else
      {
          //reset numberExists to false
          numberExists = false;

         
          
          //do NOT increment the counter
      } //for loop ends
      


    }//while loop ends

    //print out largest number
    System.out.printf("The largest number is %d \n", largestNumber);
  }//end method findlargest


} //end class Largest
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.