java.lang.ArrayIndexOutOfBoundsException - error in code Java
So I have this problem... I wanna make a program where the user inputs a num
numbers and then decide if he wants to sort them or search for a number... I get the Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0 at es_personali.es_ordinamenti.main(es_ordinamenti.java:33) error.
Can someone help me? Heres the code
package es_personali;
import java.util.*;
public class es_ordinamenti {
static Scanner in = new Scanner(System.in);
static int num;
static int[] arr =new int[num];
public static void menu(){
System.out.println("| Cosa vuoi fare?");
System.out.println("|");
System.out.println("| 1 - Inserire numeri");
System.out.println("| 2 - Ordinare in ordine crescente");
System.out.println("| 3 - Ricercare un numero");
System.out.println("| 0 - Uscire");
}
public static void main(String[] args){
int ris;
boolean inseriti=false;
System.out.println("------------------BENVENUTO!------------------");
menu();
do{
System.out.println(">>");
ris = in.nextInt();
if(ris==1){
System.out.println("Quanti numeri?");
num = in.nextInt();
for (int i = 0; i < num ; i++) {
System.out.print(">>");
arr[i]=in.nextInt();
}
inseriti=true;
}else if(ris==2){
if(inseriti){
System.out.println("Stampa senza ordinamento: ");
for (int i = 0; i < num; i++) {
System.out.println(arr[i]);
}
System.out.println("Stampa con ordinamento: ");
Arrays.sort(arr);
for (int i = 0; i < num; i++) {
System.out.println(arr[i]);
}
}else System.out.println("Non hai inserito i numeri.");
}
else if(ris==3){
System.out.println("Inserire numero da ricercare: ");
int ricerca = in.nextInt();
System.out.println("Numero " + ricerca + " trovato nell'indice: " + Arrays.binarySearch(arr, ricerca));
}
}while(ris!=0);
}
}
The code is currently incomplete, but if you can't understand something let me know!
from Recent Questions - Stack Overflow https://ift.tt/2XSJmKQ
https://ift.tt/eA8V8J
Comments
Post a Comment