Results 1 to 11 of 11
  1. #1
    exeye0h's Avatar
    exeye0h is offline Member
    Join Date
    Jan 2012
    Location
    Chicago, IL, USA
    Posts
    40
    Rep Power
    0

    Default can you begin a method with simply "void"? (and possible more questions)

    Java Code:
    public class Sample{
    	void method( int number)
    	{
    		int x = number;
    		int count = 0;
    		while (x > 0) {
    			x = x/10;
    			count++;
    		}
    		for ( int i = 0; i < count/2; i++){
    			number = number / 10;
    		}
    		System.out.println(number%10);
    	}
    }
    I am not looking to change this code because this is part of an assignment. I am looking for an explanation on this because I seem to get "Exception in thread "main" java.lang.NoSuchMethodError: main" when I try to run it. Complies just fine but it will not run and I am assuming that is because of "int number" as my parameter but please clarify, if you can!

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: can you begin a method with simply "void"? (and possible more questions)

    The code is fine, but this cannot run on its own since every Java program needs a main method to have it run (with a few select exceptions). To solve this, create a class called TestSample, give it a main method, create a Sample object in the main method, and call your "method(...)" on the object.

  3. #3
    exeye0h's Avatar
    exeye0h is offline Member
    Join Date
    Jan 2012
    Location
    Chicago, IL, USA
    Posts
    40
    Rep Power
    0

    Default Re: can you begin a method with simply "void"? (and possible more questions)

    Sorry but do you think you can walk me through this one?
    I've got the most basic of it:
    Java Code:
    public class TestSample {
    	public static void main(String [] args) {
    But say that I want to simply see what would be the value of a specific number (i.e. 1324)? That is all I want to input.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: can you begin a method with simply "void"? (and possible more questions)

    OK, first create a Sample object by declaring a Sample variable in main and having it refer to a new Sample object:

    Java Code:
    public static void main(String[] args) {
       Sample sample = new Sample();
    }
    the "Sample sample" statement declares the variable, and the "new Sample()" statement creates the new Sample object and has the sample variable hold a reference to that object.

    Next see if you can call your method on this variable. Give it a try and post what you've got.

  5. #5
    exeye0h's Avatar
    exeye0h is offline Member
    Join Date
    Jan 2012
    Location
    Chicago, IL, USA
    Posts
    40
    Rep Power
    0

    Default Re: can you begin a method with simply "void"? (and possible more questions)

    This is what I did
    Java Code:
    public class TestSample {
    	public static void main(String [] args) {
    		Sample sample = new Sample();
    		int sample = 1324;
    		}
    }
    To get my failure of
    "TestSample.java:4: sample is already defined in main(java.lang.String[])
    int sample = 1324;
    ^
    1 error
    " when trying to compile.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: can you begin a method with simply "void"? (and possible more questions)

    sample is a Sample object and you can't try to set it equal to an int. Better to call a method off of it, and you would do this adding a dot and then the method name. For example:

    Java Code:
    String foo = "bar   ";
    bar = bar.trim();
    Your Sample class has only one method that can be called (other than the ones available to all Objects), and that is method(...). So go ahead and do this. Call method off of your sample variable.

  7. #7
    exeye0h's Avatar
    exeye0h is offline Member
    Join Date
    Jan 2012
    Location
    Chicago, IL, USA
    Posts
    40
    Rep Power
    0

    Default Re: can you begin a method with simply "void"? (and possible more questions)

    So something like this?
    Java Code:
    public class TestSample {
    	public static void main(String [] args) {
    		Sample sample = new Sample();
    		sample = sample.method();
    		}
    }
    I cannot believe at how frustrating this is. This seems all so much just for one integer.
    This is what I am trying to figure out :
    can you begin a method with simply &quot;void&quot;? (and possible more questions)-picture-2.png
    I didn't want to ask for the simple answer because that doesn't teach me anything. But then there was this question and others that follow it all having to deal with methods and I simply do not understand methods and calling methods. Well... I take that back, I understand it slightly yet I don't understand when to create two separate java files or when they can be within the same java file. Because the last code I did that implemented calling methods was this one:
    Java Code:
    import java.util.Scanner;
    import java.util.Random;
    class hw6{
    	
    	public static void main(String[] args) {
    		int n; 
    		
    		Scanner sc = new Scanner(System.in) ;
    		
    			System.out.print("\n\n\tPlease enter a positive integer:") ;
    			
    			n = sc.nextInt();
    			int [] arr = new int[n];
    			gen (arr, n) ;
    			System.out.printf("\n\tThe number of data is: %d \n", n);
    			System.out.printf("\n\tThe largest element is: %d \n", max(arr));
    			System.out.printf("\n\tThe smallest element is: %d \n", min(arr));
    			System.out.printf("\n\tThe average is : %.2f\n", avg(arr));
    			System.out.printf("\n\tThe standard deviation: %.2f\n", stdev(arr));
    			histogram(arr,100);
    	}
    	
    	
    	public static int sum(int [] A){
    		int total = 0;
    		for (int i = 0; i < A.length ; i++)
    			total += A[i];
    		return total;
    	}
    	
    	public static double stdev(int [] A ){
    		int sumofSq = 0;
    		double vari = 0;
    		for (int i = 0; i < A.length ; i++)
    			sumofSq += A[i]*A[i];
    			vari = (double) sumofSq/A.length - avg(A)*avg(A);
    		double stdev = (double) Math.sqrt(vari);
    		return stdev;
    	}
    	
    	public static double avg(int [] A) {
    		int total = sum(A);
    		double average =  (double) total/A.length;
    		return average;
    	
    	}
    	
    	public static int max(int [] A ) {
    		int m = A[0] ;
    		for (int i = 1 ; i < A.length; i++)
    			if (A[i] > m) m = A[i];
    		return m;
    	}
    	
    	public static int min(int [] A ) {
    		int n = A[0] ;
    		for (int i = 1 ; i < A.length; i++)
    			if (A[i] < n) n = A[i];
    		return n;
    	}	
    	
    	public static void gen(int [] A, int n){
    			// this method will generate n random integer
    			// and store them in the array A
    		Random rand = new Random() ;
    		
    		for (int i = 0; i < n ; i++ )
    			A[i] = rand.nextInt(101) ;
    	} // end of gen
    	
    	public static void print(int [] B) {
    		for (int i = 0; i < B.length ; i++){
    			if ( i % 10 == 0 ) System.out.printf("%5d", B[i]);
    			else System.out.printf( "%5d", B[i]);
    	}
    	
    	System.out.printf( "\n\n");
    	}
    	
    	public static void histogram( int [] arr, int max) {
    		int [] count = new int[max+1];
    		for (int i = 0; i < arr.length ; i++)
    			count[arr[i]]++;
    		for (int i = 0; i < count.length; i++)
    			if (count[i] != 0){
    				System.out.printf("%3d:%s\n", i, printStar(count[i]));
    			}
    			
    	}
    	
    	
    	public static String printStar(int n) {
    		String line = "" ;
    		for (int i = 0; i < n; i++) 
    			line +="*";
    		return line ;
    	}
    }
    So I understand that one but I don't understand this one that I am working on currently.
    And this is a part of my take-home midterm so I would really like to understand this.

    I have tried changing
    Java Code:
            sample = sample.method();
    to
    Java Code:
            Sample = Sample.method();
    and neither work. As I try to compile I cannot escape this error "Exception in thread "main" java.lang.NoClassDefFoundError: TestSample
    ". I thought that the TestSample was supposed to be the main method?

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: can you begin a method with simply "void"? (and possible more questions)

    Quote Originally Posted by exeye0h View Post
    So something like this?
    Java Code:
    public class TestSample {
    	public static void main(String [] args) {
    		Sample sample = new Sample();
    		sample = sample.method();
    		}
    }
    You're getting close, almost there, but it's simpler than that. Just change

    Java Code:
    sample = sample.method();

    to
    Java Code:
    sample.method(5); // or whatever number you are trying to run this on
    The method "method" returns void, so you can't have sample = sample.method(); since void can't be assigned to any variable. Also the method requires an int parameter, so make sure to pass one in when calling it.

    and neither work. As I try to compile I cannot escape this error "Exception in thread "main" java.lang.NoClassDefFoundError: TestSample
    ". I thought that the TestSample was supposed to be the main method?
    TestSample has a main method and so the code should work.

  9. #9
    exeye0h's Avatar
    exeye0h is offline Member
    Join Date
    Jan 2012
    Location
    Chicago, IL, USA
    Posts
    40
    Rep Power
    0

    Default Re: can you begin a method with simply "void"? (and possible more questions)

    I absolutely and sincerely appreciate your patience, Fubarable!
    I believe 1324 should produce 3. Least that is what I got as an answer.
    So THANK YOU!!!!!! Very very much appreciated!!!

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: can you begin a method with simply "void"? (and possible more questions)

    You're quite welcome. Remember, the best way to get good at this is to write lots and lots and lots of code. Keep at it hard and it will get easier.

  11. #11
    exeye0h's Avatar
    exeye0h is offline Member
    Join Date
    Jan 2012
    Location
    Chicago, IL, USA
    Posts
    40
    Rep Power
    0

    Default Re: can you begin a method with simply "void"? (and possible more questions)

    I wish I had knack for programming. This kind of frustration is the exact reason why I switched from Mathematics & Computer Science major to strictly Applied & Computational Mathematics haha.
    But I will most definitely apply myself to this!

Similar Threads

  1. Replies: 8
    Last Post: 05-25-2011, 07:26 PM
  2. Equivalent of " void* " in JAVA
    By ABHIJEEEEEEET in forum New To Java
    Replies: 7
    Last Post: 08-16-2010, 07:55 AM
  3. How can I solve "void cannot be dereferenced" error?
    By fervent07 in forum New To Java
    Replies: 2
    Last Post: 04-05-2010, 09:54 PM
  4. Replies: 3
    Last Post: 05-21-2009, 04:48 PM
  5. <core:forEach var="" begin="+<%=j%>+">???
    By freddieMaize in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 09-27-2008, 02:20 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •