1

The statement says:

Write a list of multiples of 7 between 98 and 266, both including

I put this code:

import java.util.*;

public class Multiples7 {

  public static void main (String[] args) {
    Scanner entrada;

    int x;

    entrada = new Scanner(System.in);

    while (x >= 98 && x <= 266) {
      if (x % 7 == 0){
        System.out.println(x);
      }
    }
  }
}

and I get this error that I don't understand: variable x might not have been initialized

Why x not start?

Dennis Meng
  • 5,109
  • 14
  • 33
  • 36
  • 2
    You didn't initialize x. You need to assign x a value. Integers don't have a default value unless they are static class members. – Tetramputechture Oct 22 '14 at 15:52
  • Also, you might make `x` a static variable, which case it will be auto-initialized to `0`. (Though as others noted, initializing it is the best option.) – Gábor Bakos Oct 22 '14 at 15:54
  • int x = 0; Then x is initialized – JackTools.Net Oct 22 '14 at 15:55
  • But if i put int x = 0, the program dont print the result –  Oct 22 '14 at 15:56
  • possible duplicate of [Reason for The local variable may not have been initialized?](http://stackoverflow.com/questions/14422722/reason-for-the-local-variable-may-not-have-been-initialized) – Dennis Meng Oct 22 '14 at 15:57
  • 1
    @Cristiano You're never changing the value of `x`, plus I get the feeling that you actually wanted to initialize it to 98. You might want to take a step back and think about how you'd write out the multiples from 98 to 266 with paper and pencil first. – Dennis Meng Oct 22 '14 at 15:58
  • 3
    Hint for the future: you really want 7, 98 and 266 to be variables : )) – moonwave99 Oct 22 '14 at 16:00
  • 1
    @DennisMeng I do not think this is a duplicate. I think the problem the OP actually has is with the While loop. I think Cristiano is assuming the while loop test condition will set `x` to each value between 98 and 266 and execute the body. – Colin D Oct 22 '14 at 16:21

7 Answers7

3

To solve the question asked: you simply need to initialize x, which is currently uninitialized. To initialize a variable, you have to assign it a value. For example x = 0;.

However, that still is not going to cause your program to print the correct result.

One way to accomplish what you actually want to do is iterate the numbers between 98 and 266 print them when they are divisible by 7.

for(int y = 98; y <= 266; ++y) if (y % 7 == 0) System.out.println(y);

alternately, you can start at 98 (14 * 7) and then increment it by 7, printing as you go.

int y = 98;
while(y <= 266) {
  System.out.println(y);
  y+=7 
}
Colin D
  • 5,641
  • 1
  • 23
  • 35
2

You need to read the value of x or initialize it yourself. This error is shown because there is a chance that the program might get over without x being initialized.

Just initialize it :

int x = 0;

or read from scanner

x = entrada.nextInt();
Fyre
  • 1,180
  • 8
  • 12
2

Alternatively, you could use a for loop, which includes initialization.

for (int x = 98; x <= 266; x++) {
    if (x % 7 == 0) {
        System.out.println(x);
    }
}
Tetramputechture
  • 2,911
  • 2
  • 33
  • 48
1

You have only declared x but did not initialize it. Insted of int x do int x = 0;. Replace 0 with the desired value.

Robin Ellerkmann
  • 2,083
  • 4
  • 29
  • 34
1

You need to give X a starting value or it might as well not exist.

For example if X should start at 0 then use:

int x = 0;
sealz
  • 5,348
  • 5
  • 40
  • 70
0

you need to initialize x so it has a starting value and is not empty when your programm starts(int x = 98;). Also you should increment x inside your while loop (x++; or you will have an infinity loop always printing the same line.

int x = 98;

entrada = new Scanner (System.in);

while ( x >= 98 && x <= 266) {
  if (x % 7 == 0){
    System.out.println(x);
  }
  x++;
}
Simulant
  • 19,190
  • 8
  • 63
  • 98
0

It could be a single for loop. Initialize x at 98, increment by 7 and stop when x is greater then 266. Something like,

for (int x = 98; x <= 266; x += 7)
    System.out.printf("%d = 7 * %d%n", x, x / 7);
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249