0

i'm trying to make data search and sort with switch case. i have to input the amount of data and its members. for example:

amount of data: //for example: 2
input the data(s):

data num-1 : //user input
data num-2 : //user input

selection

case 1: //data search

case 2: //bubblesort

case 3: //selectionsort

case 4: //edit

i stuck at case 4. i've tried my code but the amount of data and the current doesn't changes at all and if i want to change the amount of data with bigger number than before, the index out of the bound. here's the code.

    public static void main(String[] args) {

    Scanner s=new Scanner(System.in);
    int n;
    System.out.println("-------------------data search and sort-------------------");
    System.out.println("");
    System.out.println("");
    System.out.println("Hai ");
    System.out.print("How many datas do you want? : ");
    n=s.nextInt();
    int bil[]=new int[n]; //bil is array
    System.out.println("input the data:");
    for(int i=1;i<=n;i++){
        System.out.print(" data -" +i+ " = ");
        bil[i-1]=s.nextInt();
    }
    System.out.println("");
    System.out.println("");
    while(true){
    
    System.out.println("Displaying data(s) : ");
    for(int i: bil){
        System.out.print(i+" ");
    }
    System.out.println("");
    System.out.println("Menu:");
    System.out.println("\t1. Search data");
    System.out.println("\t2. Bubblesort ascending");
    System.out.println("\t3. SelectionSort descending");
    System.out.println("\t4. Edit");
    System.out.println("\t5. Exit");
    System.out.print("What's your choice? : ");
    int pilih=s.nextInt();
    switch(pilih){
        case 1:
            System.out.println("Enter the number you are looking for = ");
            int srch = s.nextInt();
            boolean found = false;
            for(int index=0; index<bil.length; index++) {
            if(bil[index] == srch){
            found = true;
                }
            }
            if(found == true) {
            System.out.println("Found '"+srch + "' in data collection!");
            } else {
            System.out.println(srch + "there is no data you're looking for here");
            }
            System.out.println("");
            
            System.out.println("Displaying data : ");
            for(int i: bil){
            System.out.print(i+" ");
            }
            System.out.println("");
            System.out.print("Continue? (y/n)");
            String conti = s.next();
           
            boolean nue;
            switch(con){
            case "y":
            nue = false;
            break;
            case "n": 
                return;
            }
            break;
        case 2: 
             
            System.out.println("BubbleSorting is done!");
            System.out.println("");
            System.out.println("Displaying data(s) :");
            int i, j, te;
            for (i = 0; i < n; i++)
                for (i = 0; i < ( n - 1 ); i++) {
                    for (j = 0; j < n - i - 1; j++) {
                        if (bil[j] > bil[j+1]){
                            te = bil[j];
                            bil[j] = bil[j+1];
                            bil[j+1] = te;
                        }
                    }
            }
            for (i = 0; i < n; i++) {
                System.out.print(bil[i]);
            }
           
            System.out.print("Continue? (y/n)");
            conti = s.next();
            switch(conti){
            case "y":
            nue = false;
            break;
            case "n": 
                return;
            }
            break;
        case 3:
            for (i = 0; i < n; i++){
                for (j = i + 1; j < n; j++){
                    if (bil[i] < bil[j]){
                        te = bil[i];
                        bil[i] = bil[j];
                        bil[j] = te;
                    }
                }
            }
            System.out.print("SelectionSort is done");
            System.out.println("");
            System.out.println("Displaying data(s):");
            for (i = 0; i < n - 1; i++){
                System.out.print(bil[i] + " ");
            }
            System.out.print(bil[n - 1]);
            System.out.println("");
            System.out.print("Continue? (y/n)");
            conti = s.next();
            switch(conti){
            case "y":
            nue = false;
            break;
            case "n": 
                return;
            }
            break;
        case 4:
            System.out.print("WARNING!!! the data will be change!");
            System.out.print("continue? (y/t)");
            String change = s.next();
           
            boolean edit;
            switch(change){
            case "y":
            System.out.print("How many datas do you want? = ");
            n=s.nextInt();
            System.out.println("Input the data");
            for(i=1;i<=n;i++){
            System.out.print(" data -" +i+ " = ");
            bil[n]=s.nextInt();
            edit = false;
            }
            break;
            case "n": 
                return;
            }
            
            break;
        }    
    }       

ps: i do not forget to import java.util.Scanner

Olaf Kock
  • 46,930
  • 8
  • 59
  • 90

2 Answers2

0

Hey the issue here is that you are already declaring the array as int bil = new int[n]; So in your 4th case when you take another "n" as input which is larger than the previous "n" , it throws the ArrayIndexOutOfBoundsException.You need to increase the size of array before storing more values in it.Add the following code in your 4th case after taking the new "n" as input:

    bil = Arrays.copyOf(bil,n);

You should refer to this answer for more info:Resize an Array while keeping current elements in Java?

Harmless
  • 38
  • 5
0

Since you need to discard the originally entered data and get a completely new set of data points, first you need to create a new array with the new number of data points. Then you need to assign the user inputs to that array.

case 4:
    System.out.print("WARNING!!! the data will be change!");
    System.out.print("continue? (y/t)");
    String change = s.next();

    boolean edit;
    switch (change) {
        case "y":
            System.out.print("How many datas do you want? = ");
            n = s.nextInt();

            // Create a new array and assign it to the same variable bil
            bil = new int[n];

            System.out.println("Input the data");
            for (i = 1; i <= n; i++) {
                System.out.print(" data -" + i + " = ");

                // Add data point to the (i-1)th position of the array
                bil[i - 1] = s.nextInt();
                edit = false;
            }
            break;
        case "n":
            return;
    }
Udith Gunaratna
  • 2,091
  • 1
  • 13
  • 17