-1

I have an array and inside its filled with marks for students. However I cannot display the array contents as I keep receiving a reference code or something. I am now trying to check each index in the array and compare it to the pass mark of 40. When I do use an if statement I can never print the [i] afterwards, all help would be appreciated.

public class Exammarks {
    public static void main(String[] args) {
        int i = 0;
        int [] studentmarks = new int[] {45,60,70,21,95,35,83,80,5,41,40,25};
        for(i=1;i < studentmarks.length; i++); {
                if (studentmarks[i] < 40)
                       System.out.println(studentmarks[i]);
        }

    }

}
Donald Wu
  • 698
  • 7
  • 20
Kamanda
  • 305
  • 3
  • 4
  • 11
  • Related: [How to print my objects](https://stackoverflow.com/q/29140402/335858) – Sergey Kalinichenko Oct 05 '17 at 02:43
  • "I keep receiving a reference code or something" - can you be more precise? Show us the faulty code and the expected and actual output (or any error that occurs). – dave Oct 05 '17 at 02:44
  • @dave [I@677327b6 thats what I get when i try to println(studentmarks[i]); Also it shows this error : Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 12 – Kamanda Oct 05 '17 at 02:46
  • The reference code may be the position in memory the array is at. You will need to write a toString() method to get around that. or just change your print statement to `System.out.println(" " + studentmarks[i]);` – Hayden Passmore Oct 05 '17 at 02:47
  • Why define `i=0` and then start from `i=1`? – user1803551 Oct 05 '17 at 02:49
  • What do you mean by "*When I do use an if statement I can never print the [i] afterwards*"? – user1803551 Oct 05 '17 at 02:50
  • @user1803551 I saw it on a forum about finding the largest array, my program is supposed to find and calculate about many students failed. – Kamanda Oct 05 '17 at 02:51
  • You can't go around copying code you don't understand and then ask us to fix it. Use a debugger and move step by step to see what each line does. Also what does "*the largest array*" mean and what is "*find and calculate about many students failed*"? – user1803551 Oct 05 '17 at 02:54
  • @user1803551 I didn't say I didn't know what it did. I've found my answer and alternative from Donald Wu. I created that code and couldn't understand why it didn't work. If you don't want to help you don't have to – Kamanda Oct 05 '17 at 02:56
  • But the answer is not much more correct than your code. It does what your code does only doesn't skip the first element and fixes the `;` typo which you should have found yourself. This site requires a minimal level of question quality. See [ask]. – user1803551 Oct 05 '17 at 03:11

3 Answers3

1

try to use for each loop

int [] studentmarks = new int[] {45,60,70,21,95,35,83,80,5,41,40,25};
System.out.println("studentmarks size = " + studentmarks.length);
for(int marks : studentmarks) {
      System.out.println("marks = " + marks); 
      if (marks < 40)
           System.out.println("marks less than 40 = " + marks);

}
Donald Wu
  • 698
  • 7
  • 20
  • Thanks, I guess I couldn't understand why I couldn't just print the array on its own without having some string in there. Previously tried to print the array on its own using I but assigning it to a variable seemed to have worked. Much appreciated. – Kamanda Oct 05 '17 at 02:58
  • you can try to use ``List``...it is easier to understand – Donald Wu Oct 05 '17 at 02:59
  • @PolyApe *" I guess I couldn't understand why I couldn't just print the array on its own without having some string in there"* You definitely can. "*Previously tried to print the array on its own using I but assigning it to a variable seemed to have worked.*" No difference whatsoever, it's not the reason. – user1803551 Oct 05 '17 at 03:13
  • @user1803551 You gonna input some advice or just be a difficult user? – Kamanda Oct 05 '17 at 07:44
  • @PolyApe I already told you *exactly* where your problems are. Are you going to actually debug your program or just wait for people to write code for you? – user1803551 Oct 05 '17 at 11:26
1

The Array index starts from 0 whereas you have initialized for loop from 1.

 int [] studentmarks = {45,60,70,21,95,35,83,80,5,41,40,25};
 for(i=0;i < studentmarks.length; i++) 
 {
    if (studentmarks[i] < 40)
    {
       System.out.println(studentmarks[i]);
    }
 }

Also, there was ; at the end of for(i=1;i < studentmarks.length; i++);, which means, scope of for loop end there and below part of the code doesn't come under for loop scope.

 {
    if (studentmarks[i] < 40)
    {
       System.out.println(studentmarks[i]);
    }
 }
Ravi
  • 30,829
  • 42
  • 119
  • 173
1

You can do this more easily by creating a Stream from the array, filtering out the numbers less than 40 and either converting the stream back to an array if you need or just print the result. For example:

int [] studentmarks = new int[] {45,60,70,21,95,35,83,80,5,41,40,25};
Arrays.stream(studentmarks)
      .filter(m -> m < 40)
      .toArray();
// [21, 35, 5, 25]

Or if you just need to print the result:

Arrays.stream(studentmarks)
      .filter(m -> m < 40)
      .forEach(System.out::println);
user1803551
  • 12,965
  • 5
  • 47
  • 74
imk
  • 374
  • 6
  • 12
  • Have a look at the `peek` stream operation. You're creating another stream for nothing. – user1803551 Oct 05 '17 at 11:28
  • Your response is redundant. You're already creating an array of filtered results (you're not even caching it), and then you create another stream with the same operation. – user1803551 Oct 06 '17 at 03:27
  • i have given two different answer. First one if he needs an array of his result set and second one for just printing the result set. – imk Oct 06 '17 at 03:51
  • Alright, I edited the question to reflect that better. – user1803551 Oct 06 '17 at 03:54