0

I am pretty new at Java and I am finding difficulty in solving the problem. Basically the code get a number, and generate a vector in the function generateVector. When I run this code, I am asked to put a number and then the software stay running forever. If possible, could you guys help me without other functions that is kind advanced? I am still learning. Thanks.

import java.util.Scanner;

public class Atividade02 {
    static Scanner dados = new Scanner(System.in);
    static int n;

    //Main
    public static void main(String args[]){
        System.out.println("Type a number: ");
        n = dados.nextInt();
        int[] VetorA = generateVector(n);

        for(int i=0; i<VetorA.length; i++){
            System.out.println("Position: "+ VetorA[i]);
        }
    }

    //Função
    public static int[] generateVector(int n){
        int[] VetorA = new int [n];
        for (int i=0; i<n; i++){
        VetorA[i] = dados.nextInt();
        }
        return VetorA;
    }
}         
user2852724
  • 55
  • 1
  • 2
  • 6

2 Answers2

3

I am asked to put a number and then the software stay running forever.

Did you enter in the n numbers required by generateVector? The program is probably just blocked on input from the user.

Amir Afghani
  • 35,568
  • 16
  • 81
  • 120
0

Try to modfiy the class as follows:

import java.util.Scanner;

public class Atividade02 {
    // Added private access modifiers for properties.
    // It's not necessary here, but as a general rule, try to not allow direct access to 
    // class properties when possible.
    // Use accessor methods instead, it's a good habit
    private static Scanner dados = new Scanner(System.in);
    private static int n = 0;

    // Main
    public static void main(String args[]){

        // Ask for vector size
        System.out.print("Define vector size: ");
        n = dados.nextInt();

        // Make some space
        System.out.println(); 

        // Changed the method signature, since n it's declared 
        // as a class (static) property it is visible in every method of this class
        int[] vetorA = generateVector();

        // Make some other space
        System.out.println(); 

        // Show results
        for (int i = 0; i < vetorA.length; i++){
            System.out.println("Number "+ vetorA[i] +" has Position: "+ i);
        }
    }

    // The method is intended for internal use 
    // So you can keep this private too.
    private static int[] generateVector(){
        int[] vetorA = new int[n];

        for (int i = 0; i < n; i++) {
            System.out.print("Insert a number into the vector: ");
            vetorA[i] = dados.nextInt();
        }

        return vetorA;
    }
}

Also when naming variables stick with the Java naming convention, only classes start with capital letters.

DevJimbo
  • 98
  • 1
  • 9
  • Hey, Thanks for the answer. In this case you removed the **for** statement, which was the responsible for loading the vectorA til the position n. With the code you showed my, I will only get 0 on the vector results. Thanks. – user2852724 Aug 19 '15 at 04:14
  • My apologies, my brain was bananas yesterday. I modified the answer. Hope I got you right this time :) – DevJimbo Aug 19 '15 at 10:16