1

I have tried to initialize an array dynamically and display the array as result:

import java.util.Scanner;

public class Arrays {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner in = new Scanner(System.in);
        int[] c;
        String cmd="yes";
        while(cmd=="yes") {
            System.out.println("Enter value for c :");
            c=new int[] {in.nextInt()};
            System.out.println("Continue(yes/no)? :");
            cmd=in.next();
        }
        for(int k:c)
            System.out.println(k);
    }
}

But at the for-each loop(i.e. line 12 from main()) it is showing error as "The local variable c may not have been initialized".

DamCx
  • 1,047
  • 1
  • 11
  • 25
  • Initialize the c arr, `int[] c = null`, compiler doesn't know code flow will always go inside while loop – Hemant Patel May 18 '18 at 06:45
  • is the desired funcitonality to use this program to enter numbers into the array until you enter no at the continue stage? – HamishD May 18 '18 at 06:45

2 Answers2

2

The other answers are correct, you did not initialize your array. you would need to call c=null; for this to work.

However, if I understand correctly, you are trying to add numbers to an array of indefinite size, which you cannot do in Java. you should use an ArrayList.

you also used == to compare String values, which is a big no-no, I have changed it to .equals in my answer below.

public static void main(String[] args) {
    // TODO Auto-generated method stub
    Scanner in = new Scanner(System.in);
    ArrayList<Integer> c = new ArrayList<>();
    String cmd="yes";
    while(cmd.equals("yes")) {
        System.out.println("Enter value for c :");
        c.add((Integer)in.nextInt());
        System.out.println("Continue(yes/no)? :");
        cmd=in.next();
    }
    for(Integer k:c)
        System.out.println(k);
}
HamishD
  • 349
  • 2
  • 15
  • 1
    Take a look at the condition in the while-loop. Would be wise to fix that as well. – Ben May 18 '18 at 06:59
0

For your actual compilation error, c could be not initialized if the loop is not executed. Even if you know it will (since cmd is equals to "yes", the compiler don't go that far).

You will find answers that initialize at first the value, like Neng Liu's answer, but based on your logic, you can use a do..while loop since your logic show that you want to read at least once the Scanner.

do{
    System.out.println("Enter value for c :");
    c=new int[] {in.nextInt()};
    System.out.println("Continue(yes/no)? :");
    cmd=in.next();
}while("yes".equals(cmd)); //.equals is needed to compare `String` correctly

The condition in a do..while is checked after the block statement is executed (compare to a while that check the condition before). So the statement will be execute AT LEAST ONCE, so you are sure c will be initialized that way. And the compiler knows that too.


Since the title mention you want a the dynamic sized array, see HamishD's answer to use a Collection that will grow on needs. And for a more complete post about List, see Java dynamic array sizes?

AxelH
  • 14,325
  • 2
  • 25
  • 55