1

Help please, I'm new to java. I have to use arrays, for, and subroutines for homework. This is my code so far:

import java.util.*;
import static java.lang.System.out;
public class ventasArreglo{
  static int dias, semanas, i, j;
  static Scanner kb=new Scanner(System.in);

  public static void main(String args[]){
   dias=5;
   semanas=4;
   int mes[][] = new int[semanas][dias];
   introducirDatos();

  }

  static void introducirDatos(){
    for(i=0;i<semanas;i++){
      for(j=0;j<dias;j++){
        out.println("Cantidad de Ventas");
        mes[i][j]=kb.nextInt(); 
      }
    }
  }
}

But after compiling I keep getting this error: 1 error found: [line: 20]

Error: cannot find symbol
  symbol:   variable mes
  location: class ventasArreglo
  • You are getting error because your variable mes isn't defined for the whole class, but for the main function, so the other function couldn't access it. You can pass the array to the function so that it could access it. I suggest you learn about 'variable scope' – burglarhobbit Oct 01 '15 at 00:05

1 Answers1

1

The declaration for variable mes should be moved outside so that it is visible to static method introducirDatos:

    static int dias, semanas;
    static int[][] mes;

    public static void main(String[] args) {
        dias=5;
        semanas=4;
        mes = new int[semanas][dias];
        introducirDatos();

    }
    static void introducirDatos(){
        Scanner kb=new Scanner(System.in);
        for(int i=0;i<semanas;i++){
            for(int j=0;j<dias;j++){
                out.println("Cantidad de Ventas");
                mes[i][j]=kb.nextInt();
            }
        }
    }

Also Scanner should be moved inside method where it is actually needed rather than declaring it static on top level. Also loop counters need not to be on top level.

But why are you using static imports? We should use them sparingly. As mentioned in doc:

So when should you use static import? Very sparingly! Only use it when you'd otherwise be tempted to declare local copies of constants, or to abuse inheritance (the Constant Interface Antipattern). ... If you overuse the static import feature, it can make your program unreadable and unmaintainable, polluting its namespace with all the static members you import.

Check this link as well.

Community
  • 1
  • 1
akhil_mittal
  • 23,309
  • 7
  • 96
  • 95
  • I'm not sure why we're using static imports, my teacher said we would get more into that later. He said we should use them. –  Oct 01 '15 at 00:16
  • @ricardoryz: Static imports should be use sparingly. Check my edit. – akhil_mittal Oct 01 '15 at 00:17