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

Thread: Java - Problem getting my program functions

  1. #1
    Member
    Join Date
    Nov 2013
    Posts
    51
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Java - Problem getting my program functions

    Assessed work

    I want to learn from what i'm doing, so please no answers/spoilers.

    I'm required to create a program that finds the minimum value in an Array of integers.

    Here is my approach :


    public class Person {

    public static void main(String [ ] args){

    int theArray[]=new int [10];
    int result = 0;
    for (int i:theArray){
    if (i < Integer.MAX_VALUE)

    i = result;
    return i;

    }

    }
    }


    I can't really progress from this position. I keep getting told to change the return type to int on main method, then when i do it says change it to void..


  2. #2
    Member
    Join Date
    Sep 2013
    Posts
    68
    My Mood
    Confused
    Thanks
    3
    Thanked 7 Times in 7 Posts

    Default Re: Java - Problem getting my program functions

    your main method signature has void as a return type but in you main method body you are trying to return an integer value and that is also local value in the for loop. If you change you main method return type to int the compiler going to give you an error because java compiler is always looking for a static void main method that take String array as a argument.
    So either move your code to another method whose return type is int and call that method from main or remove the return type from main.

    One more problem with your code is you are comparing i with Integer.MAX_VALUE which is the maximum value of int allowed for your machine.
    Use simple for loop and compare element at index i with element at index i+1.
    sort out these issues and come back if you have any problem.

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Java - Problem getting my program functions

    Welcome to the forum! Please read this topic to learn how to post code correctly along with other useful info for new members.

    I'm going to say some of what aprabhat said differently. As you know, a main() method is required in a Java class as the entry point for the program. The main() method must have the signature you've posted,

    public static void main( String[] args )

    Since it must have the return type void, it may not have a 'return' statement as your code does. Rather than returning a value from your main() method, use a print statement to show the result on the screen.

    As for your algorithm to find the minimum value among a collection of numbers, here's another approach:

    1. pick a number from the collection and assign it to a variable that is assumed to be the minimum number in the collection

    2. compare each of the remaining values in the collection to the minimum number in the collection, and if a smaller number is found, that smaller number becomes the new minimum number in the collection

    3. repeat step 2 until all values in the collection have been considered

  4. The Following User Says Thank You to GregBrannon For This Useful Post:

    aprabhat (February 4th, 2014)

Similar Threads

  1. Java sound functions + emails
    By deanbyrne95 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 26th, 2013, 07:44 AM
  2. how to execute Xquery-Functions in java????
    By shobanakannan in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 4th, 2013, 01:27 AM
  3. Problem with java program and CPU usage
    By Bahramudin in forum Java Theory & Questions
    Replies: 3
    Last Post: July 25th, 2012, 09:26 AM
  4. Replies: 1
    Last Post: February 14th, 2012, 12:53 PM
  5. Calling Existing DLL functions using java...
    By orihsoyk in forum Java Theory & Questions
    Replies: 0
    Last Post: April 6th, 2011, 07:23 AM

Tags for this Thread