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

Thread: Call method(s) within the same class

  1. #1
    Member
    Join Date
    Aug 2011
    Posts
    55
    Thanks
    5
    Thanked 3 Times in 3 Posts

    Default Call method(s) within the same class

    I am new to programming and Java. I am trying to make 3 methods. One gets the largest number, one gets the middle, and the last gets the smallest. I do not think I created them correctly. I am getting a compiler error in Jgrasp saying illegal start to expression for
    each method. Then how do I call them? Here is my code:
    import java.util.*;
     
    public class Ch4_PrExercise2
    {
    	public static void main(String [] args)
    	{
    		Scanner console = new Scanner(System.in);
     
    		int num1, num2, num3;
     
    		System.out.println("Please input 3 numbers separated by spaces: ");
    		System.out.println();
     
    		num1 = console.nextInt();
    		num2 = console.nextInt();
    		num3 = console.nextInt();
     
     
     
    			public int largest()
    			{
    				if(num1 > num2 && num1 > num3)			
    					return num1;			
    				else if(num2 > num1 && num2 > num3)			
    					return num2;				
    				else if(num3 > num1 && num3 > num2)
    					return num3;
    			}						
     
     
     
    			public int smallest()
    			{
    				if(num1 < num2 && num1 < num3)
    					return num1;
    				else if(num2 < num1 && num2 < num3)
    					return num2;
    				else if(num3 < num1 && num3 < num2)
    					return num3;
    		   }
     
    			 public int middle()
    			 {
     
    				if((num1 < num2 && num1 >num3) || (num1 > num2 && num1 < num3))
    					return num1;
    				else if((num2 > num1 && num2 < num3) || (num2 < num1 && num2 >num3))
    					return num2;
    				else if((num3 > num2 && num3 < num1) || (num3 < num2 && num3 > num1))
    					return num3;
    			 }
     
     
    		System.out.println("Your numbers in nondescending order are: " + " " + smallest() + " " + middle() + " " largest());		
     
     
    	}
    }
    thanks in advance


  2. #2
    Junior Member
    Join Date
    Sep 2011
    Posts
    13
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Call method(s) within the same class

    try this:


    import java.util.*;
     
    	public class TestIf
    	{
    		public static void main(String [] args)
    		{
    			Scanner console = new Scanner(System.in);
     
    			int num1, num2, num3, smallest, largest, middle;
     
    			System.out.println("Please input 3 numbers separated by spaces: ");
    			System.out.println();
     
    			num1 = console.nextInt();
    			num2 = console.nextInt();
    			num3 = console.nextInt();
     
    			smallest= smallest(num1, num2, num3);
    			largest= largest(num1, num2, num3);
    			middle= middle(num1, num2, num3);
     
     
    			System.out.println("Your numbers in nondescending order are: " + " " + smallest + " " + middle + " " + largest);		
     
     
    		}
     
     
     
    				public static int largest(int num1, int num2, int num3)
    				{
    					if(num1 > num2 && num1 > num3)			
    						return  num1;			
    					else if(num2 > num1 && num2 > num3)			
    						return num2;				
    					else if(num3 > num1 && num3 > num2)
    						return num3;
    					return 0;
    				}						
     
     
     
    				public static int smallest(int num1, int num2, int num3)
    				{
    					if(num1 < num2 && num1 < num3)
    						return num1;
    					else if(num2 < num1 && num2 < num3)
    						return num2;
    					else if(num3 < num1 && num3 < num2)
    						return num3;
    					return 0;
    			   }
     
    				 public static int middle(int num1, int num2, int num3)
    				 {
     
    					if((num1 < num2 && num1 >num3) || (num1 > num2 && num1 < num3))
    						return num1;
    					else if((num2 > num1 && num2 < num3) || (num2 < num1 && num2 >num3))
    						return num2;
    					else if((num3 > num2 && num3 < num1) || (num3 < num2 && num3 > num1))
    						return num3;
    					return 0;
    				 }
     
     
     
    	}
    Last edited by helloworld922; September 19th, 2011 at 12:15 AM.

  3. #3
    Junior Member
    Join Date
    Sep 2011
    Posts
    13
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Call method(s) within the same class

    Let me know if it works

  4. #4
    Member
    Join Date
    Aug 2011
    Posts
    55
    Thanks
    5
    Thanked 3 Times in 3 Posts

    Default Re: Call method(s) within the same class

    actually it didn't
    here's the compiler errors I got:


    ----jGRASP exec: javac -g Ch4_PrExercise2.java

    Ch4_PrExercise2.java:27: error: illegal start of expression
    public static int largest(int num1, int num2, int num3)
    ^
    Ch4_PrExercise2.java:27: error: illegal start of expression
    public static int largest(int num1, int num2, int num3)
    ^
    Ch4_PrExercise2.java:27: error: ';' expected
    public static int largest(int num1, int num2, int num3)
    ^
    Ch4_PrExercise2.java:27: error: '.class' expected
    public static int largest(int num1, int num2, int num3)
    ^
    Ch4_PrExercise2.java:27: error: ';' expected
    public static int largest(int num1, int num2, int num3)
    ^
    Ch4_PrExercise2.java:27: error: <identifier> expected
    public static int largest(int num1, int num2, int num3)
    ^
    Ch4_PrExercise2.java:27: error: not a statement
    public static int largest(int num1, int num2, int num3)
    ^
    Ch4_PrExercise2.java:27: error: ';' expected
    public static int largest(int num1, int num2, int num3)
    ^
    Ch4_PrExercise2.java:40: error: illegal start of expression
    public static int smallest(int num1, int num2, int num3)
    ^
    Ch4_PrExercise2.java:40: error: illegal start of expression
    public static int smallest(int num1, int num2, int num3)
    ^
    Ch4_PrExercise2.java:40: error: ';' expected
    public static int smallest(int num1, int num2, int num3)
    ^
    Ch4_PrExercise2.java:40: error: '.class' expected
    public static int smallest(int num1, int num2, int num3)
    ^
    Ch4_PrExercise2.java:40: error: ';' expected
    public static int smallest(int num1, int num2, int num3)
    ^
    Ch4_PrExercise2.java:40: error: <identifier> expected
    public static int smallest(int num1, int num2, int num3)
    ^
    Ch4_PrExercise2.java:40: error: not a statement
    public static int smallest(int num1, int num2, int num3)
    ^
    Ch4_PrExercise2.java:40: error: ';' expected
    public static int smallest(int num1, int num2, int num3)
    ^
    Ch4_PrExercise2.java:51: error: not a statement
    publicstatic int middle(int num1, int num2, int num3)
    ^
    Ch4_PrExercise2.java:51: error: ';' expected
    publicstatic int middle(int num1, int num2, int num3)
    ^
    Ch4_PrExercise2.java:51: error: ';' expected
    publicstatic int middle(int num1, int num2, int num3)
    ^
    Ch4_PrExercise2.java:51: error: <identifier> expected
    publicstatic int middle(int num1, int num2, int num3)
    ^
    Ch4_PrExercise2.java:51: error: not a statement
    publicstatic int middle(int num1, int num2, int num3)
    ^
    Ch4_PrExercise2.java:51: error: ';' expected
    publicstatic int middle(int num1, int num2, int num3)
    ^
    Ch4_PrExercise2.java:51: error: ';' expected
    publicstatic int middle(int num1, int num2, int num3)
    ^
    Ch4_PrExercise2.java:64: error: ')' expected
    System.out.println("Your numbers in nondescending order are: " + " " + smallest + " " + middle + " " largest);
    ^
    Ch4_PrExercise2.java:64: error: illegal start of expression
    System.out.println("Your numbers in nondescending order are: " + " " + smallest + " " + middle + " " largest);
    ^
    25 errors

    ----jGRASP wedge2: exit code for process is 1.
    ----jGRASP: operation complete.

  5. #5
    Junior Member
    Join Date
    Sep 2011
    Posts
    13
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Call method(s) within the same class

    Did you make sure to change the class name back? I changed it so it would work in my project

  6. The Following User Says Thank You to Bertorox For This Useful Post:

    mwr76 (September 19th, 2011)

  7. #6
    Member
    Join Date
    Aug 2011
    Posts
    55
    Thanks
    5
    Thanked 3 Times in 3 Posts

    Default Re: Call method(s) within the same class

    yes I did change it back

  8. #7
    Member
    Join Date
    Aug 2011
    Posts
    55
    Thanks
    5
    Thanked 3 Times in 3 Posts

    Default Re: Call method(s) within the same class

    I am an idiot, it works fine.
    2 typos cause all those errors.
    thank you!

  9. #8
    Junior Member
    Join Date
    Sep 2011
    Posts
    13
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Call method(s) within the same class

    Haha don't worry about it. Happens to everyone. Glad I could help

  10. #9
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Call method(s) within the same class

    Bertorox, it's good that you helped him, but next time better not feed spoon. You must stick with the forum rules.
    Thanks

Similar Threads

  1. Beginner method call problem
    By Lasda in forum Java Theory & Questions
    Replies: 2
    Last Post: May 10th, 2011, 03:31 PM
  2. How do I call a class in a main class
    By Leprechaun_hunter in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 26th, 2011, 03:11 AM
  3. call super method outside of 'this'
    By questionmark in forum Object Oriented Programming
    Replies: 1
    Last Post: January 29th, 2011, 11:29 AM
  4. Can i call init() method in destroy method.?
    By muralidhar in forum Java Servlet
    Replies: 1
    Last Post: October 22nd, 2010, 11:18 AM
  5. problem with data access when a class call another class
    By ea09530 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 4th, 2010, 05:20 PM