5 Replies - 1137 Views - Last Post: 08 December 2011 - 08:51 PM Rate Topic: -----

#1 SnoBunny85  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 87
  • Joined: 02-March 10

Cannot return a value from a method whose result type is void

Posted 08 December 2011 - 08:22 PM

I wrote a program for the knapsack algorithm and I am getting the error : cannot return a value from a method whose result type is void... please help

public class Knapsack{
		
		//number of items
		int n = 7;
		//weight of each item
		int[] weight={12, 10, 8, 11, 14, 7, 9};
		//benefit of each item
		int[] benefit={4, 6, 5, 7, 3, 1, 6};
		//maximum weight trying to acquire
		int W = 18; 
		
	int fill(){
			
			//current weight
			int currentWeight = 0;
			//total benefit
			float totalBenefit = 0;
			
			
			//items already used
			int[] used = null;
			int i, maxI;
			//items haven't been used yet
			for(i = 0; i < n; ++i)
			{
				used[i] = 0;
			}
			
			//current weight equals max weight
			currentWeight = W;
			while(currentWeight > 0)
			{
				maxI = -1;
				for(i = 0; i < n; ++i)
					if((used[i] == 0) && ((maxI == -1)|| ((float)benefit[i]/weight[i] > (float)benefit[maxI]/weight[maxI])))
						maxI = i;
						
				used[maxI] = 1;
				currentWeight -= weight[maxI];
				totalBenefit += benefit[maxI];
				
				if(currentWeight >= 0){
					System.out.println("Added object completely in the bag. Space left: ");
					System.out.println(maxI + 1);
					System.out.println(" ");
					System.out.println(benefit[maxI]);
					System.out.println(" ");
					System.out.println(weight[maxI]);
					System.out.println(" ");
					System.out.println(currentWeight);
				}
				else{
					System.out.println("Added object in the bag:");
					System.out.println((int)((1+(float)currentWeight/weight[maxI]) * 100));
					System.out.println(" ");
					System.out.println(benefit[maxI]);
					System.out.println(" ");
					System.out.println(weight[maxI]);
					System.out.println(" ");
					System.out.println(maxI +1);
						
					totalBenefit -= benefit[maxI];
					totalBenefit += (1+(float)currentWeight/weight[maxI]) * benefit[maxI];
					}
				}
				
				System.out.println("Filled the bag with objects worth:");
				System.out.println(totalBenefit);
						 
			}
			
		public void main(String[] args)
		{
			fill();
			return 0;
		}
}
	




Is This A Good Question/Topic? 0
  • +

Replies To: Cannot return a value from a method whose result type is void

#2 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1086
  • View blog
  • Posts: 4,560
  • Joined: 09-June 09

Re: Cannot return a value from a method whose result type is void

Posted 08 December 2011 - 08:25 PM

The error pretty straight forward. main is of type void, therefore you can't return anything, including zero.
Was This Post Helpful? 0
  • +
  • -

#3 Mylo  Icon User is offline

  • Knows all, except most.

Reputation: 265
  • View blog
  • Posts: 747
  • Joined: 11-October 11

Re: Cannot return a value from a method whose result type is void

Posted 08 December 2011 - 08:26 PM

I'd suggest you loop up how methods and return statements relate. However there is not need to have a return statement in your main method, not only is it useless, but will give you that error.

Furthermore, your main method should have a 'static' modifier so that your program can run at all.

public static void main(String[] args) {
   // a typical main method
   // create KnapSack object here
}


Was This Post Helpful? 0
  • +
  • -

#4 Sheph  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 432
  • View blog
  • Posts: 1,020
  • Joined: 12-October 11

Re: Cannot return a value from a method whose result type is void

Posted 08 December 2011 - 08:33 PM

@Mylo

But you CAN return; without any statement following it, should you want to end the program early (It's not totally useless). Other languages like C++ use the return code to test the whether the program had an abnormal execution. In those languages you MUST have a return statement. In Java, it is not required. If you wanted to send a status to the code calling your program, you could have your main method throw an Exception or Error.

However, that said. This looks like a problem of a habit from another language carried over and so none of what I just said will probably help. lol
Was This Post Helpful? 1
  • +
  • -

#5 Mylo  Icon User is offline

  • Knows all, except most.

Reputation: 265
  • View blog
  • Posts: 747
  • Joined: 11-October 11

Re: Cannot return a value from a method whose result type is void

Posted 08 December 2011 - 08:41 PM

Ah, thanks again Sheph, Just keep learning more here =) I'll stick to system.exit though
Was This Post Helpful? 0
  • +
  • -

#6 Sheph  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 432
  • View blog
  • Posts: 1,020
  • Joined: 12-October 11

Re: Cannot return a value from a method whose result type is void

Posted 08 December 2011 - 08:51 PM

Haha I forgot about System.exit(); That one can be called from anywhere too. Yeah I'm pretty sure I've never used a plain old return in main.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1