简体   繁体   中英

Getting out of bounds exception in java code, need help (begginer)

package hr.java.vjezbe.glavna;

import java.math.BigDecimal;
import java.util.Scanner;

import hr.java.vjezbe.entitet.Artikli;
import hr.java.vjezbe.entitet.Kategorija;
import hr.java.vjezbe.entitet.Korisnik;

public class Glavna {

    private static final int BROJ_KORISNIKA = 3;
    private static final int BROJ_KATEGORIJA = 3;
    private static int n;

    public static void main(String[] args) {

        Scanner skener = new Scanner(System.in);

        Korisnik[] korisnici = new Korisnik[BROJ_KORISNIKA];
        Kategorija[] kategorije = new Kategorija[BROJ_KATEGORIJA];
        Artikli[] artikli = new Artikli[n];

        for (int i = 0; i < BROJ_KORISNIKA; i++) {
            System.out.println("Unesite " + (i + 1) + ". korisnika:");
            System.out.print("Unesite ime >> ");
            String ime = skener.nextLine();
            System.out.print("Unesite prezime >> ");
            String prezime = skener.nextLine();
            System.out.print("Unesite E-mail >> ");
            String email = skener.nextLine();
            System.out.print("Unesite broj telefona >> ");
            String telefon = skener.nextLine();

            korisnici[i] = new Korisnik(ime, prezime, email, telefon);

        }

        for (int i = 0; i < BROJ_KATEGORIJA; i++) {
            System.out.println("Unesite naziv " + (i + 1) + ". kategorije: ");
            String naziv = skener.nextLine();
            System.out.print("Unesite broj artikala za tu kategoriju >> ");
            n = skener.nextInt();
            skener.nextLine();
            int counter = 0;
            do {
                System.out.print("Unesite naslov artikla >> ");
                String naslov = skener.nextLine();
                System.out.print("Unesite opis artikla >> ");
                String opis = skener.nextLine();
                System.out.print("Unesite cijenu artikla (sa zarezom) >> ");
                BigDecimal cijena = skener.nextBigDecimal();
                skener.nextLine();
                artikli[n] = new Artikli(naslov, opis, cijena);
                counter++;
            } while (counter < n);

            kategorije[i]= new Kategorija(naziv, artikli);

        }
        skener.close();
    }

}

I have a problem with my code where it throws an out of bounds exception after i input the price which is called cijena. I don't know know where the problem is the code should ask the user to input the number of categories and after that make him enter it and save them in a array and after that save that array in another one called kategorije. Any help is wellcomed and advice.

n = skener.nextInt();

This line, you are taking an int value via your input. Lets say 5; so, n = 5;

At this line =>

artikli[n] = new Artikli(naslov, opis, cijena);

you are trying to access artikli[5] index however the size of your array is 0. You initiated before this array with n = 0 as you didn't initialized 'n', uninitialized int variable will be 0 by default=>

private static int n; //which is 0 by dafault

Artikli[] artikli = new Artikli[n]; // creates an array named artikli of size 0

first you have to initialized the array to the appropriate size then you will be able to access your Artikli[n] index.

Quick Solution:

replace ->  Artikli[] artikli = new Artikli[n];
with this-> Artikli[] artikli = new Artikli[1000]; 

assuming your int input will be less than 1000 so that you can access index artikli[0 to 999]

I've tried to explain as detailed as possible. If you have any confusions, comment.

You have not declared the value of n. You have to specify it because list practically cannot be 0. Do something like this:

private static int n = 3;


artikli[n] = new Artikli(naslov, opis, cijena);

Error because list is smaller than the values that you want give it

If this help you please vote me up. I'am here new and want to have some basic reputation. Thanks! 😀

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM