0

Here's what I have so far

 double[] num = {1.9, 2.9, 3.4, 3.5};
 double max = num [0];
    for (int i = 1 ; i < num.length ; i++)
    {
        if (num [i] > max)
            max = num [i]; 
    }
    System.out.println ("Max is " + max);

I need to find the index of the greatest value. I've tried printing the index by putting a sentence inside the if statement, and I've also tried by storing i into another variable. None of them worked.

JSox
  • 25
  • 4
  • Why do you have a space between `num` and `[i]`? Perhaps that is causing it. Are you getting compilation errors or what? – RaminS Apr 26 '16 at 23:28
  • @Gendarme nope, the space isn't causing nay problems. The problem isnt with the array, I just need to find a way to output the index of the greatest value. – JSox Apr 26 '16 at 23:30
  • So, you want the *index* of the max entry, and you are storing the *value* of the [i] to the max variable. Perhaps consider setting an` int idx = 0`, and then in the loop instead of `max = num[i]`, set the idx to to num[i]? – KevinO Apr 26 '16 at 23:31
  • The space won't cause any issues but is good practice to not put extra spaces. You need to start at i=0, not i=1 as array indexes start at 0, not 1. – Gremash Apr 26 '16 at 23:31
  • http://stackoverflow.com/questions/6171663/how-to-find-index-of-int-array-in-java-from-a-given-value – RaminS Apr 26 '16 at 23:32
  • 1
    @Gremash There is no need to start at 0. `max = num[0];` takes care of that. – RaminS Apr 26 '16 at 23:34

2 Answers2

2

You need to keep track of the max value and its index.

public class GetMaxIndex {
    public static void main(final String[] args) {
        final double[] numbers = { 1.9, 2.9, 3.4, 3.5 };
        double maxNumber = numbers[0];
        int maxIndex = 0;
        for (int i = 1; i < numbers.length; i++) {
            if (numbers[i] > maxNumber) {
                maxNumber = numbers[i];
                maxIndex = i;
            }
        }
        System.out.println("Max is " + maxNumber);
        System.out.println("Index of max is " + maxIndex);
    }
}
Steve Kuo
  • 61,876
  • 75
  • 195
  • 257
JayC667
  • 2,418
  • 2
  • 17
  • 31
  • 2
    @Gremash It does not. – RaminS Apr 26 '16 at 23:32
  • @Gremash No it doesn't. – Luke Joshua Park Apr 26 '16 at 23:33
  • 3
    Oh my god guys, don't u get it? As it already is initialized by the index 0, you can skip that. – JayC667 Apr 26 '16 at 23:33
  • Yep. That is true. I didn't pay close enough attention. I just jumped right to the loop. – Gremash Apr 26 '16 at 23:34
  • @JayC667 I've been learning java for about 6 months now. I sure do have a lot of learning ahead of me! – JSox Apr 26 '16 at 23:34
  • 1
    Hmm... why do you call the variable `maxNumer` instead of `maxNumber`? Some secret sorcery you are not allowed to share with us mortals? – RaminS Apr 26 '16 at 23:40
  • Hehe cause I love typos^^ I'll correct it. Thanx ;-) – JayC667 Apr 26 '16 at 23:42
  • How do you manage to do the same typo in all four places, still having valid code? Do you copy-paste your variables? :O – RaminS Apr 26 '16 at 23:44
  • 1
    @JSox If Java is your first language, it's a great one to learn on because of the strong typing. Have fun transitioning to Javascript! – Robert Mennell Apr 26 '16 at 23:44
  • @Gendarme He probably does, haha. – JSox Apr 26 '16 at 23:46
  • 1
    @Gendarme No it's because I'm using Eclipse's Refactoring, it renames all of its occurences, so one typo becomes a 'typo' everywhere. – JayC667 Apr 26 '16 at 23:46
  • @RobertMennell Thanks a lot! And yes, I'm thinking of transitioning to either python or Javascript after I learn a fair bit of Java. – JSox Apr 26 '16 at 23:47
  • @JSox I actually kind of meant that sarcastically. A lot will be the same. A LOT will be vastly different. – Robert Mennell Apr 26 '16 at 23:51
  • 1
    @RobertMennell hehe man don't joke about JavaScript... on the one hand it's really powerful. On the other hand I hate 'weak' typing and the lack of proper classes (okay, that's in the progress of being fixed now, but still...). How could they take the name of a really beautiful GPPL and turn it into an awful scripting language...? – JayC667 Apr 26 '16 at 23:54
0
double[] num = {1.9, 2.9, 3.4, 3.5};
double max = num[0];
int    maxIndex = 0;
for (int i = 1 ; i < num.length ; i++)
{
   if (num[i] > max)
  {
    maxIndex = i
    max = num[i]; 
  }
}
System.out.println("Max is " + max);
System.out.println("Max index is + maxIndex);