Search code examples
javaarraysdo-whilenetbeans-8

String value won't inputted


I am still beginner on programming.

my string value wont inputted on array

if i input "y" after input all the a b c, then input the a b c the array just get the last

String a, b, c, d;
    int x = 0;
    Scanner in = new Scanner(System.in);

    do {

        x++;
        int e = x - 1;
        array = new String[x][3];

        System.out.println("input a : ");
        a = in.nextLine();
        array[e][0] = a;
        System.out.println("input b : ");
        b = in.nextLine();
        array[e][1] = b;
        System.out.println("input c : ");
        c = in.nextLine();
        array[e][2] = c;
        System.out.println("Again ? (y/n)");
        d = in.nextLine();

    } while (d.equals("y"));

    for (int i = 0; i < array.length; i++) {
        System.out.println("Output "+(i+1)+" : ");
        System.out.println("a :" + array[i][0]);
        System.out.println("b :" + array[i][1]);
        System.out.println("c :" + array[i][2]);
        System.out.println("");
    }

}

enter image description here


Solution

  • Here in do-while loop, You are assigning a new String array to array in each iteration. Therefore It will not store the previously entered values. So at last when you print it, You will get only last entered entries and previously entered entries will be null as new Defined array has all elements as null initially. So you need to define the array outside of the do-while loop.

    array = new String[4][3]; //Define array here of any appropriate size
    
    do {
        x++;
        int e = x - 1;
    
        System.out.println("input a : ");
        a = in.nextLine();
        array[e][0] = a;
        System.out.println("input b : ");
        b = in.nextLine();
        array[e][1] = b;
        System.out.println("input c : ");
        c = in.nextLine();
        array[e][2] = c;
        System.out.println("Again ? (y/n)");
        d = in.nextLine();
    
    } while (d.equals("y"));
    

    In case you don't know the total size of Array, you can use dynamic structure like ArrayList<String>. You can use ArrayList of ArrayList or just different ArrayList manually but If your total possible inputs are more then it is preferable to use ArrayList of ArrayList.

    Different ArrayLists:

    This approach will help here If you know the total possible inputs. So here we know that we have total 3 possible inputs a,b and c So define 3 ArrayLists for each of a,b and c.

    ArrayList<String> listA=new ArrayList<String>();
    ArrayList<String> listB=new ArrayList<String>();
    ArrayList<String> listC=new ArrayList<String>();
    
    do {
        System.out.println("input a : ");
        a = in.nextLine();
        System.out.println("input b : ");
        b = in.nextLine();
        System.out.println("input c : ");
        c = in.nextLine();
    
        listA.add(a);
        listB.add(b);
        listC.add(c);
    
        System.out.println("Again ? (y/n)");
        d = in.nextLine();
    
    } while (d.equals("y"));
    
    for (int i = 0; i < listA.length; i++) 
    {
        System.out.println("Output "+(i+1)+" : ");
        System.out.println("a :" + listA.get(i));
        System.out.println("b :" + listB.get(i));
        System.out.println("c :" + listC.get(i));
        System.out.println("");
    }