0

I'm studying the search loop for an Array. when I tried to do the code by my own, there were some errors I can not found out.By the way I using Netbeans for java coding.

My array search code:

public class JavaApplication {

    public static void main(String[] args) {
        int[] nums = new int[5];
        nums = new int[]{2, 4, 6, 8, 7};
        int target = 7;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] == target) {
                return i;
            }
        }
        return -1;
    }
}

The NetBeans said that there is a unnecessary return statement for my last return statement.

How can I fix the return statement, and run this code?

6
  • 4
    you cannot return a value where the return type is "void", if you need to code any functionality do that in another method and call it within the main method. Commented Apr 10, 2017 at 21:09
  • 1
    I'm voting to close this question as off-topic because questions asking for homework help must include a summary of the work you've done so far to solve the problem, and a description of the difficulty you are having solving it. Source Commented Apr 10, 2017 at 21:16
  • Sorry I don't know why you vote for close and this is not for homework this code made by myself, the difficulty for me is I don't why there is a unnecessary return, you can just like others tell me there is a void so you can not return instead of say something that's unnecessary ok? Commented Apr 10, 2017 at 21:37
  • Also I'm new to java so I come here and looking for some errors I made it so I can improve myself Commented Apr 10, 2017 at 21:38
  • Image that if you are new to java and you make some codes by yourself, and you come to this website looking for help and someone just comment below this question is asking for homework help, and I vote for close this question. how do you feel? Please don't be such a mean person! Commented Apr 10, 2017 at 21:44

2 Answers 2

2

You cannot return anything when the method type is void.

If you need to print if an int is found or not in an array, extract the logic to a separate method and print the result like this

  public static void main(String[] args) {
    int[]nums = {2,4,6,8,7}; // you can initialize the array like this
    int target=7;

    System.out.println(findValue(target, nums));

  }



  private static int findValue(int target, int[] nums) {
    for(int i=0; i<nums.length;i++){
     if(nums[i]==target)
       return i;

        }
        return -1;
    }
Sign up to request clarification or add additional context in comments.

Comments

2

How can I fix the return statement, and run this code?

You can create a separated method which return an int and call it in your main method for example :

public static void main(String[] args) throws Exception {
    myMethod(); //call your method in the main method
}

public static int myMethod() {
    int[] nums = new int[5];
    nums = new int[]{2, 4, 6, 8, 7};
    int target = 7;
    for (int i = 0; i < nums.length; i++) {
        if (nums[i] == target) {
            return i;
        }
    }
    return -1; 
}

You can't return any thing when your method is void, plus you can't return any thing with main method, it is not allowed in java.

1 Comment

thanks i'm new to java so sometimes I made some small errors.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.