0

This is my first time and my first question, used this site a lot and love it. So here I go:

currently I learn Java (Java 2 SE), and in my book (Java - A Beginer's Guide (Herbert Schildt)) I managed to get to chapter 5, and am starting to learn about arrays. I can so far initialize and do other stuff but I cannot get this code right...Why does the eclipse say it is wrong? I used the new update with the eclipse but then again...wrong!

Here is the code!

Class MinMax2 {
    public static void main(String args[]) {
      int nums[] = { 99, -10, 100123, 18, -978, 5623, 463, -9, 287, 49 };

      int min, max;

      min = max = nums[0];
      for(int i=1, i<10, i++) {
          if(nums[i]<min) min = nums[i];
          if(nums[i]>max) max = nums[i];
      }
     System.out.println("Min and max: " + min + " " + max);
  }
}

Sorry for long text, but remember I am a rookie at this stuff, here I was going to learn about array initializers

int nums[] = {val1, val2, ... valN};

Please help me!

1
  • 2
    What wrong is saying .You need to change " for(int i=1, i<10, i++) " to " for(int i=1; i<10; i++) " Commented Oct 13, 2014 at 13:10

3 Answers 3

7

Your for loop syntax is incorrect. This,

 for(int i=1, i<10, i++) {

should be using ; instead of , and the length of the array,

 for (int i = 1; i < nums.length; i++) {
Sign up to request clarification or add additional context in comments.

1 Comment

Also it's "class" (with a lowercase c)
4

Use ; instead of , in FOR Loop..

for(int i=1; i<10; i++) {
      if(nums[i]<min) min = nums[i];
      if(nums[i]>max) max = nums[i];
  }

Comments

0

The code you have posted has ,'s in the for loop. These need to be changed to ;'s. I ran your code after changing slightly:

import java.util.ArrayList; import java.util.List;

public static void main(String[] args) {
    System.out.println("Start tmpTest3");

      int nums[] = { 99, -10, 100123, 18, -978, 5623, 463, -9, 287, 49 };

      int min, max;

      min = max = nums[0];
      for(int i=1; i<10; i++) {
          if(nums[i]<min) min = nums[i];
          if(nums[i]>max) max = nums[i];
      }
     System.out.println("Min and max: " + min + " " + max);

    System.out.println("End tmpTest3");
}   

(Changed ,'s to ;'s)

I got the following output:

Start tmpTest3
Min and max: -978 100123
End tmpTest3

So it looks like your code works

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.