?
Solved

JAVA arrays and methods

Posted on 2009-05-18
5
Medium Priority
?
323 Views
Last Modified: 2013-11-23
I need help with fixing up  the code that test the method in the main method.
And finding  the index first  occurance of the smallest index. My program  finds the index of the smallest value in the entire array.


I am writing a program that takes the smallestindex that takes the parameters, a int array and it size  and returns the index of the first ocuurene of the smallest value.
{
double[] alpha = new double[50];
                 System.out.println(smallestindex(alpha));
      {
           public static int smallestIndex(int  alpha[])
			int index = 0;
			int  smallest;
			smallest  = data[0];
			for(i =0; i < data.lenght(); i++)
			if(smallest < data[i])
			{
			smallest = data[i];
			index = i;
			}
			return index;

Open in new window

0
Comment
Question by:Valisha
  • 3
5 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 24416924
That's looking ok, apart from the array being empty as shown. What's the problem?
0
 
LVL 92

Expert Comment

by:objects
ID: 24416957
>                         for(i =0; i < data.lenght(); i++)

should be:

                        for(i =0; i < data.length; i++)

0
 
LVL 92

Expert Comment

by:objects
ID: 24416969
> double[] alpha = new double[50];

you also don't initialize your array

double[] alpha = new double[] { 1.2, 4.5, 1.7, 9.0, -3.4, 5.6};

or you could use a loop



0
 

Author Comment

by:Valisha
ID: 24417387
System.out.println(smallestindex(alpha));
      {
           public static int smallestIndex(int  alpha[])
is this part correct
0
 
LVL 92

Accepted Solution

by:
objects earned 2000 total points
ID: 24417444
try this:

public class Smallest {

      public static void main(String[] args)
      {
            int[] alpha = new int[] { 1, 4, 1, 9, -3, 5};
            System.out.println(smallestIndex(alpha));
      }

      public static int smallestIndex(int[] data) {
            int index = 0;
            int smallest;
            smallest = data[0];
            for (int i = 0; i < data.length; i++)
                  if (smallest < data[i]) {
                        smallest = data[i];
                        index = i;
                  }
            return index;
      }

}


0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month15 days, 23 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question