This week's book giveaway is in the iOS forum.
We're giving away four copies of Classic Computer Science Problems in Swift and have David Kopec on-line!
See this thread for details.
Win a copy of Classic Computer Science Problems in Swift this week in the iOS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Selection Sort  RSS feed

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Everyone,

I am new in JAVA. I was practicing Selection sort, but it causes an error I couldn't figure it out. Any suggestion? Thanks ahead


the error is :

Compiling
error:java.io.IOException: /Solution.java:18: error: missing return statement
}
^
1 error
 
Saloon Keeper
Posts: 1431
43
Android Chrome IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please note that some of you spacing can be improved upon.
Given this code: The error that you are getting is due to the fact that you do not have a return statement for all of the cases in the method.
After your for loop, but before the end of the method you need to have a return statement, which happens to be around line 18.
 
Rancher
Posts: 2973
32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

missing return statement 


The compiler can not find a return statement in a method that is defined to return a value.    The solve method is defined to return an int array: int[] so there must be a return  statement that returns an int array.
The method receives an int array as an argument and makes changes to the contents of that array. 
I don't see why the method is defined to an array as it does not create an array that could be returned.
Some solutions:
change the method's definition to return value of void vs int[]
Add a return statement that returns the array that is passed as an arg
 
Jessica Jones
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried change int[] to void, it doesn't work, and I tried writing "return array;" it didn't work either.
 
Master Rancher
Posts: 2563
87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As an aside: in the code you have

What class is 'Solution'? Did you mean 'Practise sol = new Practise();'?
 
Norm Radder
Rancher
Posts: 2973
32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

it doesn't work


What does that mean?
If there were error messages, please copy the full text and paste it here if you need help.
 
Jessica Jones
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Piet Souris wrote:As an aside: in the code you have

What class is 'Solution'? Did you mean 'Practise sol = new Practise();'?



yes, The class was called "Solution".
 
Jessica Jones
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Norm Radder wrote:

it doesn't work


What does that mean?
If there were error messages, please copy the full text and paste it here if you need help.



When I change int[] to void at first line, it appears so many errors:
such as :
TestSolution.java:5: error: 'void' type not allowed here
Assert.assertArrayEquals(new int[]{},s.solve(new int[]{}));
^
/TestSolution.java:11: error: 'void' type not allowed here
Assert.assertArrayEquals(new int[]{1},s.solve(new int[]{1}));
^

then I change int[] back, and add "return array;" at 18 line.
the error now is :
Validation err:Invalid dependency java/lang/System

 
Norm Radder
Rancher
Posts: 2973
32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where did the Assert come from?  It is not shown in your post.

Validation err:Invalid dependency java/lang/System


What program are you executing that gives that error message?
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I've tried the following code and works. I just added the return statement and refactored the name of the class to match with the instance.

remeber to add the package.

 
Marshal
Posts: 58830
179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

We usually don't allow full solutions, but it is over two days since the preceding post, so I shall let that pass. There is a much better way to print an array than what all posts here show. What do you mean about adding the package?
 
André Monteiro
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Campbell, sorry, I didn´t know about the full solutions.

I did mean whether She'll use the class inside a package She should use a package statement.

Can You tell me which is the better way to print an array? Is that snippet following?

System.out.println(Arrays.toString(array));
 
André Monteiro
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

André Monteiro wrote:Thanks Campbell, sorry, I didn´t know about the full solutions.

I did mean whether She'll use the class inside a package She should use a package statement.

Can You tell me which is the better way to print an array? Is that snippet following?

System.out.println(Arrays.toString(array));



I'm thinking I did again.   
 
Campbell Ritchie
Marshal
Posts: 58830
179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

André Monteiro wrote:. . . I'm thinking I did again.   

No, you haven't done anything now. Look here for an easier way to print an array.
 
Campbell Ritchie
Marshal
Posts: 58830
179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Somebody has pointed out my mistake, missing that you found Arrays.toString(). Sorry about that
 
André Monteiro
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No worries. 
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!