[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

JAVA arrays and methods

Posted on 2009-05-18
5
Medium Priority
?
322 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

649 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