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 8 of 8

Thread: Illegal start

  1. #1
    Junior Member hing09's Avatar
    Join Date
    Mar 2010
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Illegal start

    Hi all,

    Does anyone know why i keep getting illegal start of type when i type System.out.println("The Largest Number is " + maxValue(numbers));
    when i put // infront of it as a comment it compiles fine although i do not get the output.
    import java.util.Scanner;
    import java.io.*;
    import java.io.IOException;
    import java.util.*;
    import java.util.Arrays;
     
     
    public class arrayNum
    {
     
           public static void main (String args[]) throws IOException{
     
     
                    Scanner input = new Scanner(System.in);
                    int array[] = new int[5];
                    int count=0;
                    int numbers;
     
     
     
     				System.out.println("Please enter 5 numbers");
     
     
                    {
                    while (count < 5)
                    {
    				//the input you get must be store in the array
    				array[count] = input.nextInt();
    						count++;
     
     
                    }
    				}
    																}
     
    				System.out.println("The Largest Number is " + maxValue(numbers));
     
                    	public static int getmaxValue(int[] numbers)
                	{
    						int maxValue = numbers[1];
    						for(int i=1;i<numbers.length;i++)
    						{
    							if(numbers[i] < maxValue)
    							{
    								maxValue = numbers[i];
    						}
    							}
    						return maxValue;
    				}
     
     
     
     
     
     
    			//	int max = array[5];
    		//		for(int i = 1; i < 5; i++)
    		//		 {
    		//		 if(array[i] > max)
    		//		 {
     		//		 max = array[i];
    		//		 }
    		//		 }
    				// print the value instead of printing
     
     
    			//	return max;
    }

    Your help is greatly appreciated!
    Last edited by helloworld922; March 24th, 2010 at 01:48 AM.


  2. #2
    Member Faz's Avatar
    Join Date
    Mar 2010
    Posts
    97
    Thanks
    5
    Thanked 14 Times in 14 Posts

    Default Re: Illegal start

    Just a quick scan through it. As far as I can tell it isn't inside any method so try moving it before the } above it. Also you don't need the { before the while. Let us know how that works out.

  3. #3
    Junior Member hing09's Avatar
    Join Date
    Mar 2010
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Illegal start

    Thanks,

    I have removed the } in the while loop although it now compiles but does not give me the output
    The out put should be showing the largest number that the user has entered 5 times.

  4. #4
    Junior Member hing09's Avatar
    Join Date
    Mar 2010
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Illegal start

    I just realised you need the { brackets for the while loop otherwise it will keep looping.

  5. #5
    Member Faz's Avatar
    Join Date
    Mar 2010
    Posts
    97
    Thanks
    5
    Thanked 14 Times in 14 Posts

    Default Re: Illegal start

    So it is working now or not?

    When I said to take out the { I meant the one before the while loop because there's no point to it and it isn't closed either.
     { <---This one
                    while (count < 5)
                    {

  6. #6
    Junior Member
    Join Date
    Mar 2010
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thumbs up Re: Illegal start

    package javaapplication1;
     
     
    import java.util.Scanner;
    import java.io.*;
    import java.io.IOException;
    import java.util.*;
    import java.util.Arrays;
     
     
     
    class Main
    {
     
     
     
           public static void main (String args[]) throws IOException
           {
     
     
                    Scanner input = new Scanner(System.in);
                    int array[] = new int[5];
                    int count=0;
                    int numbers;
     
     
     
     				System.out.println("Please enter 5 numbers");
     
     
                    {
                    while (count < 5)
                    {
    				//the input you get must be store in the array
    				array[count] = input.nextInt();
    						count++;
     
     
                    }
                    System.out.println("The Largest Number is " + getmaxValue(array));
    				}
    																}
     
     
     
                    	public static int getmaxValue(int[] numbers)
                	{
    						int maxValue = numbers[0];
    						for(int i=1;i<numbers.length;i++)
    						{
    							if(numbers[i] > maxValue)
    							{
    								maxValue = numbers[i];
    						}
    							}
    						return maxValue;
    				}
     
     
     
     
     
     
    			//	int max = array[5];
    		//		for(int i = 1; i < 5; i++)
    		//		 {
    		//		 if(array[i] > max)
    		//		 {
     		//		 max = array[i];
    		//		 }
    		//		 }
    				// print the value instead of printing
     
     
    			//	return max;
    }

    You should have to import the pakage of javaapplication1 and also your statement was written outside the main().Also, your getmaxvalue() was not for maximum but for minimum and you took the first index of array as the max value as you should have to take the zero index in the beginning. Well, its ok now. Enjoy.
    Last edited by helloworld922; March 25th, 2010 at 06:41 PM. Reason: please use [code] tags!

  7. #7
    Junior Member
    Join Date
    Mar 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Illegal start

    First, you don't need to have parenthesis before the while loop. So remove that parenthesis and the matching closing parenthesis after closing the while loop.

    Secondly, I think u should modify your array instantiation to something like:

    int[] array; // Declare the array

    array = new int[5]; // Instantiate the array

    Refer to Arrays (The Java™ Tutorials > Learning the Java Language > Language Basics) for details.

    I hope this helps.

  8. #8
    Junior Member hing09's Avatar
    Join Date
    Mar 2010
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Illegal start

    Thanks alot, helped out heaps!

Similar Threads

  1. Where to start?
    By tonykasdorf in forum Java Theory & Questions
    Replies: 3
    Last Post: March 4th, 2010, 11:52 PM
  2. "illegal modifier for parameter sum; only final is permitted
    By etidd in forum Java Theory & Questions
    Replies: 3
    Last Post: February 11th, 2010, 07:51 AM
  3. Illegal Start of Expression Error. Any help is appreciated.
    By SKhoujinian91 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 8th, 2009, 12:57 AM
  4. How should i write and compile java with Ubuntu?
    By Talk Binary in forum Java IDEs
    Replies: 19
    Last Post: May 7th, 2009, 05:29 AM
  5. Replies: 1
    Last Post: May 7th, 2009, 04:31 AM