-4

I don't understand why this runs but does not print anything. (I am new to coding altogether, so any advice is great.)

import java.util.Scanner;
import java.util.Random;
public class ArrayDoubleValues{ 
  public static void main(String[] args){ 
    Scanner scan = new Scanner(System.in);

    // Length input
    System.out.println("Length: ");
    int length = scan.nextInt();

    int[] list = new int[length];

    for (int i = 0; i == list.length; i++){
      list[i] =(int)(Math.random()+ 1) * 100; 
      System.out.print(list.length);
    }

    for(int i = 0; i == list.length; i++){
       System.out.println(list[i]); 
    }
  }
}
moondaisy
  • 4,303
  • 6
  • 41
  • 70
Phantom
  • 13
  • 5
  • 1
    That's Java, not JavaScript :) –  May 18 '18 at 14:17
  • 1
    It isn't JavaScript, it's Java (I guess, maybe is just a mix of Java and JS). [What's the difference between JavaScript and Java?](https://stackoverflow.com/questions/245062/whats-the-difference-between-javascript-and-java) – Gabriel Carneiro May 18 '18 at 14:18

1 Answers1

1

You have a wrong condition here --

for (int i = 0; i == list.length; i++){

Your program never enters these loops because i is not equal to list.length when you reach them. Instead, it should be

for (int i = 0; i < list.length; i++) { ... }
  • this fixed it. Thank you! I am confused though. Why would == not be accepted? Does it not mean "equal to" So it would stop when i equals the length of the array? – Phantom May 18 '18 at 14:42
  • This condition means "repeat the loop _while_ it's true". Since it's never true, you repeat the loop 0 times. –  May 18 '18 at 14:47
  • I think you got it wrong how the loop statement works. The condition you define there must be met to enter the loop. If you put "i == list.length" in there that would mean you want it to execute only when i is equal to length of the list. However, whenever the condition isn't met the program will leave the for loop, won't increment i and will never go inside the loop (unless list.length is equal 0). – Amongalen May 18 '18 at 14:51