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

Thread: Passing an element from an array to a Method

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question Passing an element from an array to a Method

    Hello;

    My code is supposed to validate each element of the array to ensure they are within the valid range (50-100).
    I can't seem to pass them to the Method.

    import java.util.Scanner;

    public class Method2 {

    public static void main(String[] args){
    Scanner input = new Scanner(System.in);
    final int NUMBER_OF_INTEGERS = 5;
    int[] numbers = new int[NUMBER_OF_INTEGERS]; //Create an array

    for(int i = 0; i <NUMBER_OF_INTEGERS; i++){
    System.out.println("Enter an integer (50-100): ");
    numbers[i] = input.nextInt();
    int isValid(i);
    }

    public static int isValid(int n){
    int result;

    if(n <= 50 && n >= 100)
    System.out.println("**Invalid number ");
    else
    result = n;

    return result;
    }
    }


    }


  2. #2
    Member Kewish's Avatar
    Join Date
    Apr 2013
    Location
    Australia
    Posts
    116
    Thanks
    10
    Thanked 17 Times in 14 Posts

    Default Re: Passing an element from an array to a Method

    Please read the sticky post at the top of this subforum for the correct way to post your code. Announcements - What's Wrong With My Code?


    public static int isValid(int n)
    This returns an integer. Which you do here;
    return result;
    Great! Where are you storing that returned result??? There's problem #1

    Also, I think you may need to consider your steps again.
    Step 1 - Read Number
    Step 2 - Test Number
    Step 3 - Valid Number = Store It / Invalid Number alert user
    Step 4 - Repeat until array is filled with valid numbers.

    Also, are 50 and 100 valid entries or not?
    Why did you use && in the if statement. Can a number be both <= 50 AND >= 100 at the same time?
    You are passing the value of i into the method and testing that, is that the number you want to test if it is valid. What is the value of i for each iteration of the loop

    With a name like isValid, I feel it would be more natural to return a boolean result. But both ways can work.

Similar Threads

  1. Passing array method error
    By floorplay in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 31st, 2013, 09:07 AM
  2. [SOLVED] Passing 2D array to method
    By drgroove777 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 24th, 2012, 02:08 AM
  3. [SOLVED] Cannot find symbol - Calling method passing array Pets
    By Rilstin81 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 11th, 2011, 03:27 PM
  4. passing a string and int array into a static method
    By u-will-neva-no in forum Java Theory & Questions
    Replies: 2
    Last Post: May 8th, 2011, 05:35 PM
  5. how to find an element in an array
    By humdinger in forum Collections and Generics
    Replies: 8
    Last Post: April 9th, 2010, 05:22 PM

Tags for this Thread