Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

myteethhurt

  • 4 years ago

public class tester { int[]alpha= {25,35,45,55}; for(int x= 0; x<=4; x++) System.out.print(alpha[x]+" "); } How can I make it compile?

  • This Question is Closed
  1. Tomas.A
    • 4 years ago
    Best Response
    You've already chosen the best response.
    Medals 2

    first of all you need to have main method secondly in loop you should stop at element 4, e.g. make x<4 instead of x<=4 because it would be out of bounds

  2. myteethhurt
    • 4 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    How do you write the main method if alpha was already declared?

  3. myteethhurt
    • 4 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    I have absolutely no understanding of arrays :(

  4. Tomas.A
    • 4 years ago
    Best Response
    You've already chosen the best response.
    Medals 2

    wrap evetyhing to main method

  5. Tomas.A
    • 4 years ago
    Best Response
    You've already chosen the best response.
    Medals 2

    you always have to have main method arrays have nothing to do with it

  6. Tomas.A
    • 4 years ago
    Best Response
    You've already chosen the best response.
    Medals 2

    public class tester { public static void main (String[] args) { int[]alpha= {25,35,45,55}; for(int x= 0; x<4; x++) System.out.print(alpha[x]+" "); } }

  7. myteethhurt
    • 4 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    OH NO DUH!!!

  8. myteethhurt
    • 4 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Thank you so much! I was thinking of object oriented programming and I was really confused

  9. Neeraj_OCW
    • 4 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Remove the equal sign from for loop of array, as they are zero-index based. It'll always count a index more than your actual array indexes,if you put equal.

  10. Not the answer you are looking for?
    Search for more explanations.

    • Attachments:

Ask your own question

Sign Up
Find more explanations on OpenStudy
Privacy Policy